-
Notifications
You must be signed in to change notification settings - Fork 233
/
config.go
122 lines (93 loc) · 2.86 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
package cmd
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/superfly/flyctl/docstrings"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/helpers"
)
func newConfigCommand() *Command {
configStrings := docstrings.Get("config")
cmd := &Command{
Command: &cobra.Command{
Use: configStrings.Usage,
Short: configStrings.Short,
Long: configStrings.Long,
},
}
configDisplayStrings := docstrings.Get("config.display")
BuildCommand(cmd, runDisplayConfig, configDisplayStrings.Usage, configDisplayStrings.Short, configDisplayStrings.Long, os.Stdout, requireSession, requireAppName)
configSaveStrings := docstrings.Get("config.save")
BuildCommand(cmd, runSaveConfig, configSaveStrings.Usage, configSaveStrings.Short, configSaveStrings.Long, os.Stdout, requireSession, requireAppName)
configValidateStrings := docstrings.Get("config.validate")
BuildCommand(cmd, runValidateConfig, configValidateStrings.Usage, configValidateStrings.Short, configValidateStrings.Long, os.Stdout, requireSession, requireAppName)
return cmd
}
func runDisplayConfig(ctx *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)
return nil
}
func runSaveConfig(ctx *CmdContext) error {
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(ctx *CmdContext) error {
if ctx.AppConfig == nil {
return errors.New("App config file not found")
}
fmt.Println("Validating", ctx.ConfigFile)
serverCfg, err := ctx.Client.API().ParseConfig(ctx.AppName, ctx.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 printAppConfigServices(prefix string, cfg api.AppConfig) {
if len(cfg.Services) > 0 {
fmt.Println(prefix, "Services")
for _, svc := range cfg.Services {
fmt.Println(prefix, " ", svc.Description)
}
}
}
func writeAppConfig(path string, appConfig *flyctl.AppConfig) error {
if !confirmFileOverwrite(path) {
return nil
}
if err := appConfig.WriteToFile(path); err != nil {
return err
}
fmt.Println("Wrote config file", helpers.PathRelativeToCWD(path))
return nil
}