-
Notifications
You must be signed in to change notification settings - Fork 57
/
profile.go
126 lines (106 loc) · 3.22 KB
/
profile.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
package main
import (
"fmt"
"github.com/rackspace/rack/commandoptions"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
"github.com/rackspace/rack/internal/gopkg.in/ini.v1"
"github.com/rackspace/rack/util"
)
var commandActivate = cli.Command{
Name: "activate",
Description: "Activate a profile. Activating a profile will have the following\n" +
"\torder for the way that `rack` looks for command configuration values:\n" +
"\t1. command-line options\n" +
"\t2. the active profile\n" +
"\t3. the default profile\n" +
"\t4. environment variables\n" +
"\n" +
"\tNOTE: The safest way to use `rack` is by always explicitly providing\n" +
"\tconfiguration values (like `--profile`) as command-line options. Running\n" +
"\ta command without knowing which profile is active can result in unintended\n" +
"\tconsequences.",
Usage: "rack profile activate --name <profile-name>",
Action: profileActivate,
Flags: profileFlagsActivate,
BashComplete: func(c *cli.Context) {
commandoptions.CompleteFlags(profileFlagsActivate)
},
}
var commandList = cli.Command{
Name: "list",
Description: "List profile information",
Usage: "rack profile list",
Action: profileList,
}
var commandsProfile = []cli.Command{
commandList,
}
var commandsProfileAdmin = []cli.Command{
commandActivate,
}
func profileCommandsGet(canActivateProfile bool) []cli.Command {
if canActivateProfile {
return append(commandsProfile, commandsProfileAdmin...)
}
return commandsProfile
}
var profileFlagsActivate = []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "[required] The name of the profile to activate",
},
}
func profileActivate(c *cli.Context) {
if !c.IsSet("name") {
fmt.Fprintln(c.App.Writer, "You must provide the profile name with the `name` flag")
return
}
profileName := c.String("name")
configFileLoc, err := util.ConfigFileLocation()
if err != nil {
fmt.Fprintf(c.App.Writer, "Error to determining config file location: %s\n", err)
return
}
cfg, err := ini.Load(configFileLoc)
if err != nil {
fmt.Fprintf(c.App.Writer, "Error loading config file: %s\n", err)
return
}
chosenSection, err := cfg.GetSection(profileName)
if err != nil {
fmt.Fprintf(c.App.Writer, "Section [%s] doesn't exist in config file\n", profileName)
return
}
sections := cfg.Sections()
for _, section := range sections {
section.DeleteKey("enabled")
}
chosenSection.Key("enabled").SetValue("true")
err = cfg.SaveTo(configFileLoc)
if err != nil {
fmt.Fprintf(c.App.Writer, "Error saving config file: %s\n", err)
return
}
fmt.Fprintf(c.App.Writer, "Successfully activated profile [%s]\n", profileName)
}
func profileList(c *cli.Context) {
configFileLoc, err := util.ConfigFileLocation()
if err != nil {
fmt.Fprintf(c.App.Writer, "Error to determining config file location: %s\n", err)
return
}
cfg, err := ini.Load(configFileLoc)
if err != nil {
fmt.Fprintf(c.App.Writer, "Error loading config file: %s\n", err)
return
}
sections := cfg.Sections()
for _, section := range sections {
fmt.Fprintf(c.App.Writer, "[%s]\n", section.Name())
for k, v := range section.KeysHash() {
fmt.Fprintf(c.App.Writer, "%s = %s\n", k, v)
}
fmt.Fprintln(c.App.Writer)
fmt.Fprintln(c.App.Writer)
}
}