-
Notifications
You must be signed in to change notification settings - Fork 24
/
configure.go
90 lines (76 loc) · 2.5 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
package cmd
import (
"errors"
"io"
"os"
"path"
"github.com/aserto-dev/topaz/pkg/cc/config"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/fatih/color"
)
type ConfigureCmd struct {
PolicyName string `short:"n" help:"policy name"`
LocalPolicyImage string `short:"l" help:"local policy image name"`
Resource string `short:"r" 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"`
Force bool `flag:"" default:"false" short:"f" required:"false" help:"forcefully create configuration"`
EnableDirectoryV2 bool `flag:"" name:"enable-v2" hidden:"" default:"true" help:"enable directory version 2 services for backwards compatibility"`
}
func (cmd ConfigureCmd) Run(c *cc.CommonCtx) error {
if cmd.PolicyName == "" && cmd.Resource == "" {
if cmd.LocalPolicyImage == "" {
return errors.New("you either need to provide a local policy image or the resource and the policy name for the configuration")
}
}
if !cmd.Stdout {
color.Green(">>> configure policy")
}
configGenerator := config.NewGenerator(cmd.PolicyName).
WithVersion(config.ConfigFileVersion).
WithLocalPolicyImage(cmd.LocalPolicyImage).
WithPolicyName(cmd.PolicyName).
WithResource(cmd.Resource).
WithEdgeDirectory(cmd.EdgeDirectory).
WithEnableDirectoryV2(cmd.EnableDirectoryV2)
configDir, err := configGenerator.CreateConfigDir()
if err != nil {
return err
}
if _, err := configGenerator.CreateCertsDir(); err != nil {
return err
}
certGenerator := GenerateCertsCmd{CertsDir: cc.GetTopazCertsDir()}
err = certGenerator.Run(c)
if err != nil {
return err
}
if _, err := configGenerator.CreateDataDir(); err != nil {
return err
}
var w io.Writer
if cmd.Stdout {
w = c.UI.Output()
} else {
if !cmd.Force {
if _, err := os.Stat(path.Join(configDir, "config.yaml")); err == nil {
c.UI.Exclamation().Msg("A configuration file already exists.")
if !promptYesNo("Do you want to continue?", false) {
return nil
}
}
}
w, err = os.Create(path.Join(configDir, "config.yaml"))
if err != nil {
return err
}
}
if !cmd.Stdout {
if cmd.LocalPolicyImage != "" {
color.Green("using local policy image: %s", cmd.LocalPolicyImage)
return configGenerator.GenerateConfig(w, config.LocalImageTemplate)
}
color.Green("policy name: %s", cmd.PolicyName)
}
return configGenerator.GenerateConfig(w, config.Template)
}