-
Notifications
You must be signed in to change notification settings - Fork 26
/
configure.go
114 lines (90 loc) · 2.33 KB
/
configure.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
package cmd
import (
"html/template"
"io"
"os"
"path"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/fatih/color"
)
type ConfigureCmd struct {
PolicyName string `arg:"" required:"" help:"policy name"`
Resource string `short:"r" required:"" help:"resource url"`
Stdout bool `short:"p" help:"generated configuration is printed to stdout but not saved"`
EdgeDirectory bool `short:"d" help:"enable edge directory" default:"false"`
SeedMetadata bool `short:"s" help:"enable seed metadata" default:"false"`
}
func (cmd ConfigureCmd) Run(c *cc.CommonCtx) error {
color.Green(">>> configure policy")
configDir, err := CreateConfigDir()
if err != nil {
return err
}
if _, err := CreateCertsDir(); err != nil {
return err
}
if _, err := CreateDataDir(); err != nil {
return err
}
params := templateParams{
PolicyName: cmd.PolicyName,
Resource: cmd.Resource,
EdgeDirectory: cmd.EdgeDirectory,
SeedMetadata: cmd.SeedMetadata,
}
color.Green("policy name: %s", params.PolicyName)
var w io.Writer
if cmd.Stdout {
w = c.UI.Output()
} else {
w, err = os.Create(path.Join(configDir, "config.yaml"))
if err != nil {
return err
}
}
return WriteConfig(w, configTemplate, ¶ms)
}
func CreateConfigDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
configDir := path.Join(home, "/.config/topaz/cfg")
if fi, err := os.Stat(configDir); err == nil && fi.IsDir() {
return configDir, nil
}
return configDir, os.MkdirAll(configDir, 0700)
}
func CreateCertsDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
certsDir := path.Join(home, "/.config/topaz/certs")
if fi, err := os.Stat(certsDir); err == nil && fi.IsDir() {
return certsDir, nil
}
return certsDir, os.MkdirAll(certsDir, 0700)
}
func CreateDataDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
dataDir := path.Join(home, "/.config/topaz/db")
if fi, err := os.Stat(dataDir); err == nil && fi.IsDir() {
return dataDir, nil
}
return dataDir, os.MkdirAll(dataDir, 0700)
}
func WriteConfig(w io.Writer, templ string, params *templateParams) error {
t, err := template.New("config").Parse(templ)
if err != nil {
return err
}
err = t.Execute(w, params)
if err != nil {
return err
}
return nil
}