github.com/Azure/draft-classic@v0.16.0/cmd/draft/config.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 8 "github.com/Azure/draft/pkg/draft/draftpath" 9 "github.com/BurntSushi/toml" 10 "github.com/spf13/cobra" 11 ) 12 13 const ( 14 configHelp = `Manage global Draft configuration stored in $DRAFT_HOME/config.toml.` 15 ) 16 17 type configKey struct { 18 name string 19 description string 20 } 21 22 var ( 23 registry = configKey{name: "registry", description: "Registry to push built containers to (e.g. docker.io/foo, foo.azurecr.io)"} 24 containerBuilder = configKey{name: "container-builder", description: "How to build the container (supported values: docker, acrbuild)"} 25 resourceGroupName = configKey{name: "resource-group-name", description: "The Azure resource group of the container registry (for Azure registries only)"} 26 disablePushWarning = configKey{name: "disable-push-warning", description: "Suppresses warning if no registry set"} 27 configKeys = []configKey{registry, containerBuilder, resourceGroupName, disablePushWarning} 28 ) 29 30 // DraftConfig is the configuration stored in $DRAFT_HOME/config.toml 31 type DraftConfig map[string]string 32 33 // ReadConfig reads in global configuration from $DRAFT_HOME/config.toml 34 func ReadConfig() (DraftConfig, error) { 35 var data DraftConfig 36 h := draftpath.Home(draftHome) 37 f, err := os.Open(h.Config()) 38 if err != nil { 39 if os.IsNotExist(err) { 40 return nil, nil 41 } 42 return nil, fmt.Errorf("Could not open file %s: %s", h.Config(), err) 43 } 44 defer f.Close() 45 if _, err := toml.DecodeReader(f, &data); err != nil { 46 return nil, fmt.Errorf("Could not decode config %s: %s", h.Config(), err) 47 } 48 return data, nil 49 } 50 51 // SaveConfig saves global configuration to $DRAFT_HOME/config.toml 52 func SaveConfig(data DraftConfig) error { 53 h := draftpath.Home(draftHome) 54 f, err := os.OpenFile(h.Config(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) 55 if err != nil { 56 return fmt.Errorf("Could not open file %s: %s", h.Config(), err) 57 } 58 defer f.Close() 59 return toml.NewEncoder(f).Encode(data) 60 } 61 62 func newConfigCmd(out io.Writer) *cobra.Command { 63 cmd := &cobra.Command{ 64 Use: "config", 65 Short: "manage Draft configuration", 66 Long: configHelp, 67 } 68 cmd.AddCommand( 69 newConfigListCmd(out), 70 newConfigGetCmd(out), 71 newConfigSetCmd(out), 72 newConfigUnsetCmd(out), 73 ) 74 return cmd 75 }