github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/vars/contract.go (about) 1 package vars 2 3 import ( 4 "github.com/Sirupsen/logrus" 5 "github.com/daticahealth/cli/commands/services" 6 "github.com/daticahealth/cli/config" 7 "github.com/daticahealth/cli/lib/auth" 8 "github.com/daticahealth/cli/lib/prompts" 9 "github.com/daticahealth/cli/models" 10 "github.com/jault3/mow.cli" 11 ) 12 13 // Cmd is the contract between the user and the CLI. This specifies the command 14 // name, arguments, and required/optional arguments and flags for the command. 15 var Cmd = models.Command{ 16 Name: "vars", 17 ShortHelp: "Interaction with environment variables for an environment", 18 LongHelp: "The <code>vars</code> command allows you to manage environment variables for your code services. The vars command can not be run directly but has subcommands.", 19 CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) { 20 return func(cmd *cli.Cmd) { 21 cmd.CommandLong(ListSubCmd.Name, ListSubCmd.ShortHelp, ListSubCmd.LongHelp, ListSubCmd.CmdFunc(settings)) 22 cmd.CommandLong(SetSubCmd.Name, SetSubCmd.ShortHelp, SetSubCmd.LongHelp, SetSubCmd.CmdFunc(settings)) 23 cmd.CommandLong(UnsetSubCmd.Name, UnsetSubCmd.ShortHelp, UnsetSubCmd.LongHelp, UnsetSubCmd.CmdFunc(settings)) 24 } 25 }, 26 } 27 28 var ListSubCmd = models.Command{ 29 Name: "list", 30 ShortHelp: "List all environment variables", 31 LongHelp: "<code>vars list</code> prints out all known environment variables for the given code service. " + 32 "You can print out environment variables in JSON or YAML format through the <code>--json</code> or <code>--yaml</code> flags. " + 33 "Here are some sample commands\n\n" + 34 "<pre>\ndatica -E \"<your_env_name>\" vars list code-1\n" + 35 "datica -E \"<your_env_name>\" vars list code-1 --json\n</pre>", 36 CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) { 37 return func(subCmd *cli.Cmd) { 38 serviceName := subCmd.StringArg("SERVICE_NAME", "", "The name of the service containing the environment variables.") 39 json := subCmd.BoolOpt("json", false, "Output environment variables in JSON format") 40 yaml := subCmd.BoolOpt("yaml", false, "Output environment variables in YAML format") 41 subCmd.Action = func() { 42 if _, err := auth.New(settings, prompts.New()).Signin(); err != nil { 43 logrus.Fatal(err.Error()) 44 } 45 if err := config.CheckRequiredAssociation(settings); err != nil { 46 logrus.Fatal(err.Error()) 47 } 48 var formatter Formatter 49 if *json { 50 formatter = &JSONFormatter{} 51 } else if *yaml { 52 formatter = &YAMLFormatter{} 53 } else { 54 formatter = &PlainFormatter{} 55 } 56 err := CmdList(*serviceName, formatter, New(settings), services.New(settings)) 57 if err != nil { 58 logrus.Fatal(err.Error()) 59 } 60 } 61 subCmd.Spec = "SERVICE_NAME [--json | --yaml]" 62 } 63 }, 64 } 65 66 var SetSubCmd = models.Command{ 67 Name: "set", 68 ShortHelp: "Set one or more new environment variables or update the values of existing ones", 69 LongHelp: "<code>vars set</code> allows you to add new environment variables or update the value of an existing environment variable on the given code service. " + 70 "You can set/update 1 or more environment variables at a time with this command by repeating the <code>-v</code> option multiple times. " + 71 "Once new environment variables are added or values updated, a redeploy is required for the given code service to have access to the new values. " + 72 "The environment variables must be of the form <code><key>=<value></code>. Here is a sample command\n\n" + 73 "<pre>\ndatica -E \"<your_env_name>\" vars set code-1 -v AWS_ACCESS_KEY_ID=1234 -v AWS_SECRET_ACCESS_KEY=5678\n</pre>", 74 CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) { 75 return func(subCmd *cli.Cmd) { 76 serviceName := subCmd.StringArg("SERVICE_NAME", "", "The name of the service on which the environment variables will be set.") 77 variables := subCmd.Strings(cli.StringsOpt{ 78 Name: "v variable", 79 Value: []string{}, 80 Desc: "The env variable to set or update in the form \"<key>=<value>\"", 81 HideValue: true, 82 }) 83 fileName := subCmd.StringOpt("f file", "", "The path to a file to import environment variables from. This file can be in JSON, YAML, or KEY=VALUE format") 84 subCmd.Action = func() { 85 if _, err := auth.New(settings, prompts.New()).Signin(); err != nil { 86 logrus.Fatal(err.Error()) 87 } 88 if err := config.CheckRequiredAssociation(settings); err != nil { 89 logrus.Fatal(err.Error()) 90 } 91 err := CmdSet(*serviceName, *variables, *fileName, New(settings), services.New(settings)) 92 if err != nil { 93 logrus.Fatal(err.Error()) 94 } 95 } 96 subCmd.Spec = "SERVICE_NAME (-v... | -f)" 97 } 98 }, 99 } 100 101 var UnsetSubCmd = models.Command{ 102 Name: "unset", 103 ShortHelp: "Unset (delete) an existing environment variable", 104 LongHelp: "<code>vars unset</code> removes environment variables from the given code service. " + 105 "Only the environment variable name is required to unset. " + 106 "Once environment variables are unset, a redeploy is required for the given code service to realize the variable was removed. " + 107 "You can unset any number of environment variables in one command. " + 108 "Here is a sample command\n\n" + 109 "<pre>\ndatica -E \"<your_env_name>\" vars unset code-1 AWS_ACCESS_KEY_ID AWS_SECRET_ACCES_KEY_ID\n</pre>", 110 CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) { 111 return func(subCmd *cli.Cmd) { 112 serviceName := subCmd.StringArg("SERVICE_NAME", "", "The name of the service on which the environment variables will be unset.") 113 variables := subCmd.Strings(cli.StringsArg{ 114 Name: "VARIABLE", 115 Value: []string{}, 116 Desc: "The names of environment variables to unset", 117 HideValue: true, 118 }) 119 subCmd.Action = func() { 120 if _, err := auth.New(settings, prompts.New()).Signin(); err != nil { 121 logrus.Fatal(err.Error()) 122 } 123 if err := config.CheckRequiredAssociation(settings); err != nil { 124 logrus.Fatal(err.Error()) 125 } 126 err := CmdUnset(*serviceName, *variables, New(settings), services.New(settings)) 127 if err != nil { 128 logrus.Fatal(err.Error()) 129 } 130 } 131 subCmd.Spec = "SERVICE_NAME VARIABLE..." 132 } 133 }, 134 } 135 136 // IVars 137 type IVars interface { 138 List(svcID string) (map[string]string, error) 139 Set(svcID string, envVarsMap map[string]string) error 140 Unset(svcID, key string) error 141 } 142 143 // SVars is a concrete implementation of IVars 144 type SVars struct { 145 Settings *models.Settings 146 } 147 148 // New generates a new instance of IVars 149 func New(settings *models.Settings) IVars { 150 return &SVars{ 151 Settings: settings, 152 } 153 }