-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
95 lines (72 loc) · 1.95 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"flag"
"log"
"os"
"os/exec"
"github.com/mitchellh/cli"
add "github.com/franela/vault/commands/add"
cmdimport "github.com/franela/vault/commands/cmdimport"
get "github.com/franela/vault/commands/get"
inita "github.com/franela/vault/commands/init"
recipients "github.com/franela/vault/commands/recipients"
remove "github.com/franela/vault/commands/remove"
repair "github.com/franela/vault/commands/repair"
set "github.com/franela/vault/commands/set"
"github.com/franela/vault/validator"
)
var commandValidator validator.Validator
func init() {
commandValidator = &validator.CommandValidator{}
}
func main() {
if !isGPGInstalled() {
log.Println("Could not find GPG in your PATH. Please make sure it is installed and in your PATH. You may download GPG from https://www.gnupg.org/download/ ")
os.Exit(3)
}
c := initializeCli(os.Args[1:])
exitStatus, err := runCommand(c)
if err != nil {
log.Println(err)
}
os.Exit(exitStatus)
}
func isGPGInstalled() bool {
_, err := exec.LookPath("gpg")
if err != nil {
return false
}
return true
}
func runCommand(cli *cli.CLI) (int, error) {
exitStatus, err := cli.Run()
if exitStatus != 0 {
commandValidator.Validate()
}
return exitStatus, err
}
func initializeCli(args []string) *cli.CLI {
c := cli.NewCLI("vault", "0.0.1")
c.Args = args
devNull, _ := os.Open(os.DevNull)
log.SetOutput(devNull)
c.Commands = map[string]cli.CommandFactory{
"init": inita.Factory,
"set": set.Factory,
"get": get.Factory,
"recipients": recipients.Factory,
"add": add.Factory,
"remove": remove.Factory,
"repair": repair.Factory,
"import": cmdimport.Factory,
}
if !c.IsVersion() && !c.IsHelp() {
initFlags := flag.NewFlagSet("verbose", flag.ContinueOnError)
var verbose = initFlags.Bool("verbose", false, "Logs verbose information to stderr")
initFlags.Parse(args)
if *verbose {
log.SetOutput(os.Stderr)
}
}
return c
}