/
describe.go
96 lines (84 loc) · 2.73 KB
/
describe.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/pkg/errors"
"github.com/spf13/cobra"
validator "gopkg.in/go-playground/validator.v9"
)
var validate *validator.Validate
/******************************************************************************/
// Global options
type optionsLinkStruct struct {
StackName string `validate:"required"`
OutputDirectory string `validate:"required"`
}
var optionsLink optionsLinkStruct
// RootCmd represents the root Cobra command invoked for the discovery
// and serialization of an existing CloudFormation stack
var RootCmd = &cobra.Command{
Use: "link",
Short: "Link is a tool to discover and serialize a prexisting CloudFormation stack",
Long: "",
PreRunE: func(cmd *cobra.Command, args []string) error {
validateErr := validate.Struct(optionsLink)
if nil != validateErr {
return validateErr
}
// Make sure the output value is a directory
osStat, osStatErr := os.Stat(optionsLink.OutputDirectory)
if nil != osStatErr {
return osStatErr
}
if !osStat.IsDir() {
return errors.Errorf("--output (%s) is not a valid directory", optionsLink.OutputDirectory)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// Get the output and stuff it to a file
sess, err := session.NewSession()
if err != nil {
return errors.Wrap(err, "Attempting to create session")
}
svc := cloudformation.New(sess)
params := &cloudformation.DescribeStacksInput{
StackName: aws.String(optionsLink.StackName),
}
describeStacksResponse, describeStacksResponseErr := svc.DescribeStacks(params)
if describeStacksResponseErr != nil {
return describeStacksResponseErr
}
stackInfo, stackInfoErr := json.Marshal(describeStacksResponse)
if stackInfoErr != nil {
return errors.Wrapf(stackInfoErr, "Failed to describe stacks")
}
outputFilepath := filepath.Join(optionsLink.OutputDirectory, fmt.Sprintf("%s.json", optionsLink.StackName))
err = ioutil.WriteFile(outputFilepath, stackInfo, 0600)
if nil != err {
return errors.Wrap(err, "Attempting to write output file")
}
fmt.Println("Created file: " + outputFilepath)
fmt.Println(describeStacksResponse)
return nil
},
}
func init() {
validate = validator.New()
cobra.OnInitialize()
RootCmd.PersistentFlags().StringVar(&optionsLink.StackName, "stackName", "", "CloudFormation Stack Name/ID to query")
RootCmd.PersistentFlags().StringVar(&optionsLink.OutputDirectory, "output", "", "Output directory")
}
func main() {
// Take a stack name and an output file...
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}