forked from awslabs/fargatecli
/
service_env_unset.go
65 lines (50 loc) · 1.71 KB
/
service_env_unset.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
64
65
package cmd
import (
"strings"
"github.com/jpignata/fargate/console"
ECS "github.com/jpignata/fargate/ecs"
"github.com/jpignata/fargate/util"
"github.com/spf13/cobra"
)
type ServiceEnvUnsetOperation struct {
ServiceName string
Keys []string
}
func (o *ServiceEnvUnsetOperation) Validate() {
if len(o.Keys) == 0 {
console.IssueExit("No keys specified")
}
}
func (o *ServiceEnvUnsetOperation) SetKeys(keys []string) {
o.Keys = util.Map(keys, strings.ToUpper)
}
var serviceEnvUnsetCmd = &cobra.Command{
Use: "unset --key <key-name> [--key <key-name>] ...",
Short: "Unset environment variables",
Long: `Unset environment variables
Unsets the environment variable specified via the --key flag. Specify --key with
a key name multiple times to unset multiple variables.`,
Run: func(cmd *cobra.Command, args []string) {
operation := &ServiceEnvUnsetOperation{
ServiceName: args[0],
}
operation.SetKeys(flagServiceEnvUnsetKeys)
operation.Validate()
serviceEnvUnset(operation)
},
}
var flagServiceEnvUnsetKeys []string
func init() {
serviceEnvUnsetCmd.Flags().StringSliceVarP(&flagServiceEnvUnsetKeys, "key", "k", []string{}, "Environment variable keys to unset [e.g. KEY, NGINX_PORT]")
serviceEnvCmd.AddCommand(serviceEnvUnsetCmd)
}
func serviceEnvUnset(operation *ServiceEnvUnsetOperation) {
ecs := ECS.New(sess, clusterName)
service := ecs.DescribeService(operation.ServiceName)
taskDefinitionArn := ecs.RemoveEnvVarsFromTaskDefinition(service.TaskDefinitionArn, operation.Keys)
ecs.UpdateServiceTaskDefinition(operation.ServiceName, taskDefinitionArn)
console.Info("Unset %s environment variables:", operation.ServiceName)
for _, key := range operation.Keys {
console.Info("- %s", key)
}
}