-
Notifications
You must be signed in to change notification settings - Fork 191
/
config.go
158 lines (127 loc) · 3.11 KB
/
config.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"go.flipt.io/flipt/internal/cmd/util"
"go.flipt.io/flipt/internal/config"
"gopkg.in/yaml.v2"
)
type initCommand struct {
force bool
}
func (c *initCommand) run(cmd *cobra.Command, args []string) error {
defaultFile := providedConfigFile
if defaultFile == "" {
defaultFile = userConfigFile
}
file := defaultFile
ack := c.force
if !ack {
q := []*survey.Question{
{
Name: "file",
Prompt: &survey.Input{
Message: "Configuration file path:",
Default: defaultFile,
},
Validate: survey.Required,
},
}
if err := survey.Ask(q, &file); err != nil {
return err
}
// check if file exists
if _, err := os.Stat(file); err == nil {
// file exists
overwrite, err := util.PromptConfirm("File exists. Overwrite?", false)
if err != nil {
return err
}
if !overwrite {
return nil
}
} else if !os.IsNotExist(err) {
return err
}
}
// get path to file, create directory if not exists
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
return err
}
cfg := config.Default()
cfg.Version = config.Version // write version for backward compatibility
out, err := yaml.Marshal(cfg)
if err != nil {
return err
}
var b bytes.Buffer
b.WriteString("# yaml-language-server: $schema=https://raw.githubusercontent.com/flipt-io/flipt/main/config/flipt.schema.json\n\n")
b.Write(out)
if err := os.WriteFile(file, b.Bytes(), 0600); err != nil {
return err
}
fmt.Printf("Configuration file written to %s\n", file)
return nil
}
type editCommand struct{}
func (c *editCommand) run(cmd *cobra.Command, args []string) error {
// TODO: check if no TTY
file := providedConfigFile
if file == "" {
file = userConfigFile
}
b, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("reading config file: %w", err)
}
content := string(b)
if err := survey.AskOne(&survey.Editor{
Message: "Edit configuration",
Default: string(b),
HideDefault: true,
AppendDefault: true,
FileName: "flipt*.yml",
}, &content); err != nil {
return err
}
// create if not exists, truncate if exists
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("creating config file: %w", err)
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
return fmt.Errorf("writing config file: %w", err)
}
return nil
}
func newConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage Flipt configuration",
Aliases: []string{"cfg"},
}
var (
initCmd = &initCommand{}
editCmd = &editCommand{}
)
var init = &cobra.Command{
Use: "init",
Short: "Initialize Flipt configuration",
RunE: initCmd.run,
}
init.Flags().BoolVarP(&initCmd.force, "force", "y", false, "Overwrite existing configuration file")
var edit = &cobra.Command{
Use: "edit",
Short: "Edit Flipt configuration",
RunE: editCmd.run,
}
cmd.PersistentFlags().StringVar(&providedConfigFile, "config", "", "path to config file")
cmd.AddCommand(init)
cmd.AddCommand(edit)
return cmd
}