Open3

go-to-k/delstack のソースコード見て実践的な Go の書き方を勉強する

hassaku63hassaku63

help

$ ./delstack -h
NAME:
   delstack - A CLI tool to force delete the entire CloudFormation stack.

USAGE:
   delstack [global options] [arguments...]

GLOBAL OPTIONS:
   --stackName value, -s value  CloudFormation stack name
   --profile value, -p value    AWS profile name
   --region value, -r value     AWS region
   --interactive, -i            Interactive Mode (default: false)
   --help, -h                   show help

tree

$ tree -d
.
├── cmd
│   └── delstack
├── internal
│   ├── app
│   ├── io
│   ├── operation
│   ├── resourcetype
│   └── version
├── pkg
│   └── client
└── testdata
    └── yamldir

12 directories
hassaku63hassaku63

CLI コマンド作れる urfave/cliv2 を見てみると cli.App の Action が諸々パースされてから実際に動作するロジックらしい

CLI のロジック部分である func (a *App) getAction() func(c *cli.Context) errorGitHub Copilot Labs に説明させてみた(それほど量多くないけど)

Here is the explanation for the code above:

  1. First, we check if the interactive mode is enabled or not.
  2. If the interactive mode is enabled, we call the doInteractiveMode() method to get the target resource types and the keyword to filter the stack names.
  3. If the interactive mode is disabled, we get all the resource types without filtering.
  4. If the interactive mode is enabled and the stack name is not specified, we call the ListStacksFilteredByKeyword() method to get the stack names. Then, we call the selectStackName() method to select one of the stack names.
  5. If the interactive mode is enabled and the stack name is specified, we use the stack name specified by the command option.
  6. We create the CloudFormationStackOperator instance with the target resource types.
  7. If the interactive mode is enabled and the stack name is not specified, we create the OperatorCollection instance with the target resource types. Otherwise, we create the OperatorCollection instance with all the resource types.
  8. We create the OperatorManager instance with the OperatorCollection instance.
  9. We create the CloudFormationStackOperator instance with the OperatorFactory instance.
  10. We call the DeleteCloudFormationStack() method to delete the stack.