/
profile.go
169 lines (145 loc) · 4.58 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"regexp"
)
const provideProfileName = "you must provide a profile name"
var (
isValid = regexp.MustCompile(`^[A-Za-z\d-]+$`).MatchString
profileValue string
)
// setProfileCmd represents the set profile command.
var setProfileCmd = &cobra.Command{
Use: "profile profile-name",
Short: "set a profile value for creating and starting clusters",
Long: `The 'set profile' command sets a profile value for creating and starting clusters.
Profiles can be specified using the '-P' option when creating and starting clusters. They
contain property values to be set prior to the final class and must be surrounded by quotes
and be space delimited. If you set a profile that exists, it will be overwritten.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideProfileName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
profileName = args[0]
err error
found = false
profiles = Config.Profiles
)
// validate profile name
if err = validateProfileName(profileName); err != nil {
return err
}
if len(profileValue) == 0 {
return errors.New("you must provide a value for the profile")
}
// confirm the operation
if !confirmOperation(cmd, fmt.Sprintf("Are you sure you want to set the profile %s to a value of [%s]? (y/n) ", profileName, profileValue)) {
return nil
}
// loop though the list of profiles
for i, v := range profiles {
if v.Name == profileName {
// update
profiles[i].Value = profileValue
found = true
break
}
}
if !found {
// must be new so append
profiles = append(profiles, ProfileValue{Name: profileName, Value: profileValue})
}
viper.Set(profilesKey, profiles)
err = WriteConfig()
if err != nil {
return err
}
cmd.Printf("profile %s was set to value [%s]\n", profileName, profileValue)
return nil
},
}
// removeProfileCmd represents the remove profile command.
var removeProfileCmd = &cobra.Command{
Use: "profile profile-name",
Short: "remove a profile value from the list of profiles",
Long: `The 'remove profile' command removes a profile value from the list of profiles.`,
ValidArgsFunction: completionAllProfiles,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideProfileName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
profileName = args[0]
err error
profiles = Config.Profiles
)
if getProfileValue(profileName) == "" {
return fmt.Errorf("a profile with the name %s does not exist", profileName)
}
// confirm the operation
if !confirmOperation(cmd, fmt.Sprintf("Are you sure you want to remove the profile %s? (y/n) ", profileName)) {
return nil
}
newProfiles := make([]ProfileValue, 0)
// loop though the list of profiles
for _, v := range profiles {
if v.Name != profileName {
newProfiles = append(newProfiles, v)
}
}
viper.Set(profilesKey, newProfiles)
err = WriteConfig()
if err != nil {
return err
}
cmd.Printf("profile %s was removed\n", profileName)
return nil
},
}
// getProfilesCmd represents the get profiles command.
var getProfilesCmd = &cobra.Command{
Use: "profiles",
Short: "display the profiles that have been created",
Long: `The 'get profiles' displays the profiles that have been created.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Println(FormatProfiles(Config.Profiles))
return nil
},
}
func init() {
setProfileCmd.Flags().StringVarP(&profileValue, "value", "v", "", "profile value to set")
_ = setProfileCmd.MarkFlagRequired("value")
setProfileCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
removeProfileCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
}
func validateProfileName(profileName string) error {
if !isValid(profileName) {
return fmt.Errorf("profile name %s must only contain letters, numbers and '", profileName)
}
return nil
}
// getProfileValue returns the value for a given profile or "" if the profile doesn't exist.
func getProfileValue(profileName string) string {
for _, v := range Config.Profiles {
if v.Name == profileName {
return v.Value
}
}
return ""
}