github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/controller/init.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 "errors" 20 "fmt" 21 "os" 22 23 "github.com/awslabs/clencli/cobra/aid" 24 "github.com/awslabs/clencli/cobra/dao" 25 "github.com/awslabs/clencli/helper" 26 "github.com/sirupsen/logrus" 27 "github.com/spf13/cobra" 28 ) 29 30 var initValidArgs = []string{"project"} 31 var initValidProjectTypes = []string{"basic", "cloud", "cloudformation", "terraform"} 32 33 // InitCmd command to initialize projects 34 func InitCmd() *cobra.Command { 35 man, err := helper.GetManual("init") 36 if err != nil { 37 fmt.Println(err) 38 os.Exit(1) 39 } 40 41 cmd := &cobra.Command{ 42 Use: man.Use, 43 Short: man.Short, 44 Long: man.Long, 45 Example: man.Example, 46 ValidArgs: initValidArgs, 47 Args: cobra.OnlyValidArgs, 48 PreRunE: initPreRun, 49 RunE: initRun, 50 } 51 52 cmd.Flags().String("project-name", "", "The project name.") 53 cmd.Flags().String("project-type", "basic", "The project type.") 54 cmd.MarkFlagRequired("name") 55 56 return cmd 57 } 58 59 func initPreRun(cmd *cobra.Command, args []string) error { 60 logrus.Traceln("start: command init pre-run") 61 62 if err := helper.ValidateCmdArgs(cmd, args, "init"); err != nil { 63 return err 64 } 65 66 if err := helper.ValidateCmdArgAndFlag(cmd, args, "init", "project", "project-name"); err != nil { 67 return err 68 } 69 70 if err := helper.ValidateCmdArgAndFlag(cmd, args, "init", "project", "project-type"); err != nil { 71 return err 72 } 73 74 logrus.Traceln("end: command init pre-run") 75 return nil 76 } 77 78 func initRun(cmd *cobra.Command, args []string) error { 79 logrus.Traceln("start: command init run") 80 81 cmd.SilenceUsage = true 82 83 if args[0] == "project" { 84 85 pName, err := cmd.Flags().GetString("project-name") 86 if err != nil { 87 logrus.Errorf("unable to access --project-name\n%v", err) 88 return fmt.Errorf("unable to access --project-name\n%v", err) 89 } 90 91 pType, err := cmd.Flags().GetString("project-type") 92 if err != nil { 93 logrus.Errorf("unable to access --project-type\n%v", err) 94 return fmt.Errorf("unable to access --project-type\n%v", err) 95 } 96 97 switch pType { 98 case "basic": 99 err = aid.CreateBasicProject(cmd, pName) 100 case "cloud": 101 err = aid.CreateCloudProject(cmd, pName) 102 case "cloudformation": 103 err = aid.CreateCloudFormationProject(cmd, pName) 104 case "terraform": 105 err = aid.CreateTerraformProject(cmd, pName) 106 default: 107 return errors.New("unknow project type") 108 } 109 110 if aid.ConfigurationsDirectoryExist() && aid.ConfigurationsFileExist() { 111 config, err := dao.GetConfigurations() 112 if err != nil { 113 logrus.Errorf("unable to get configuration during initialization\n%v", err) 114 return fmt.Errorf("unable to initialize project based on configurations\n%v", err) 115 } 116 117 created := aid.InitCustomized(profile, config) 118 if !created { 119 return fmt.Errorf("unable to initialize project based on configurations\n%s", err) 120 } 121 122 } 123 124 if err != nil { 125 return fmt.Errorf("unable to initialize project sucessfully \n%s", err) 126 } 127 128 cmd.Printf("%s was successfully initialized as a %s project\n", pName, pType) 129 130 } 131 132 logrus.Traceln("end: command init run") 133 return nil 134 }