-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_get.go
86 lines (73 loc) · 1.59 KB
/
cmd_get.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2021 Ronny Bangsund
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package main
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/grimdork/opt"
)
// GetCmd options.
type GetCmd struct {
opt.DefaultHelp
Key string `placeholder:"KEY" help:"Key to fetch."`
Tags bool `short:"t" help:"Get tags too."`
}
func (cmd *GetCmd) Run(in []string) error {
if cmd.Help || cmd.Key == "" {
return opt.ErrUsage
}
client, err := getClient()
if err != nil {
return nil
}
param, err := client.GetParameter(context.Background(), &ssm.GetParameterInput{
Name: aws.String(validKey(cmd.Key)),
WithDecryption: true,
})
if err != nil {
return err
}
switch param.Parameter.Type {
case "StringList":
a := strings.Split(*param.Parameter.Value, ",")
if len(a) < 2 {
pr("%s", *param.Parameter.Value)
return nil
}
nl := false
for _, x := range a {
fmt.Printf("%s", x)
if nl {
pr("")
} else {
fmt.Print(" = ")
}
nl = !nl
}
default:
pr("%s", *param.Parameter.Value)
}
if cmd.Tags {
tags, err := client.ListTagsForResource(context.Background(), &ssm.ListTagsForResourceInput{
ResourceId: param.Parameter.Name,
ResourceType: types.ResourceTypeForTaggingParameter,
})
if err != nil {
return err
}
if len(tags.TagList) == 0 {
return nil
}
pr("Tags:")
for _, t := range tags.TagList {
pr("\t%s = %s", *t.Key, *t.Value)
}
}
return nil
}