forked from fluxcd/flux
/
set_config_cmd.go
74 lines (61 loc) · 1.54 KB
/
set_config_cmd.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
package main
import (
"io/ioutil"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/weaveworks/flux"
)
type setConfigOpts struct {
*rootOpts
file string
key bool
}
func newSetConfig(parent *rootOpts) *setConfigOpts {
return &setConfigOpts{rootOpts: parent}
}
func (opts *setConfigOpts) Command() *cobra.Command {
cmd := &cobra.Command{
Use: "set-config",
Short: "set configuration values for an instance",
Example: makeExample(
"fluxctl set-config --file=./dev/flux-conf.yaml --generate-deploy-key",
),
RunE: opts.RunE,
}
cmd.Flags().StringVarP(&opts.file, "file", "f", "", "A file to upload as configuration; this will overwrite all values.")
cmd.Flags().BoolVarP(&opts.key, "generate-deploy-key", "k", false, "Generate and replace Git deploy key")
return cmd
}
func (opts *setConfigOpts) RunE(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errorWantedNoArgs
}
if cmd.Flags().NFlag() == 0 {
return newUsageError("a flag is required")
}
if opts.key {
err := opts.GitGenerateKey()
if err != nil {
return err
}
}
if opts.file != "" {
var config flux.UnsafeInstanceConfig
bytes, err := ioutil.ReadFile(opts.file)
if err == nil {
err = yaml.Unmarshal(bytes, &config)
}
if err != nil {
return errors.Wrapf(err, "reading config from file")
}
err = opts.API.SetConfig(noInstanceID, config)
if err != nil {
return err
}
}
return nil
}
func (opts *setConfigOpts) GitGenerateKey() error {
return opts.API.GenerateDeployKey(noInstanceID)
}