ls

package
v1.8.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Example (Ls_help)
package main

import (
	"os"

	"github.com/aws-cloudformation/rain/internal/cmd/ls"
)

func main() {
	os.Args = []string{
		os.Args[0],
		"--help",
	}

	ls.Cmd.Execute()
}
Output:

Displays a list of all running stacks or the contents of <stack> if provided. If the -c arg is supplied, operates on changesets instead of stacks

Usage:
  ls <stack> [changeset]

Aliases:
  ls, list

Flags:
  -a, --all         list stacks in all regions; if you specify a stack, show more details
  -c, --changeset   List changesets instead of stacks
  -h, --help        help for ls

Index

Examples

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:                   "ls <stack> [changeset]",
	Short:                 "List running CloudFormation stacks or changesets",
	Long:                  "Displays a list of all running stacks or the contents of <stack> if provided. If the -c arg is supplied, operates on changesets instead of stacks",
	Args:                  cobra.MaximumNArgs(2),
	Aliases:               []string{"list"},
	DisableFlagsInUseLine: true,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) > 0 {

			if changeset {

				if len(args) != 2 {
					panic("Usage: rain ls -c stackName changeSetName")
				}

				stackName := args[0]
				changeSetName := args[1]
				spinner.Push("Fetching changeset details")
				cs, err := cfn.GetChangeSet(stackName, changeSetName)
				if err != nil {
					panic(ui.Errorf(err, "failed to get changeset '%s'", changeSetName))
				}
				out := ""
				out += fmt.Sprintf("Arn: %v\n", *cs.ChangeSetId)
				out += fmt.Sprintf("Created: %v\n", cs.CreationTime)
				descr := ""
				if cs.Description != nil {
					descr = *cs.Description
				}
				out += fmt.Sprintf("Description: %v\n", descr)
				reason := ""
				if cs.StatusReason != nil {
					reason = "(" + *cs.StatusReason + ")"
				}
				out += fmt.Sprintf("Status: %v/%v %v\n",
					ui.ColouriseStatus(string(cs.ExecutionStatus)),
					ui.ColouriseStatus(string(cs.Status)),
					reason)
				out += "Parameters: "
				if len(cs.Parameters) == 0 {
					out += "(None)\n"
				} else {
					out += "\n"
				}
				for _, p := range cs.Parameters {
					k, v := "", ""
					if p.ParameterKey != nil {
						k = *p.ParameterKey
					}
					if p.ParameterValue != nil {
						v = *p.ParameterValue
					}
					out += fmt.Sprintf("  %s: %s\n", k, v)
				}

				out += "Changes: \n"
				for _, csch := range cs.Changes {
					if csch.ResourceChange == nil {
						continue
					}
					change := csch.ResourceChange
					rid := ""
					if change.LogicalResourceId != nil {
						rid = *change.LogicalResourceId
					}
					rt := ""
					if change.ResourceType != nil {
						rt = *change.ResourceType
					}
					pid := ""
					if change.PhysicalResourceId != nil {
						pid = *change.PhysicalResourceId
					}
					replace := ""
					switch string(change.Replacement) {
					case "True":
						replace = " [Replace]"
					case "Conditional":
						replace = " [Might replace]"
					}
					out += fmt.Sprintf("  %s%s: %s (%s) %s\n",
						string(change.Action),
						replace,
						rid,
						rt,
						pid)

				}

				spinner.Pop()

				fmt.Println(out)

			} else {

				stackName := args[0]
				spinner.Push("Fetching stack status")
				stack, err := cfn.GetStack(stackName)
				if err != nil {
					panic(ui.Errorf(err, "failed to list stack '%s'", stackName))
				}

				output := cfn.GetStackSummary(stack, all)
				spinner.Pop()

				fmt.Println(output)
				fmt.Println(console.Yellow("  ChangeSets:"))
				err = ShowChangeSetsForStack(*stack.StackName)
				if err != nil {
					panic(err)
				}
			}
		} else {

			var err error
			regions := []string{aws.Config().Region}

			if all {
				spinner.Push("Fetching region list")
				regions, err = ec2.GetRegions()
				if err != nil {
					panic(ui.Errorf(err, "unable to get region list"))
				}
				spinner.Pop()
			}

			origRegion := aws.Config().Region

			for _, region := range regions {

				spinner.Push(fmt.Sprintf("Fetching stacks in %s", region))
				aws.SetRegion(region)
				stacks, err := cfn.ListStacks()
				if err != nil {
					panic(ui.Errorf(err, "failed to list stacks"))
				}
				spinner.Pop()

				if len(stacks) == 0 && all {
					continue
				}

				stackNames := make(sort.StringSlice, 0)

				if changeset {
					fmt.Println(console.Yellow(fmt.Sprintf("Stacks with changesets in %s:", region)))
					for _, stack := range stacks {
						if stack.StackName == nil {
							continue
						}
						config.Debugf("Checking stack %s", *stack.StackName)

						err := ShowChangeSetsForStack(*stack.StackName)
						if err != nil {
							panic(err)
						}

					}

				} else {

					stackMap := make(map[string]types.StackSummary)
					for _, stack := range stacks {
						stackNames = append(stackNames, *stack.StackName)
						stackMap[*stack.StackName] = stack
					}
					sort.Strings(stackNames)

					fmt.Println(console.Yellow(fmt.Sprintf("CloudFormation stacks in %s:", region)))
					for _, stackName := range stackNames {
						stack := stackMap[stackName]

						if stack.ParentId == nil {
							fmt.Println(ui.Indent("  ", formatStack(stack, stackMap)))
						}
					}
				}
			}

			aws.SetRegion(origRegion)
		}

		all = false
	},
}

Cmd is the ls command's entrypoint

Functions

func ShowChangeSetsForStack added in v1.8.0

func ShowChangeSetsForStack(stackName string) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL