github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/view/configure.go (about) 1 /* 2 Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package view 17 18 import ( 19 "github.com/awslabs/clencli/cobra/aid" 20 "github.com/awslabs/clencli/cobra/dao" 21 "github.com/awslabs/clencli/cobra/model" 22 "github.com/sirupsen/logrus" 23 "github.com/spf13/cobra" 24 ) 25 26 // CREDENTIALS 27 28 // CreateCredentials create the credentials 29 func CreateCredentials(cmd *cobra.Command, name string) model.Credentials { 30 cmd.Println("> Credentials") 31 var credentials model.Credentials 32 cProfile := createCredentialProfile(cmd, name) 33 credentials.Profiles = append(credentials.Profiles, cProfile) 34 return credentials 35 } 36 37 func createCredentialProfile(cmd *cobra.Command, name string) model.CredentialProfile { 38 cmd.Println(">> Profile: " + name) 39 var cProfile model.CredentialProfile 40 cProfile.Name = name 41 cProfile.Description = "managed by clencli" 42 43 var cred model.Credential 44 cred = askAboutCredential(cmd, cred) 45 46 cProfile.Credentials = append(cProfile.Credentials, cred) 47 48 for { 49 answer := aid.GetUserInputAsBool(cmd, "Would you like to setup another credential?", false) 50 if answer { 51 var newCred model.Credential 52 newCred = askAboutCredential(cmd, newCred) 53 cProfile.Credentials = append(cProfile.Credentials, newCred) 54 } else { 55 break 56 } 57 } 58 59 return cProfile 60 } 61 62 func askAboutCredential(cmd *cobra.Command, credential model.Credential) model.Credential { 63 cmd.Println(">>>> Credential") 64 credential.Name = aid.GetUserInputAsString(cmd, ">>>>> Name", credential.Name) 65 66 credential.Provider = aid.GetUserInputAsString(cmd, ">>>>> Provider", credential.Provider) 67 credential.AccessKey = aid.GetSensitiveUserInputAsString(cmd, ">>>>> Access Key", credential.AccessKey) 68 credential.SecretKey = aid.GetSensitiveUserInputAsString(cmd, ">>>>> Secret Key", credential.SecretKey) 69 credential.SessionToken = aid.GetSensitiveUserInputAsString(cmd, ">>>>> Session Token", credential.SessionToken) 70 return credential 71 } 72 73 // UpdateCredentials update the given credentials 74 func UpdateCredentials(cmd *cobra.Command, name string) model.Credentials { 75 cmd.Println("> Credentials") 76 77 credentials, err := dao.GetCredentials() 78 if err != nil { 79 logrus.Fatalf("unable to update credentials\n%v", err) 80 } 81 82 found := false 83 for i, profile := range credentials.Profiles { 84 if profile.Name == name { 85 found = true 86 credentials.Profiles[i] = askAboutCredentialProfile(cmd, profile) 87 } 88 } 89 90 if !found { 91 cmd.Printf("No credentials not found for profile %s\n", name) 92 } 93 94 return credentials 95 } 96 97 func askAboutCredentialProfile(cmd *cobra.Command, profile model.CredentialProfile) model.CredentialProfile { 98 cmd.Println(">> Profile: " + profile.Name) 99 profile.Name = aid.GetUserInputAsString(cmd, ">>> Name", profile.Name) 100 profile.Description = aid.GetUserInputAsString(cmd, ">>> Description", profile.Description) 101 102 for i, credential := range profile.Credentials { 103 profile.Credentials[i] = askAboutCredential(cmd, credential) 104 } 105 106 return profile 107 } 108 109 // CONFIGURATIONS 110 111 // CreateConfigurations create the configuration file with the given profile name 112 func CreateConfigurations(cmd *cobra.Command, name string) model.Configurations { 113 cmd.Println("> Configurations") 114 var configurations model.Configurations 115 cProfile := createConfigurationProfile(cmd, name) 116 configurations.Profiles = append(configurations.Profiles, cProfile) 117 return configurations 118 } 119 120 // createConfigurationProfile create the given profile name into the configurations file, return the profile created 121 func createConfigurationProfile(cmd *cobra.Command, name string) model.ConfigurationProfile { 122 cmd.Println(">> Profile: " + name) 123 var cProfile model.ConfigurationProfile 124 cProfile.Name = name 125 cProfile.Description = "managed by clencli" 126 127 var conf model.Configuration 128 conf = askAboutConfiguration(cmd, conf) 129 cProfile.Configurations = append(cProfile.Configurations, conf) 130 131 for { 132 answer := aid.GetUserInputAsBool(cmd, "Would you like to setup another configuration?", false) 133 if answer { 134 var newConf model.Configuration 135 newConf = askAboutConfiguration(cmd, newConf) 136 cProfile.Configurations = append(cProfile.Configurations, newConf) 137 } else { 138 break 139 } 140 } 141 142 return cProfile 143 } 144 145 func askAboutConfiguration(cmd *cobra.Command, conf model.Configuration) model.Configuration { 146 cmd.Println(">>> Configuration") 147 conf.Name = aid.GetUserInputAsString(cmd, ">>>> Name", conf.Name) 148 149 conf.Unsplash = askAboutUnsplashConfiguration(cmd, conf.Unsplash) 150 conf.Initialization = askAboutInitialization(cmd, conf.Initialization) 151 return conf 152 } 153 154 // askAboutUnsplashConfiguration ask user about configuration 155 func askAboutUnsplashConfiguration(cmd *cobra.Command, unsplash model.Unsplash) model.Unsplash { 156 // configuration can have many types: Unsplash, AWS, etc 157 answer := aid.GetUserInputAsBool(cmd, "Would you like to setup Unsplash configuration?", false) 158 if answer { 159 cmd.Println(">>>> Unsplash") 160 unsplash.RandomPhoto = askAboutUnsplashRandomPhoto(cmd, unsplash.RandomPhoto) 161 162 } else { 163 cmd.Println("Skipping Unplash configuration ...") 164 } 165 return unsplash 166 } 167 168 func askAboutUnsplashRandomPhoto(cmd *cobra.Command, randomPhoto model.UnsplashRandomPhoto) model.UnsplashRandomPhoto { 169 // unsplash configuration may have multiple nested configuration, such as random photo, etc... 170 cmd.Println(">>>>>> Random Photo") 171 randomPhoto.Parameters = askAboutUnsplashRandomPhotoParameters(cmd, randomPhoto.Parameters) 172 return randomPhoto 173 } 174 175 func askAboutUnsplashRandomPhotoParameters(cmd *cobra.Command, params model.UnsplashRandomPhotoParameters) model.UnsplashRandomPhotoParameters { 176 cmd.Println(">>>>>>> Parameters") 177 params.Collections = aid.GetUserInputAsString(cmd, ">>>>>>>> Collections (Public collection ID to filter selection. If multiple, comma-separated) ", params.Collections) 178 params.Featured = aid.GetUserInputAsBool(cmd, ">>>>>>>> Featured (Limit selection to featured photos. Valid values: false and true)", params.Featured) 179 params.Filter = aid.GetUserInputAsString(cmd, ">>>>>>>> Filter (Limit results by content safety. Valid values are low and high)", params.Filter) 180 params.Orientation = aid.GetUserInputAsString(cmd, ">>>>>>>> Orientation (Filter by photo orientation. Valid values: landscape, portrait, squarish)", params.Orientation) 181 params.Query = aid.GetUserInputAsString(cmd, ">>>>>>>> Query (Limit selection to photos matching a search term)", params.Query) 182 params.Size = aid.GetUserInputAsString(cmd, ">>>>>>>> Size (Photos size. Valid values: all, thumb, small, regular, full, raw)", params.Size) 183 params.Username = aid.GetUserInputAsString(cmd, ">>>>>>>> Username (Limit selection to a single user)", params.Username) 184 185 return params 186 } 187 188 func askAboutInitialization(cmd *cobra.Command, init model.Initialization) model.Initialization { 189 // configuration can have many types: Unsplash, AWS, etc 190 answer := aid.GetUserInputAsBool(cmd, "Would you like to setup a customized initialization?", false) 191 if answer { 192 193 cmd.Println(">>>> Initialization") 194 if len(init.Files) == 0 { 195 var file model.File 196 file = askAboutInitializationFile(cmd, file) 197 init.Files = append(init.Files, file) 198 } else { 199 for i, f := range init.Files { 200 init.Files[i] = askAboutInitializationFile(cmd, f) 201 } 202 } 203 204 for { 205 answer = aid.GetUserInputAsBool(cmd, "Would you like to setup a new file?", false) 206 if answer { 207 var file model.File 208 file = askAboutInitializationFile(cmd, file) 209 init.Files = append(init.Files, file) 210 } else { 211 break 212 } 213 } 214 215 } 216 217 return init 218 } 219 220 func askAboutInitializationFile(cmd *cobra.Command, file model.File) model.File { 221 cmd.Println(">>>>> File") 222 file.Path = aid.GetUserInputAsString(cmd, ">>>>>> Path", file.Path) 223 file.Src = aid.GetUserInputAsString(cmd, ">>>>>> Source", file.Src) 224 file.Dest = aid.GetUserInputAsString(cmd, ">>>>>> Destination", file.Dest) 225 file.State = aid.GetUserInputAsString(cmd, ">>>>>> State", file.State) 226 return file 227 } 228 229 // UpdateConfigurations update the given configurations 230 func UpdateConfigurations(cmd *cobra.Command, name string) model.Configurations { 231 cmd.Println("> Configurations") 232 configurations, err := dao.GetConfigurations() 233 if err != nil { 234 logrus.Fatalf("unable to update configurations\n%v", err) 235 } 236 237 found := false 238 for i, profile := range configurations.Profiles { 239 if profile.Name == name { 240 found = true 241 configurations.Profiles[i] = askAboutConfigurationProfile(cmd, profile) 242 } 243 } 244 245 if !found { 246 cmd.Printf("No configurations not found for profile %s\n", name) 247 } 248 249 return configurations 250 } 251 252 func askAboutConfigurationProfile(cmd *cobra.Command, profile model.ConfigurationProfile) model.ConfigurationProfile { 253 cmd.Println(">> Profile: " + profile.Name) 254 profile.Name = aid.GetUserInputAsString(cmd, ">>> Name", profile.Name) 255 profile.Description = aid.GetUserInputAsString(cmd, ">>> Description", profile.Description) 256 257 for i, configuration := range profile.Configurations { 258 profile.Configurations[i] = askAboutConfiguration(cmd, configuration) 259 } 260 261 return profile 262 }