-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepl.go
More file actions
79 lines (67 loc) · 1.35 KB
/
Copy pathrepl.go
File metadata and controls
79 lines (67 loc) · 1.35 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
70
71
72
73
74
75
76
77
78
79
//go run repl.go
package main
import (
"os"
"fmt"
"github.com/urfave/cli"
"os/signal"
"syscall"
"bufio"
"strings"
)
func Repl(c *cli.Context) {
input := ""
for input != "." {
in := bufio.NewReader(os.Stdin)
fmt.Print("Hello> ")
input, _ := in.ReadString('\n')
inputArgs := strings.Fields(input)
if len(inputArgs) == 0 {
//fmt.Println(color("Command not recognized. Have you tried 'help'?", "response"))
//fmt.Println(color("", "response"))
} else {
command := inputArgs[0]
switch command {
//case "clear", "cls":
case "message":
fmt.Println("hello my friend!")
case "exit":
fmt.Println("Bye!")
os.Exit(3)
default:
fmt.Println("Command not recognized. Have you tried 'help'?")
}
}
}
}
func main() {
app := cli.NewApp()
app.Version = "1.1"
app.Name = "hello-cli"
app.Usage = "A CLI tool for Tech Talks"
app.Action = func(c *cli.Context) error {
//Start the REPL if not argument given
cs := make(chan os.Signal, 2)
signal.Notify(cs, os.Interrupt, syscall.SIGTERM)
go func() {
<-cs
fmt.Println("Bye!")
os.Exit(0)
}()
if c.NArg() == 0 {
Repl(c)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "message",
Usage: "write a message",
Action: func(c *cli.Context) error {
fmt.Println("hello", c.Args().First())
return nil
},
},
}
app.Run(os.Args)
}