/
config.go
106 lines (93 loc) · 2.23 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
package plural
import (
"io"
"os"
"github.com/pluralsh/plural-cli/pkg/config"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/urfave/cli"
)
func configCommands() []cli.Command {
return []cli.Command{
{
Name: "amend",
Usage: "modify config",
ArgsUsage: "[key] [value]",
Action: latestVersion(handleAmend),
},
{
Name: "read",
Usage: "dumps config",
ArgsUsage: "",
Action: latestVersion(handleRead),
},
{
Name: "import",
Usage: "imports a new config from a given token",
Action: latestVersion(handleConfigImport),
},
}
}
func profileCommands() []cli.Command {
return []cli.Command{
{
Name: "use",
Usage: "moves the config in PROFILE to the current config",
ArgsUsage: "PROFILE",
Action: latestVersion(handleUseProfile),
},
{
Name: "save",
Usage: "saves the current config as PROFILE",
ArgsUsage: "PROFILE",
Action: latestVersion(handleSaveProfile),
},
{
Name: "show",
Usage: "displays the configuration for the current profile",
ArgsUsage: "PROFILE",
Action: latestVersion(handleRead),
},
{
Name: "list",
Usage: "lists all saved profiles",
ArgsUsage: "",
Action: latestVersion(listProfiles),
},
}
}
func handleAmend(c *cli.Context) error {
return config.Amend(c.Args().Get(0), c.Args().Get(1))
}
func handleRead(c *cli.Context) error {
conf := config.Read()
d, err := conf.Marshal()
if err != nil {
return err
}
os.Stdout.Write(d)
return nil
}
func handleConfigImport(c *cli.Context) error {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
return config.FromToken(string(data))
}
func handleUseProfile(c *cli.Context) error {
return config.Profile(c.Args().Get(0))
}
func handleSaveProfile(c *cli.Context) error {
conf := config.Read()
return conf.SaveProfile(c.Args().Get(0))
}
func listProfiles(c *cli.Context) error {
profiles, err := config.Profiles()
if err != nil {
return err
}
headers := []string{"Name", "Email", "Endpoint"}
return utils.PrintTable(profiles, headers, func(profile *config.VersionedConfig) ([]string, error) {
return []string{profile.Metadata.Name, profile.Spec.Email, profile.Spec.BaseUrl()}, nil
})
}