-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtour.go
More file actions
69 lines (58 loc) · 1.83 KB
/
Copy pathtour.go
File metadata and controls
69 lines (58 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Package main provides a CLI tool for running Go tour examples.
// It executes example programs by specifying a module and program name,
// making it easier to run and explore the Go tour code samples.
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
)
// Logger interface for dependency injection to enable testing of Fatal calls
type Logger interface {
Printf(format string, v ...interface{})
Println(v ...interface{})
Fatal(v ...interface{})
Fatalf(format string, v ...interface{})
}
// main initializes and runs the tour example CLI, which executes
// specific examples from the Go tour by providing a module and program name.
func main() {
logger := log.New(os.Stderr, "", 0)
runMain(logger)
}
// runMain contains the main logic and accepts a logger for dependency injection
func runMain(logger Logger) {
flag.Usage = func() {
logger.Printf("Usage: %s <module> <program>", os.Args[0])
logger.Println("\nArguments:")
logger.Println(" module The module name (e.g., welcome)")
logger.Println(" program The program name (e.g., hello)")
logger.Println("\nFlags:")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 2 {
flag.Usage()
logger.Fatal("insufficient arguments")
}
module := flag.Arg(0)
program := flag.Arg(1)
output, err := run(module, program)
if err != nil {
logger.Fatalf("Failed to run %s/%s: %v", module, program, err)
}
fmt.Print(output)
}
// run executes the specified program in the given module and returns its output
func run(module string, program string) (string, error) {
programPath := filepath.Join(module, program, program+".go")
if _, err := os.Stat(programPath); os.IsNotExist(err) {
return "", fmt.Errorf("program %s not found", programPath)
}
cmd := exec.Command("go", "run", programPath)
output, err := cmd.CombinedOutput()
return string(output), err
}