github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/controller/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 controller 17 18 import ( 19 "fmt" 20 "os" 21 22 "github.com/awslabs/clencli/cobra/aid" 23 "github.com/awslabs/clencli/cobra/dao" 24 "github.com/awslabs/clencli/cobra/view" 25 "github.com/awslabs/clencli/helper" 26 "github.com/spf13/cobra" 27 ) 28 29 var configureValidArgs = []string{"delete"} 30 31 // ConfigureCmd command to display clencli current version 32 func ConfigureCmd() *cobra.Command { 33 man, err := helper.GetManual("configure") 34 if err != nil { 35 fmt.Println(err) 36 os.Exit(1) 37 } 38 39 cmd := &cobra.Command{ 40 Use: man.Use, 41 Short: man.Short, 42 Long: man.Long, 43 Example: man.Example, 44 ValidArgs: configureValidArgs, 45 Args: cobra.OnlyValidArgs, 46 RunE: configureRun, 47 } 48 49 return cmd 50 } 51 52 func configureRun(cmd *cobra.Command, args []string) error { 53 if !aid.ConfigurationsDirectoryExist() { 54 if created, dir := aid.CreateConfigurationsDirectory(); created { 55 cmd.Printf("clencli configuration directory created at %s\n", dir) 56 createCredentials(cmd) 57 createConfigurations(cmd) 58 } 59 } else { 60 // configurations directory exist 61 if !aid.CredentialsFileExist() { 62 createCredentials(cmd) 63 } else { 64 updateCredentials(cmd) 65 } 66 67 if !aid.ConfigurationsFileExist() { 68 createConfigurations(cmd) 69 } else { 70 updateConfigurations(cmd) 71 } 72 } 73 74 return nil 75 } 76 77 func createCredentials(cmd *cobra.Command) { 78 answer := aid.GetUserInputAsBool(cmd, "Would you like to setup credentials?", false) 79 if answer { 80 credentials := view.CreateCredentials(cmd, profile) 81 dao.SaveCredentials(credentials) 82 } 83 } 84 85 func updateCredentials(cmd *cobra.Command) { 86 answer := aid.GetUserInputAsBool(cmd, "Would you like to update credentials?", false) 87 if answer { 88 credentials := view.UpdateCredentials(cmd, profile) 89 dao.SaveCredentials(credentials) 90 } 91 } 92 93 func createConfigurations(cmd *cobra.Command) { 94 answer := aid.GetUserInputAsBool(cmd, "Would you like to setup configurations?", false) 95 if answer { 96 configurations := view.CreateConfigurations(cmd, profile) 97 dao.SaveConfigurations(configurations) 98 } 99 } 100 101 func updateConfigurations(cmd *cobra.Command) { 102 answer := aid.GetUserInputAsBool(cmd, "Would you like to update configurations?", false) 103 if answer { 104 configurations := view.UpdateConfigurations(cmd, profile) 105 dao.SaveConfigurations(configurations) 106 } 107 }