|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | | - "github.com/murex/gamekit-coffeemachine/settings" |
6 | | - "io" |
7 | | - "log" |
| 4 | + "github.com/murex/gamekit-coffeemachine/cli" |
8 | 5 | "os" |
9 | | - "os/exec" |
10 | | - "path" |
11 | | - "path/filepath" |
12 | | - "strings" |
13 | | - "time" |
14 | | -) |
15 | | - |
16 | | -var ( |
17 | | - infoLog = log.New(os.Stderr, "🟩 ", 0) |
18 | | - errorLog = log.New(os.Stderr, "🟥 ", 0) |
19 | | - stdinLog = log.New(os.Stderr, "⏩ ", 0) |
20 | | - stdoutLog = log.New(os.Stderr, "⏪ ", 0) |
21 | 6 | ) |
22 | 7 |
|
23 | 8 | // main is the entry point of the coffee machine command line runner |
24 | 9 | func main() { |
25 | | - infoLog.Println("starting coffee machine command line interface", settings.BuildVersion) |
26 | | - runCli(parseArgs()) |
27 | | - infoLog.Println("closing coffee machine process runner") |
28 | | -} |
29 | | - |
30 | | -// runCli starts the coffee machine command line interface. |
31 | | -// runDir is the directory where the coffee machine implementation is located. |
32 | | -// It must contain a run.sh script that starts the coffee machine implementation. |
33 | | -func runCli(runDir string) { |
34 | | - cmd := exec.Command("bash", "./run.sh") |
35 | | - cmd.Dir = runDir |
36 | | - |
37 | | - stdin, errStdinPipe := cmd.StdinPipe() |
38 | | - if errStdinPipe != nil { |
39 | | - errorLog.Fatal(errStdinPipe) |
40 | | - } |
41 | | - |
42 | | - stdout, errStdoutPipe := cmd.StdoutPipe() |
43 | | - if errStdoutPipe != nil { |
44 | | - errorLog.Fatalln(errStdoutPipe) |
45 | | - } |
46 | | - |
47 | | - stderr, errStderrPipe := cmd.StderrPipe() |
48 | | - if errStderrPipe != nil { |
49 | | - errorLog.Fatalln(errStderrPipe) |
50 | | - } |
51 | | - |
52 | | - go parseAndForward(stdin) |
53 | | - |
54 | | - if errStart := cmd.Start(); errStart != nil { |
55 | | - errorLog.Fatalln(errStart) |
56 | | - } |
57 | | - |
58 | | - go scanAndLog(stdout, stdoutLog) |
59 | | - go scanAndLog(stderr, errorLog) |
60 | | - |
61 | | - if errWait := cmd.Wait(); errWait != nil { |
62 | | - errorLog.Fatalln(errWait) |
63 | | - } |
64 | | -} |
65 | | - |
66 | | -func parseArgs() string { |
67 | | - if len(os.Args) < 2 { |
68 | | - errorLog.Fatalf("syntax: %s <language-implementation-path>", path.Base(os.Args[0])) |
69 | | - } |
70 | | - langImplPath, errAbs := filepath.Abs(os.Args[1]) |
71 | | - if errAbs != nil { |
72 | | - errorLog.Fatal(errAbs) |
73 | | - } |
74 | | - language := filepath.Base(langImplPath) |
75 | | - |
76 | | - infoLog.Println("implementation path is", langImplPath) |
77 | | - infoLog.Println("implementation language is", language) |
78 | | - return langImplPath |
79 | | -} |
80 | | - |
81 | | -func parseAndForward(w io.WriteCloser) { |
82 | | - defer func(stdin io.WriteCloser) { |
83 | | - _ = stdin.Close() |
84 | | - }(w) |
85 | | - const invite = "enter command to send to the coffee machine" |
86 | | - infoLog.Println(invite) |
87 | | - scanner := bufio.NewScanner(os.Stdin) |
88 | | - for scanner.Scan() { |
89 | | - msg := strings.TrimSpace(strings.ToLower(scanner.Text())) |
90 | | - stdinLog.Println(msg) |
91 | | - _, err := io.WriteString(w, msg+"\n") |
92 | | - if err != nil { |
93 | | - errorLog.Fatalln(err) |
94 | | - } |
95 | | - time.Sleep(100 * time.Millisecond) |
96 | | - infoLog.Println(invite) |
97 | | - } |
98 | | - |
99 | | - if errScanner := scanner.Err(); errScanner != nil { |
100 | | - errorLog.Fatalln(errScanner) |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -func scanAndLog(r io.ReadCloser, logger *log.Logger) { |
105 | | - scanner := bufio.NewScanner(r) |
106 | | - for scanner.Scan() { |
107 | | - logger.Println(scanner.Text()) |
108 | | - } |
109 | | - if errScanner := scanner.Err(); errScanner != nil { |
110 | | - errorLog.Fatalln(errScanner) |
111 | | - } |
| 10 | + cli.Run(os.Args) |
112 | 11 | } |
0 commit comments