github.com/mweagle/Sparta@v1.15.0/aws/cloudformation/cli/describe.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/session"
    12  	"github.com/aws/aws-sdk-go/service/cloudformation"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  	validator "gopkg.in/go-playground/validator.v9"
    16  )
    17  
    18  var validate *validator.Validate
    19  
    20  /******************************************************************************/
    21  // Global options
    22  type optionsLinkStruct struct {
    23  	StackName       string `validate:"required"`
    24  	OutputDirectory string `validate:"required"`
    25  }
    26  
    27  var optionsLink optionsLinkStruct
    28  
    29  // RootCmd represents the root Cobra command invoked for the discovery
    30  // and serialization of an existing CloudFormation stack
    31  var RootCmd = &cobra.Command{
    32  	Use:   "link",
    33  	Short: "Link is a tool to discover and serialize a prexisting CloudFormation stack",
    34  	Long:  "",
    35  	PreRunE: func(cmd *cobra.Command, args []string) error {
    36  		validateErr := validate.Struct(optionsLink)
    37  		if nil != validateErr {
    38  			return validateErr
    39  		}
    40  		// Make sure the output value is a directory
    41  		osStat, osStatErr := os.Stat(optionsLink.OutputDirectory)
    42  		if nil != osStatErr {
    43  			return osStatErr
    44  		}
    45  		if !osStat.IsDir() {
    46  			return errors.Errorf("--output (%s) is not a valid directory", optionsLink.OutputDirectory)
    47  		}
    48  		return nil
    49  	},
    50  	RunE: func(cmd *cobra.Command, args []string) error {
    51  		// Get the output and stuff it to a file
    52  		sess, err := session.NewSession()
    53  		if err != nil {
    54  			return errors.Wrap(err, "Attempting to create session")
    55  		}
    56  
    57  		svc := cloudformation.New(sess)
    58  
    59  		params := &cloudformation.DescribeStacksInput{
    60  			StackName: aws.String(optionsLink.StackName),
    61  		}
    62  		describeStacksResponse, describeStacksResponseErr := svc.DescribeStacks(params)
    63  
    64  		if describeStacksResponseErr != nil {
    65  			return describeStacksResponseErr
    66  		}
    67  
    68  		stackInfo, stackInfoErr := json.Marshal(describeStacksResponse)
    69  		if stackInfoErr != nil {
    70  			return errors.Wrapf(stackInfoErr, "Failed to describe stacks")
    71  		}
    72  		outputFilepath := filepath.Join(optionsLink.OutputDirectory, fmt.Sprintf("%s.json", optionsLink.StackName))
    73  		err = ioutil.WriteFile(outputFilepath, stackInfo, 0600)
    74  		if nil != err {
    75  			return errors.Wrap(err, "Attempting to write output file")
    76  		}
    77  		fmt.Println("Created file: " + outputFilepath)
    78  		fmt.Println(describeStacksResponse)
    79  		return nil
    80  	},
    81  }
    82  
    83  func init() {
    84  	validate = validator.New()
    85  	cobra.OnInitialize()
    86  	RootCmd.PersistentFlags().StringVar(&optionsLink.StackName, "stackName", "", "CloudFormation Stack Name/ID to query")
    87  	RootCmd.PersistentFlags().StringVar(&optionsLink.OutputDirectory, "output", "", "Output directory")
    88  }
    89  
    90  func main() {
    91  	// Take a stack name and an output file...
    92  	if err := RootCmd.Execute(); err != nil {
    93  		fmt.Println(err)
    94  		os.Exit(-1)
    95  	}
    96  }