-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
127 lines (106 loc) · 3.04 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"io/ioutil"
"os"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
cfgtypes "github.com/cloudfoundry/config-server/types"
"github.com/jessevdk/go-flags"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Options struct {
Verbose bool `short:"v" long:"verbose" description:"Set to pass in verbose mode."`
Version func() ` long:"version" description:"Show version"`
DefinitionFile string `short:"d" long:"def-file" required:"true" description:"path a file containing definition of credentials"`
StoreFile string `short:"s" long:"var-store" positional-args:"true" required:"true" description:"path to a file which will contains generated credentials"`
}
type VersionInfo struct {
Version string
Commit string
Date string
}
var version string
var commit string
var date string
var mainOpts = &Options{}
var parser = flags.NewParser(mainOpts, flags.HelpFlag|flags.PassDoubleDash|flags.IgnoreUnknown)
func Parse(vInfo VersionInfo, args []string) error {
askVersion := false
mainOpts.Version = func() {
askVersion = true
fmt.Printf("gotof %v, commit %v, built at %v\n", vInfo.Version, vInfo.Commit, vInfo.Date)
}
_, err := parser.ParseArgs(args[1:])
if err != nil {
if errFlag, ok := err.(*flags.Error); ok && askVersion && errFlag.Type == flags.ErrCommandRequired {
return nil
}
if errFlag, ok := err.(*flags.Error); ok && errFlag.Type == flags.ErrCommandRequired {
logrus.Error(err.Error())
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
if errFlag, ok := err.(*flags.Error); ok && errFlag.Type == flags.ErrHelp {
fmt.Println(err.Error())
os.Exit(0)
}
return err
}
if mainOpts.Verbose {
logrus.SetLevel(logrus.TraceLevel)
}
return nil
}
type varDefYaml struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Options interface{} `yaml:"options"`
}
func (v varDefYaml) ToVariableDefinition() boshtpl.VariableDefinition {
return boshtpl.VariableDefinition{
Name: v.Name,
Type: v.Type,
Options: v.Options,
}
}
func getVariableDefinitions() ([]boshtpl.VariableDefinition, error) {
varDefsYaml := make([]varDefYaml, 0)
b, err := ioutil.ReadFile(mainOpts.DefinitionFile)
if err != nil {
return []boshtpl.VariableDefinition{}, err
}
err = yaml.Unmarshal(b, &varDefsYaml)
if err != nil {
return []boshtpl.VariableDefinition{}, err
}
varDefs := make([]boshtpl.VariableDefinition, len(varDefsYaml))
for i, v := range varDefsYaml {
varDefs[i] = v.ToVariableDefinition()
}
return varDefs, nil
}
func run() error {
err := Parse(VersionInfo{
Version: version,
Commit: commit,
Date: date,
}, os.Args)
if err != nil {
return err
}
varsDefs, err := getVariableDefinitions()
if err != nil {
return err
}
fsStore := NewVarsFSStore(mainOpts.StoreFile)
fsStore.ValueGeneratorFactory = cfgtypes.NewValueGeneratorConcrete(NewVarsCertLoader(fsStore))
return fsStore.LoadAndStore(varsDefs)
}
func main() {
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
os.Exit(1)
}
}