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