-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
72 lines (57 loc) · 1.98 KB
/
context.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
package profile
import (
"errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"bunnyshell.com/cli/pkg/lib"
)
func init() {
profileName := &lib.CLIContext.ProfileName
var organization string
var project string
var environment string
var serviceComponent string
var contextCommand = &cobra.Command{
Use: "context",
RunE: func(cmd *cobra.Command, args []string) error {
profile, err := lib.GetProfile(lib.CLIContext.ProfileName)
if err != nil {
return lib.FormatCommandError(cmd, err)
}
var updates []string
if organization != "" {
updates = append(updates, "organization")
profile.Context.Organization = organization
}
if project != "" {
updates = append(updates, "project")
profile.Context.Project = project
}
if environment != "" {
updates = append(updates, "environment")
profile.Context.Environment = environment
}
if serviceComponent != "" {
updates = append(updates, "service component")
profile.Context.ServiceComponent = serviceComponent
}
if len(updates) == 0 {
return errors.New("no context changes")
}
if err := viper.WriteConfig(); err != nil {
return lib.FormatCommandError(cmd, err)
}
return lib.FormatCommandData(cmd, map[string]interface{}{
"message": "Updated profile context",
"data": updates,
})
},
}
contextCommand.Flags().StringVar(profileName, "name", *profileName, "Name of the profile")
contextCommand.MarkFlagRequired("name")
contextCommand.Flags().StringVar(&organization, "organization", organization, "Set Organization context for all resources")
contextCommand.Flags().StringVar(&project, "project", project, "Set Project context for all resources")
contextCommand.Flags().StringVar(&environment, "environment", environment, "Set Organization context for all resources")
contextCommand.Flags().StringVar(&serviceComponent, "serviceComponent", serviceComponent, "Set Organization context for all resources")
mainCmd.AddCommand(contextCommand)
}