Skip to content

Commit d6c87a2

Browse files
authored
fix(ls): unify piped and non-piped output (workbench backwards compat) (#292)
fix(ls): unify piped and non-piped output to use colored format Workbench depends on parsing the colored output from brev ls, so remove the piped/non-piped branching and always use the rich colored table. The plain variants are kept for future use once Workbench stops depending on the colored format.
1 parent 21031b7 commit d6c87a2

1 file changed

Lines changed: 35 additions & 54 deletions

File tree

pkg/cmd/ls/ls.go

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,11 @@ with other commands like stop, start, or delete.`,
101101
Args: cmderrors.TransformToValidationError(cobra.MinimumNArgs(0)),
102102
ValidArgs: []string{"orgs", "workspaces"},
103103
RunE: func(cmd *cobra.Command, args []string) error {
104-
// Auto-switch to names-only output when piped (for chaining with stop/start/delete)
105-
piped := cmdutil.IsStdoutPiped()
106-
107-
err := RunLs(t, loginLsStore, args, org, showAll, jsonOutput, piped)
104+
err := RunLs(t, loginLsStore, args, org, showAll, jsonOutput)
108105
if err != nil {
109106
return breverrors.WrapAndTrace(err)
110107
}
111-
// Call analytics for ls (skip when piped to avoid polluting output)
112-
if !piped && !jsonOutput {
108+
if !jsonOutput {
113109
trackLsAnalytics(loginLsStore)
114110
}
115111
return nil
@@ -172,8 +168,8 @@ func getOrgForRunLs(lsStore LsStore, orgflag string) (*entity.Organization, erro
172168
return org, nil
173169
}
174170

175-
func RunLs(t *terminal.Terminal, lsStore LsStore, args []string, orgflag string, showAll bool, jsonOutput bool, piped bool) error {
176-
ls := NewLs(lsStore, t, jsonOutput, piped)
171+
func RunLs(t *terminal.Terminal, lsStore LsStore, args []string, orgflag string, showAll bool, jsonOutput bool) error {
172+
ls := NewLs(lsStore, t, jsonOutput)
177173
user, err := lsStore.GetCurrentUser()
178174
if err != nil {
179175
return breverrors.WrapAndTrace(err)
@@ -238,15 +234,13 @@ type Ls struct {
238234
lsStore LsStore
239235
terminal *terminal.Terminal
240236
jsonOutput bool
241-
piped bool
242237
}
243238

244-
func NewLs(lsStore LsStore, terminal *terminal.Terminal, jsonOutput bool, piped bool) *Ls {
239+
func NewLs(lsStore LsStore, terminal *terminal.Terminal, jsonOutput bool) *Ls {
245240
return &Ls{
246241
lsStore: lsStore,
247242
terminal: terminal,
248243
jsonOutput: jsonOutput,
249-
piped: piped,
250244
}
251245
}
252246

@@ -267,9 +261,6 @@ func (ls Ls) RunOrgs() error {
267261
fmt.Println("[]")
268262
return nil
269263
}
270-
if ls.piped {
271-
return nil
272-
}
273264
ls.terminal.Vprint(ls.terminal.Yellow(fmt.Sprintf("You don't have any orgs. Create one! %s", config.GlobalConfig.GetConsoleURL())))
274265
return nil
275266
}
@@ -284,13 +275,7 @@ func (ls Ls) RunOrgs() error {
284275
return ls.outputOrgsJSON(orgs, defaultOrg)
285276
}
286277

287-
// Handle piped output - clean table without colors
288-
if ls.piped {
289-
displayOrgTablePlain(orgs, defaultOrg)
290-
return nil
291-
}
292-
293-
// Standard table output
278+
// Table output with colors and help text
294279
ls.terminal.Vprint(ls.terminal.Yellow("Your organizations:"))
295280
displayOrgTable(ls.terminal, orgs, defaultOrg)
296281
if len(orgs) > 1 {
@@ -427,14 +412,7 @@ func (ls Ls) RunWorkspaces(org *entity.Organization, user *entity.User, showAll
427412
return ls.outputWorkspacesJSON(workspacesToShow)
428413
}
429414

430-
// Handle piped output - clean table without colors or extra text
431-
// Enables: brev ls | grep RUNNING | awk '{print $1}' | brev stop
432-
if ls.piped {
433-
displayWorkspacesTablePlain(workspacesToShow)
434-
return nil
435-
}
436-
437-
// Standard table output with colors and help text
415+
// Table output with colors and help text
438416
orgs, err := ls.lsStore.GetOrganizations(nil)
439417
if err != nil {
440418
return breverrors.WrapAndTrace(err)
@@ -551,23 +529,6 @@ func displayWorkspacesTable(t *terminal.Terminal, workspaces []entity.Workspace)
551529
ta.Render()
552530
}
553531

554-
// displayWorkspacesTablePlain outputs a clean table without colors for piping
555-
// Enables: brev ls | grep RUNNING | awk '{print $1}' | brev stop
556-
func displayWorkspacesTablePlain(workspaces []entity.Workspace) {
557-
ta := table.NewWriter()
558-
ta.SetOutputMirror(os.Stdout)
559-
ta.Style().Options = getBrevTableOptions()
560-
header := table.Row{"NAME", "STATUS", "BUILD", "SHELL", "ID", "MACHINE"}
561-
ta.AppendHeader(header)
562-
for _, w := range workspaces {
563-
status := getWorkspaceDisplayStatus(w)
564-
instanceString := cmdutil.GetInstanceString(w)
565-
workspaceRow := []table.Row{{w.Name, status, string(w.VerbBuildStatus), getShellDisplayStatus(w), w.ID, instanceString}}
566-
ta.AppendRows(workspaceRow)
567-
}
568-
ta.Render()
569-
}
570-
571532
func getShellDisplayStatus(w entity.Workspace) string {
572533
status := entity.NotReady
573534
if w.Status == entity.Running && w.VerbBuildStatus == entity.Completed {
@@ -584,25 +545,29 @@ func getWorkspaceDisplayStatus(w entity.Workspace) string {
584545
return status
585546
}
586547

587-
func displayOrgTable(t *terminal.Terminal, orgs []entity.Organization, currentOrg *entity.Organization) {
548+
// TODO: use displayOrgTablePlain and displayWorkspacesTablePlain for piped output
549+
// once Workbench stops depending on the colored output format.
550+
551+
// displayWorkspacesTablePlain outputs a clean table without colors for piping
552+
// Enables: brev ls | grep RUNNING | awk '{print $1}' | brev stop
553+
func displayWorkspacesTablePlain(workspaces []entity.Workspace) { //nolint:unused // see TODO above
588554
ta := table.NewWriter()
589555
ta.SetOutputMirror(os.Stdout)
590556
ta.Style().Options = getBrevTableOptions()
591-
header := table.Row{"NAME", "ID"}
557+
header := table.Row{"NAME", "STATUS", "BUILD", "SHELL", "ID", "MACHINE"}
592558
ta.AppendHeader(header)
593-
for _, o := range orgs {
594-
workspaceRow := []table.Row{{o.Name, o.ID}}
595-
if o.ID == currentOrg.ID {
596-
workspaceRow = []table.Row{{t.Green("* " + o.Name), t.Green(o.ID)}}
597-
}
559+
for _, w := range workspaces {
560+
status := getWorkspaceDisplayStatus(w)
561+
instanceString := cmdutil.GetInstanceString(w)
562+
workspaceRow := []table.Row{{w.Name, status, string(w.VerbBuildStatus), getShellDisplayStatus(w), w.ID, instanceString}}
598563
ta.AppendRows(workspaceRow)
599564
}
600565
ta.Render()
601566
}
602567

603568
// displayOrgTablePlain outputs a clean table without colors for piping
604569
// Enables: brev ls orgs | grep myorg | awk '{print $1}'
605-
func displayOrgTablePlain(orgs []entity.Organization, currentOrg *entity.Organization) {
570+
func displayOrgTablePlain(orgs []entity.Organization, currentOrg *entity.Organization) { //nolint:unused // see TODO above
606571
ta := table.NewWriter()
607572
ta.SetOutputMirror(os.Stdout)
608573
ta.Style().Options = getBrevTableOptions()
@@ -618,6 +583,22 @@ func displayOrgTablePlain(orgs []entity.Organization, currentOrg *entity.Organiz
618583
ta.Render()
619584
}
620585

586+
func displayOrgTable(t *terminal.Terminal, orgs []entity.Organization, currentOrg *entity.Organization) {
587+
ta := table.NewWriter()
588+
ta.SetOutputMirror(os.Stdout)
589+
ta.Style().Options = getBrevTableOptions()
590+
header := table.Row{"NAME", "ID"}
591+
ta.AppendHeader(header)
592+
for _, o := range orgs {
593+
workspaceRow := []table.Row{{o.Name, o.ID}}
594+
if o.ID == currentOrg.ID {
595+
workspaceRow = []table.Row{{t.Green("* " + o.Name), t.Green(o.ID)}}
596+
}
597+
ta.AppendRows(workspaceRow)
598+
}
599+
ta.Render()
600+
}
601+
621602
func displayProjectsTable(projects []virtualproject.VirtualProject) {
622603
ta := table.NewWriter()
623604
ta.SetOutputMirror(os.Stdout)

0 commit comments

Comments
 (0)