github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/init.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "fmt" 8 9 "github.com/juju/cmd" 10 "launchpad.net/gnuflag" 11 12 "github.com/juju/juju/environs" 13 ) 14 15 // InitCommand is used to write out a boilerplate environments.yaml file. 16 type InitCommand struct { 17 cmd.CommandBase 18 WriteFile bool 19 Show bool 20 } 21 22 func (c *InitCommand) Info() *cmd.Info { 23 return &cmd.Info{ 24 Name: "init", 25 Purpose: "generate boilerplate configuration for juju environments", 26 Aliases: []string{"generate-config"}, 27 } 28 } 29 30 func (c *InitCommand) SetFlags(f *gnuflag.FlagSet) { 31 f.BoolVar(&c.WriteFile, "f", false, "force overwriting environments.yaml file even if it exists (ignored if --show flag specified)") 32 f.BoolVar(&c.Show, "show", false, "print the generated configuration data to stdout instead of writing it to a file") 33 } 34 35 var errJujuEnvExists = fmt.Errorf(`A juju environment configuration already exists. 36 37 Use -f to overwrite the existing environments.yaml. 38 `) 39 40 // Run checks to see if there is already an environments.yaml file. In one does not exist already, 41 // a boilerplate version is created so that the user can edit it to get started. 42 func (c *InitCommand) Run(context *cmd.Context) error { 43 out := context.Stdout 44 config := environs.BoilerplateConfig() 45 if c.Show { 46 fmt.Fprint(out, config) 47 return nil 48 } 49 _, err := environs.ReadEnvirons("") 50 if err == nil && !c.WriteFile { 51 return errJujuEnvExists 52 } 53 if err != nil && !environs.IsNoEnv(err) { 54 return err 55 } 56 filename, err := environs.WriteEnvirons("", config) 57 if err != nil { 58 return fmt.Errorf("A boilerplate environment configuration file could not be created: %s", err.Error()) 59 } 60 fmt.Fprintf(out, "A boilerplate environment configuration file has been written to %s.\n", filename) 61 fmt.Fprint(out, "Edit the file to configure your juju environment and run bootstrap.\n") 62 return nil 63 }