Skip to content

Commit 3103515

Browse files
committed
Move command line interface code to dedicated cli plackage
1 parent 18aeaef commit 3103515

File tree

2 files changed

+114
-103
lines changed

2 files changed

+114
-103
lines changed

cli/runner.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package cli
2+
3+
import (
4+
"bufio"
5+
"github.com/murex/gamekit-coffeemachine/settings"
6+
"io"
7+
"log"
8+
"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+
)
22+
23+
// Run runs the coffee machine command line interface.
24+
func Run(args []string) {
25+
infoLog.Println("starting coffee machine command line interface", settings.BuildVersion)
26+
start(parseArgs(args))
27+
infoLog.Println("closing coffee machine process runner")
28+
}
29+
30+
// start 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 start(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 scanAndSend(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(args []string) string {
67+
if len(args) < 2 {
68+
errorLog.Fatalf("syntax: %s <language-implementation-path>", path.Base(args[0]))
69+
}
70+
langImplPath, errAbs := filepath.Abs(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 scanAndSend(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+
}
112+
}

main.go

Lines changed: 2 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,11 @@
11
package main
22

33
import (
4-
"bufio"
5-
"github.com/murex/gamekit-coffeemachine/settings"
6-
"io"
7-
"log"
4+
"github.com/murex/gamekit-coffeemachine/cli"
85
"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)
216
)
227

238
// main is the entry point of the coffee machine command line runner
249
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)
11211
}

0 commit comments

Comments
 (0)