Skip to content

Add JSON format to list command #2073

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

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 @@ -39,6 +39,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
50 changes: 50 additions & 0 deletions commands/stack/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package list

import (
"context"
"encoding/json"
"fmt"

"github.com/terramate-io/terramate/cloud/api/status"
Expand All @@ -28,6 +29,7 @@ type Spec struct {
Tags []string
NoTags []string
Printers printer.Printers
Format string
}

// StatusFilters contains the status filters for the list command.
Expand Down Expand Up @@ -96,8 +98,56 @@ func (s *Spec) printStacksList(allStacks []stack.Entry) error {
if err != nil {
return errors.E(err, "Invalid stack configuration: "+failReason)
}

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

// 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 {
return errors.E("Failed to access node in graph: "+err.Error())
}

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 := s.Engine.FriendlyFmtDir(dir)
if !ok {
printer.Stderr.Error(fmt.Sprintf("Unable to format stack dir %s", dir))
continue
}
levelGroups[level] = append(levelGroups[level], friendlyDir)
}

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

// Original output format for non-runOrder case or when format is not json
for _, st := range stacks {
dir := st.Dir().String()
friendlyDir, ok := s.Engine.FriendlyFmtDir(dir)
Expand Down
2 changes: 2 additions & 0 deletions ui/tui/cli_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func DefaultAfterConfigHandler(ctx context.Context, c *CLI) (commands.Executor,
tel.StringFlag("filter-deployment-status", parsedArgs.List.DeploymentStatus),
tel.StringFlag("filter-target", parsedArgs.List.Target),
tel.BoolFlag("run-order", parsedArgs.List.RunOrder),
tel.StringFlag("format", parsedArgs.List.Format),
)
expStatus := parsedArgs.List.ExperimentalStatus
cloudStatus := parsedArgs.List.Status
Expand Down Expand Up @@ -354,6 +355,7 @@ func DefaultAfterConfigHandler(ctx context.Context, c *CLI) (commands.Executor,
RunOrder: parsedArgs.List.RunOrder,
Tags: parsedArgs.Tags,
NoTags: parsedArgs.NoTags,
Format: parsedArgs.List.Format,
}, true, false, nil

case "generate":
Expand Down
1 change: 1 addition & 0 deletions ui/tui/cli_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type FlagSpec 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" enum:"text,json"`

changeDetectionFlags
} `cmd:"" help:"List stacks."`
Expand Down