github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/aid/init.go (about) 1 package aid 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/awslabs/clencli/cobra/model" 9 "github.com/awslabs/clencli/helper" 10 "github.com/sirupsen/logrus" 11 "github.com/spf13/cobra" 12 ) 13 14 /* BASIC PROJECT */ 15 16 // CreateBasicProject creates a basic project 17 func CreateBasicProject(cmd *cobra.Command, name string) error { 18 err := createAndEnterProjectDir(name) 19 if err != nil { 20 return err 21 } 22 23 if initalized := initProject(); !initalized { 24 logrus.Errorf("unable to initialize basic project") 25 return fmt.Errorf("unable to initalize project %s", name) 26 } 27 28 return nil 29 } 30 31 func createAndEnterProjectDir(name string) error { 32 33 if !helper.MkDirsIfNotExist(name) { 34 return fmt.Errorf("unable to create directory %s", name) 35 } 36 37 err := os.Chdir(name) 38 if err != nil { 39 return fmt.Errorf("unable to enter directory %s", name) 40 } 41 42 wd, err := os.Getwd() 43 if err != nil { 44 return fmt.Errorf("unable to returns a rooted path name corresponding to the current directory:\n%v", err) 45 } 46 logrus.Infof("current working directory changed to %s", wd) 47 48 return nil 49 } 50 51 // create the basic configuration files 52 func initProject() bool { 53 54 // Create a directory for clencli 55 a := helper.MkDirsIfNotExist("clencli") 56 b := helper.WriteFileFromBox("/init/clencli/readme.yaml", "clencli/readme.yaml") 57 c := helper.WriteFileFromBox("/init/clencli/readme.tmpl", "clencli/readme.tmpl") 58 d := helper.WriteFileFromBox("/init/.gitignore", ".gitignore") 59 60 return (a && b && c && d) 61 } 62 63 // InitCustomized TODO... 64 func InitCustomized(profile string, config model.Configurations) bool { 65 66 for _, p := range config.Profiles { 67 if p.Name == profile { 68 for _, c := range p.Configurations { 69 for _, f := range c.Initialization.Files { 70 if f.State == "directory" { 71 if !helper.MkDirsIfNotExist(f.Path) { 72 logrus.Errorf("unable to create directory based on configuration") 73 return false 74 } 75 } else if f.State == "file" { 76 if strings.Contains(f.Src, "http") { 77 if err := helper.DownloadFileTo(f.Src, f.Dest); err != nil { 78 logrus.Errorf("unable to download file based on configuration") 79 return false 80 } 81 } else { 82 if err := helper.CopyFileTo(f.Src, f.Dest); err != nil { 83 logrus.Errorf("unable to copy file based on configuration") 84 return false 85 } 86 } 87 } 88 } 89 } 90 } 91 } 92 93 return true 94 } 95 96 /* CLOUD PROJECT */ 97 98 // CreateCloudProject copies the necessary templates for cloud projects 99 func CreateCloudProject(cmd *cobra.Command, name string) error { 100 if err := CreateBasicProject(cmd, name); err != nil { 101 return nil 102 } 103 104 if initialized := initCloudProject(); !initialized { 105 logrus.Errorf("unable to initialize cloud project") 106 return fmt.Errorf("unable to initalize project %s", name) 107 } 108 109 return nil 110 } 111 112 // copies the High Level Design template file 113 func initCloudProject() bool { 114 a := helper.WriteFileFromBox("/init/clencli/hld.yaml", "clencli/hld.yaml") 115 b := helper.WriteFileFromBox("/init/clencli/hld.tmpl", "clencli/hld.tmpl") 116 117 return (a && b) 118 } 119 120 /* CLOUDFORMATION PROJECT */ 121 122 // CreateCloudFormationProject creates an AWS CloudFormation project 123 func CreateCloudFormationProject(cmd *cobra.Command, name string) error { 124 if err := CreateBasicProject(cmd, name); err != nil { 125 return nil 126 } 127 128 if initialized := initCloudProject(); !initialized { 129 logrus.Errorf("unable to initialize cloud project") 130 return fmt.Errorf("unable to initalize project %s", name) 131 } 132 133 if initialized := initCloudFormationProject(); !initialized { 134 logrus.Errorf("unable to initialize cloudformation project") 135 return fmt.Errorf("unable to initalize project %s", name) 136 } 137 138 return nil 139 } 140 141 // initialize a project with CloudFormation structure and copies template files 142 func initCloudFormationProject() bool { 143 144 a := helper.MkDirsIfNotExist("environments") 145 b := helper.MkDirsIfNotExist("environments/dev") 146 c := helper.MkDirsIfNotExist("environments/prod") 147 d := helper.WriteFileFromBox("/init/project/type/clouformation/skeleton.yaml", "skeleton.yaml") 148 e := helper.WriteFileFromBox("/init/project/type/clouformation/skeleton.json", "skeleton.json") 149 150 /* TODO: copy a template to create standard tags for the entire stack easily 151 https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html 152 example aws cloudformation create-stack ... --tags */ 153 154 /* TODO: copy Makefile */ 155 /* TODO: copy LICENSE */ 156 157 return (a && b && c && d && e) 158 } 159 160 /* TERRAFORM PROJECT */ 161 162 // CreateTerraformProject creates a HashiCorp Terraform project 163 func CreateTerraformProject(cmd *cobra.Command, name string) error { 164 if err := CreateBasicProject(cmd, name); err != nil { 165 return nil 166 } 167 168 if initialized := initCloudProject(); !initialized { 169 logrus.Errorf("unable to initialize terraform project") 170 return fmt.Errorf("unable to initalize project %s", name) 171 } 172 173 if initialized := initTerraformProject(); !initialized { 174 logrus.Errorf("unable to initialize cloud project") 175 return fmt.Errorf("unable to initalize project %s", name) 176 } 177 178 return nil 179 } 180 181 // InitTerraform initialize a project with Terraform structure 182 func initTerraformProject() bool { 183 a := helper.WriteFileFromBox("/init/project/type/terraform/Makefile", "Makefile") 184 b := helper.WriteFileFromBox("/init/project/type/terraform/LICENSE", "LICENSE") 185 186 c := helper.MkDirsIfNotExist("environments") 187 d := helper.WriteFileFromBox("/init/project/type/terraform/environments/dev.tf", "environments/dev.tf") 188 e := helper.WriteFileFromBox("/init/project/type/terraform/environments/prod.tf", "environments/prod.tf") 189 190 f := helper.WriteFileFromBox("/init/project/type/terraform/main.tf", "main.tf") 191 g := helper.WriteFileFromBox("/init/project/type/terraform/variables.tf", "variables.tf") 192 h := helper.WriteFileFromBox("/init/project/type/terraform/outputs.tf", "outputs.tf") 193 194 return (a && b && c && d && e && f && g && h) 195 196 } 197 198 // TODO: allow users to inform additional files to be added to their project initialization