github.com/kolanos/fargate@v0.2.3/cmd/service_env_set.go (about) 1 package cmd 2 3 import ( 4 "github.com/jpignata/fargate/console" 5 ECS "github.com/jpignata/fargate/ecs" 6 "github.com/spf13/cobra" 7 ) 8 9 type ServiceEnvSetOperation struct { 10 ServiceName string 11 EnvVars []ECS.EnvVar 12 } 13 14 func (o *ServiceEnvSetOperation) Validate() { 15 if len(o.EnvVars) == 0 { 16 console.IssueExit("No environment variables specified") 17 } 18 } 19 20 func (o *ServiceEnvSetOperation) SetEnvVars(inputEnvVars []string) { 21 o.EnvVars = extractEnvVars(inputEnvVars) 22 } 23 24 var flagServiceEnvSetEnvVars []string 25 26 var serviceEnvSetCmd = &cobra.Command{ 27 Use: "set --env <key=value> [--env <key=value] ...", 28 Short: "Set environment variables", 29 Long: `Set environment variables 30 31 At least one environment variable must be specified via the --env flag. Specify 32 --env with a key=value parameter multiple times to add multiple variables.`, 33 Run: func(cmd *cobra.Command, args []string) { 34 operation := &ServiceEnvSetOperation{ 35 ServiceName: args[0], 36 } 37 38 operation.SetEnvVars(flagServiceEnvSetEnvVars) 39 operation.Validate() 40 serviceEnvSet(operation) 41 }, 42 } 43 44 func init() { 45 serviceEnvSetCmd.Flags().StringSliceVarP(&flagServiceEnvSetEnvVars, "env", "e", []string{}, "Environment variables to set [e.g. KEY=value]") 46 47 serviceEnvCmd.AddCommand(serviceEnvSetCmd) 48 } 49 50 func serviceEnvSet(operation *ServiceEnvSetOperation) { 51 ecs := ECS.New(sess, clusterName) 52 service := ecs.DescribeService(operation.ServiceName) 53 taskDefinitionArn := ecs.AddEnvVarsToTaskDefinition(service.TaskDefinitionArn, operation.EnvVars) 54 55 ecs.UpdateServiceTaskDefinition(operation.ServiceName, taskDefinitionArn) 56 57 console.Info("Set %s environment variables:", operation.ServiceName) 58 59 for _, envVar := range operation.EnvVars { 60 console.Info("- %s=%s", envVar.Key, envVar.Value) 61 } 62 63 }