Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON format to list command #2073

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:

- Fix `terramate cloud login --github` when the Github email is not verified but the associated Firebase account is verified.

### Added

- Add `--format` option to `terramate list --run-order` command. This option allows to format the output of the command as either `json` or `text` (current default). The json format is useful for automation and scripting as it groups the stacks by run order level.

## v0.12.0

### Added
Expand Down
56 changes: 53 additions & 3 deletions cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type cliSpec struct {
cloudFilterFlags
Target string `help:"Select the deployment target of the filtered stacks."`
RunOrder bool `default:"false" help:"Sort listed stacks by order of execution"`
Format string `default:"text" help:"Format the output of the list command" enum:"text,json"`

changeDetectionFlags
} `cmd:"" help:"List stacks."`
Expand Down Expand Up @@ -746,6 +747,7 @@ func (c *cli) run() {
tel.StringFlag("filter-deployment-status", c.parsedArgs.List.DeploymentStatus),
tel.StringFlag("filter-target", c.parsedArgs.List.Target),
tel.BoolFlag("run-order", c.parsedArgs.List.RunOrder),
tel.StringFlag("format", c.parsedArgs.List.Format),
)
c.setupGit()
c.setupChangeDetection(c.parsedArgs.List.EnableChangeDetection, c.parsedArgs.List.DisableChangeDetection)
Expand Down Expand Up @@ -2005,10 +2007,10 @@ func (c *cli) printStacks() {
fatal(err)
}

c.printStacksList(report.Stacks, c.parsedArgs.List.Why, c.parsedArgs.List.RunOrder)
c.printStacksList(report.Stacks, c.parsedArgs.List.Why, c.parsedArgs.List.RunOrder, c.parsedArgs.List.Format)
}

func (c *cli) printStacksList(allStacks []stack.Entry, why bool, runOrder bool) {
func (c *cli) printStacksList(allStacks []stack.Entry, why bool, runOrder bool, format string) {
filteredStacks := c.filterStacks(allStacks)

reasons := map[string]string{}
Expand All @@ -2026,10 +2028,58 @@ func (c *cli) printStacksList(allStacks []stack.Entry, why bool, runOrder bool)
if err != nil {
fatalWithDetailf(errors.E(err, failReason), "Invalid stack configuration")
}

if format == "json" {
graph, reason, err := run.BuildDAGFromStacks(c.cfg(), stacks,
func(s *config.SortableStack) *config.Stack { return s.Stack })
if err != nil {
fatalWithDetailf(errors.E(err, reason), "Failed to build dependency graph")
}

// Calculate levels based on DAG
levelGroups := make(map[int][]string)
stackLevels := make(map[string]int)

// Process nodes in topological order
order := graph.Order()
for _, id := range order {
stack, err := graph.Node(id)
if err != nil {
fatalWithDetailf(err, "Failed to access node in graph")
}

level := 1 // Default level
// Check ancestors (dependencies)
for _, ancestorID := range graph.AncestorsOf(id) {
ancestorLevel := stackLevels[string(ancestorID)]
if ancestorLevel >= level {
level = ancestorLevel + 1
}
}

dir := stack.Stack.Dir.String()
stackLevels[dir] = level

friendlyDir, ok := c.friendlyFmtDir(dir)
if !ok {
printer.Stderr.Error(stdfmt.Sprintf("Unable to format stack dir %s", dir))
continue
}
levelGroups[level] = append(levelGroups[level], friendlyDir)
}

jsonOutput, err := stdjson.MarshalIndent(levelGroups, "", " ")
if err != nil {
fatalWithDetailf(err, "Error formatting stack groups as JSON")
}
printer.Stdout.Println(string(jsonOutput))
return
}
}

// Original output format for non-runOrder case or when format is not json
for _, s := range stacks {
dir := s.Dir().String()
dir := s.Stack.Dir.String()
friendlyDir, ok := c.friendlyFmtDir(dir)
if !ok {
printer.Stderr.Error(stdfmt.Sprintf("Unable to format stack dir %s", dir))
Expand Down