github.com/gopinath-langote/1build@v1.7.0/cmd/initialize/init.go (about) 1 package initialize 2 3 import ( 4 "fmt" 5 "github.com/gopinath-langote/1build/cmd/config" 6 "github.com/gopinath-langote/1build/cmd/utils" 7 "github.com/spf13/cobra" 8 ) 9 10 // Cmd cobra command for initializing one build configuration 11 var Cmd = &cobra.Command{ 12 Use: "init", 13 Short: "Create default project configuration", 14 Long: `Create default project configuration 15 16 - Name of the project needs be passed as parameter - 'name' 17 18 For example: 19 20 1build initialize --name project 21 1build initialize --name "My favorite project"`, 22 PreRun: func(cmd *cobra.Command, args []string) { 23 if config.IsConfigFilePresent() { 24 fmt.Println("'" + config.OneBuildConfigFileName + "' configuration file already exists.") 25 utils.ExitError() 26 } 27 }, 28 Run: func(cmd *cobra.Command, args []string) { 29 projectName, _ := cmd.Flags().GetString("name") 30 31 defaultCommand := map[string]string{} 32 defaultCommand["build"] = "echo 'Running build'" 33 34 oneBuildConfiguration := config.OneBuildConfiguration{ 35 Project: projectName, 36 Commands: []map[string]string{defaultCommand}, 37 } 38 39 err := config.WriteConfigFile(oneBuildConfiguration) 40 if err != nil { 41 fmt.Println("Failed to create file '" + config.OneBuildConfigFileName + "'") 42 } 43 }, 44 } 45 46 func init() { 47 Cmd.Flags().StringP("name", "n", "", "Project name") 48 _ = Cmd.MarkFlagRequired("name") 49 }