-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
81 lines (67 loc) · 1.39 KB
/
commands.go
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
80
81
package phong
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/urfave/cli/v2"
)
// Flags variable
var Flags = []cli.Flag{
flagName,
}
// Commands variable
var Commands = []*cli.Command{
commandList,
}
var usages = map[string]string{
"list": "| phong l",
}
var flagName = &cli.StringFlag{
Name: "region, r",
Aliases: []string{"r"},
Value: "JP",
Usage: "Pass a `<country code>`",
DefaultText: "JP",
}
var commandList = &cli.Command{
Name: "list",
Aliases: []string{"l"},
Usage: "Show the list of country code (alpha-2)",
Action: doList,
}
func init() {
usages := setUsages()
cli.CommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
phong {{.Name}} ` + usages + `{{ "\n\n" }}`
}
func setUsages() string {
s := "{{if false}}"
for _, command := range append(Commands) {
s = s + fmt.Sprintf("{{else if (eq .Name %q)}}%s", command.Name, usages[command.Name])
}
return s + "{{end}}"
}
func doList(c *cli.Context) error {
if c.NArg() != 0 {
cli.ShowCommandHelp(c, "list")
os.Exit(exitErr)
}
confirm(len(countryList))
for k, v := range countryList {
fmt.Printf("%s %s -- %s\n", v.flag, k, v.name)
}
return nil
}
func confirm(n int) {
fmt.Printf("Display all %d lines? (Y or n)", n)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
t := scanner.Text()
if strings.ToLower(t) == "y" || len(t) == 0 {
return
}
os.Exit(exitErr)
}