-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack-get.go
124 lines (99 loc) · 2.84 KB
/
stack-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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cmd
import (
"errors"
"fmt"
cf "github.com/aws/aws-sdk-go/service/cloudformation"
ctlaws "github.com/liangrog/cfctl/pkg/aws"
"github.com/liangrog/cfctl/pkg/conf"
"github.com/liangrog/cfctl/pkg/utils"
"github.com/liangrog/cfctl/pkg/utils/i18n"
"github.com/liangrog/cfctl/pkg/utils/templates"
"github.com/spf13/cobra"
)
var (
stackGetShort = i18n.T("Get one or more stack details")
stackGetLong = templates.LongDesc(i18n.T(`
Get all stacks details by default.
If stack names given, only return the detail for those stacks`))
stackGetExample = templates.Examples(i18n.T(`
# Get all stack details in config file backend-infra.yaml
$ cfctl stack get --file backend-infra.yaml
# Get a specific stack 'stack-a' detail
$ cfctl stack get --name stack-a
# Get multiple stack details
$ cfctl stack get --name stack-a,stack-b
# Get stack details with tag Name=frontend
$ cfctl stack get --tags Name=frontend`))
)
// Register sub commands
func init() {
cmd := getCmdStackGet()
addFlagsStackGet(cmd)
CmdStack.AddCommand(cmd)
}
func addFlagsStackGet(cmd *cobra.Command) {
cmd.Flags().String(CMD_STACK_GET_NAME, "", "get stack's details for given stack name. Multiple stack names can be given and seperated by comma, e.g 'stack-a,stack-b'")
}
// cmd: get
func getCmdStackGet() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: stackGetShort,
Long: stackGetLong,
Example: fmt.Sprintf(stackGetExample),
RunE: func(cmd *cobra.Command, args []string) error {
err := stackGet(
cmd.Flags().Lookup(CMD_STACK_DEPLOY_FILE).Value.String(),
cmd.Flags().Lookup(CMD_ROOT_OUTPUT).Value.String(),
cmd.Flags().Lookup(CMD_STACK_GET_NAME).Value.String(),
cmd.Flags().Lookup(CMD_STACK_DEPLOY_TAGS).Value.String(),
)
silenceUsageOnError(cmd, err)
return err
},
}
return cmd
}
// Get stacks
func stackGet(f, format, stackNames, tags string) error {
stack := ctlaws.NewStack(cf.New(ctlaws.AWSSess))
// Load deploy configuration file.
dc, err := conf.NewDeployConfig(f)
if err != nil {
return err
}
// Retrieve the list of stacks and apply filters.
filters := make(map[string]string)
if len(stackNames) > 0 {
filters["name"] = stackNames
}
if len(tags) > 0 {
filters["tag"] = tags
}
sl := dc.GetStackList(filters)
if len(sl) == 0 {
return errors.New("No stack found.")
}
// If stack name given
var errMsg []string
for k, _ := range sl {
if !stack.Exist(k) {
errMsg = append(errMsg, utils.MsgFormat(fmt.Sprintf("Failed to find stack %s\n", k), utils.MessageTypeError))
continue
}
if out, err := stack.DescribeStack(k); err != nil {
return err
} else {
if err := utils.Print(utils.FormatType(format), out); err != nil {
return err
}
}
}
// Print out error message
if len(errMsg) > 0 {
for _, msg := range errMsg {
utils.StdoutWarn(msg)
}
}
return nil
}