Skip to content

Commit

Permalink
Adds quiet flag to infractl list (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stringy authored Jul 17, 2024
1 parent f35e6ca commit 28acef9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
12 changes: 10 additions & 2 deletions cmd/infractl/cluster/list/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ $ infractl list --expired
$ infractl list --all
# List clusters whose name matches a prefix.
$ infractl list --prefix=<match>`
$ infractl list --prefix=<match>
# List only the names of clusters
$ infractl list --quiet`

// Command defines the handler for infractl list.
func Command() *cobra.Command {
Expand All @@ -36,13 +39,15 @@ func Command() *cobra.Command {

cmd.Flags().Bool("all", false, "include clusters not owned by you")
cmd.Flags().Bool("expired", false, "include expired clusters")
cmd.Flags().BoolP("quiet", "q", false, "only output cluster names")
cmd.Flags().String("prefix", "", "only include clusters whose names matches this prefix")
return cmd
}

func run(ctx context.Context, conn *grpc.ClientConn, cmd *cobra.Command, _ []string) (common.PrettyPrinter, error) {
includeAll := common.MustBool(cmd.Flags(), "all")
includeExpired := common.MustBool(cmd.Flags(), "expired")
quietMode := common.MustBool(cmd.Flags(), "quiet")
prefix, _ := cmd.Flags().GetString("prefix")

req := v1.ClusterListRequest{
Expand All @@ -56,5 +61,8 @@ func run(ctx context.Context, conn *grpc.ClientConn, cmd *cobra.Command, _ []str
return nil, err
}

return prettyClusterListResponse(*resp), nil
return prettyClusterListResponse{
ClusterListResponse: *resp,
QuietMode: quietMode,
}, nil
}
23 changes: 14 additions & 9 deletions cmd/infractl/cluster/list/fancy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
v1 "github.com/stackrox/infra/generated/api/v1"
)

type prettyClusterListResponse v1.ClusterListResponse
type prettyClusterListResponse struct {
v1.ClusterListResponse
QuietMode bool
}

func (p prettyClusterListResponse) PrettyPrint(cmd *cobra.Command) {
for _, cluster := range p.Clusters {
Expand All @@ -23,15 +26,17 @@ func (p prettyClusterListResponse) PrettyPrint(cmd *cobra.Command) {
)

cmd.Printf("%s \n", cluster.GetID())
cmd.Printf(" Flavor: %s\n", cluster.GetFlavor())
cmd.Printf(" Owner: %s\n", cluster.GetOwner())
cmd.Printf(" Description: %s\n", cluster.GetDescription())
cmd.Printf(" Status: %s\n", cluster.GetStatus())
cmd.Printf(" Created: %v\n", common.FormatTime(createdOn))
if destroyedOn.Unix() != 0 {
cmd.Printf(" Destroyed: %v\n", common.FormatTime(destroyedOn))
if !p.QuietMode {
cmd.Printf(" Flavor: %s\n", cluster.GetFlavor())
cmd.Printf(" Owner: %s\n", cluster.GetOwner())
cmd.Printf(" Description: %s\n", cluster.GetDescription())
cmd.Printf(" Status: %s\n", cluster.GetStatus())
cmd.Printf(" Created: %v\n", common.FormatTime(createdOn))
if destroyedOn.Unix() != 0 {
cmd.Printf(" Destroyed: %v\n", common.FormatTime(destroyedOn))
}
cmd.Printf(" Lifespan: %s\n", common.FormatExpiration(remaining))
}
cmd.Printf(" Lifespan: %s\n", common.FormatExpiration(remaining))
}
}

Expand Down

0 comments on commit 28acef9

Please sign in to comment.