-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
76 lines (63 loc) · 1.66 KB
/
cli.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
package main
import (
"flag"
"fmt"
"io"
"os"
)
// Exit codes are int values that represent an exit code for a particular error.
const (
ExitCodeOK int = 0
ExitCodeError int = 1 + iota
)
// CLI is the command line object
type CLI struct {
// outStream and errStream are the stdout and stderr
// to write message from the CLI.
outStream, errStream io.Writer
}
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
var (
rootDir string = ""
recipe bool
version bool
)
if len(args) < 2 {
fmt.Println("No role or recipes are specified.")
return 1
}
// Define option flag parse
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
flags.SetOutput(cli.errStream)
flags.BoolVar(&recipe, "recipe", false, "Specify a recipe.")
// flags.BoolVar(&recipe, "re", false, "(Short)")
flags.StringVar(&rootDir, "rootdir", "", "")
flags.StringVar(&rootDir, "r", "", "(Short)")
flags.BoolVar(&version, "version", false, "Print version information and quit.")
// Parse commandline flag
if err := flags.Parse(args[1:]); err != nil {
return ExitCodeError
}
// Show version
if version {
fmt.Fprintf(cli.errStream, "%s version %s\n", Name, Version)
return ExitCodeOK
}
currentDir, _ := os.Getwd()
if rootDir == "" {
rootDir = currentDir
} else if rootDir[:1] != "/" {
rootDir = currentDir + "/" + rootDir
}
// check rootdir has 'cookbooks' dir
if _, err := os.Stat(rootDir + "/cookbooks"); err != nil {
fmt.Println("cookbooks directory is not found.")
return 1
}
_ = recipe
cv := &Chefviz{outStream: cli.outStream, errStream: cli.errStream}
cv.newChefviz()
cv.main(rootDir, flags.Args())
return ExitCodeOK
}