-
Notifications
You must be signed in to change notification settings - Fork 219
/
config.go
118 lines (87 loc) · 2.93 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
package cmd
import (
"errors"
"fmt"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/docstrings"
"github.com/logrusorgru/aurora"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/helpers"
)
func newConfigCommand(client *client.Client) *Command {
configStrings := docstrings.Get("config")
cmd := BuildCommandKS(nil, nil, configStrings, client, requireSession, requireAppName)
configDisplayStrings := docstrings.Get("config.display")
BuildCommandKS(cmd, runDisplayConfig, configDisplayStrings, client, requireSession, requireAppName)
configSaveStrings := docstrings.Get("config.save")
BuildCommandKS(cmd, runSaveConfig, configSaveStrings, client, requireSession, requireAppName)
configValidateStrings := docstrings.Get("config.validate")
BuildCommandKS(cmd, runValidateConfig, configValidateStrings, client, requireSession, requireAppName)
return cmd
}
func runDisplayConfig(ctx *cmdctx.CmdContext) error {
cfg, err := ctx.Client.API().GetConfig(ctx.AppName)
if err != nil {
return err
}
//encoder := json.NewEncoder(os.Stdout)
//encoder.SetIndent("", " ")
//encoder.Encode(cfg.Definition)
ctx.WriteJSON(cfg.Definition)
return nil
}
func runSaveConfig(ctx *cmdctx.CmdContext) error {
configfilename, err := flyctl.ResolveConfigFileFromPath(ctx.WorkingDir)
if err != nil {
return err
}
if helpers.FileExists(configfilename) {
ctx.Status("create", cmdctx.SERROR, "An existing configuration file has been found.")
confirmation := confirm(fmt.Sprintf("Overwrite file '%s'", configfilename))
if !confirmation {
return nil
}
}
if ctx.AppConfig == nil {
ctx.AppConfig = flyctl.NewAppConfig()
}
ctx.AppConfig.AppName = ctx.AppName
serverCfg, err := ctx.Client.API().GetConfig(ctx.AppName)
if err != nil {
return err
}
ctx.AppConfig.Definition = serverCfg.Definition
return writeAppConfig(ctx.ConfigFile, ctx.AppConfig)
}
func runValidateConfig(commandContext *cmdctx.CmdContext) error {
if commandContext.AppConfig == nil {
return errors.New("App config file not found")
}
commandContext.Status("config", cmdctx.STITLE, "Validating", commandContext.ConfigFile)
serverCfg, err := commandContext.Client.API().ParseConfig(commandContext.AppName, commandContext.AppConfig.Definition)
if err != nil {
return err
}
if serverCfg.Valid {
fmt.Println(aurora.Green("✓").String(), "Configuration is valid")
return nil
}
printAppConfigErrors(*serverCfg)
return errors.New("App configuration is not valid")
}
func printAppConfigErrors(cfg api.AppConfig) {
fmt.Println()
for _, error := range cfg.Errors {
fmt.Println(" ", aurora.Red("✘").String(), error)
}
fmt.Println()
}
func writeAppConfig(path string, appConfig *flyctl.AppConfig) error {
if err := appConfig.WriteToFile(path); err != nil {
return err
}
fmt.Println("Wrote config file", helpers.PathRelativeToCWD(path))
return nil
}