github.com/seilagamo/poc-lava-release@v0.3.3-rc3/cmd/lava/internal/initialize/initialize.go (about) 1 // Copyright 2023 Adevinta 2 3 // Package initialize implements the init command. 4 package initialize 5 6 import ( 7 _ "embed" 8 "errors" 9 "fmt" 10 "io/fs" 11 "os" 12 13 "github.com/seilagamo/poc-lava-release/cmd/lava/internal/base" 14 ) 15 16 // CmdInit represents the init command. 17 var CmdInit = &base.Command{ 18 UsageLine: "init [flags]", 19 Short: "init Lava project", 20 Long: ` 21 Initializes a Lava project. 22 23 This command creates a default Lava configuration file. 24 25 The -c flag allows to specify the name of the configuration file. By 26 default, a file with the name "lava.yaml" is created in the current 27 directory. 28 29 The -f flag allows to overwrite the output configuration file if it 30 exists. 31 `, 32 } 33 34 var ( 35 cfgfile = CmdInit.Flag.String("c", "lava.yaml", "config file") 36 force = CmdInit.Flag.Bool("f", false, "overwrite config file") 37 38 //go:embed default.yaml 39 defaultConfig []byte 40 ) 41 42 func init() { 43 CmdInit.Run = run // Break initialization cycle. 44 } 45 46 // run is the entry point of the init command. 47 func run(args []string) error { 48 if len(args) > 0 { 49 return errors.New("too many arguments") 50 } 51 52 if !*force { 53 _, err := os.Stat(*cfgfile) 54 if err == nil { 55 return fs.ErrExist 56 } 57 if !errors.Is(err, fs.ErrNotExist) { 58 return fmt.Errorf("stat: %w", err) 59 } 60 } 61 62 if err := os.WriteFile(*cfgfile, defaultConfig, 0644); err != nil { 63 return fmt.Errorf("write file: %w", err) 64 } 65 66 return nil 67 }