Skip to content

Commit 316c141

Browse files
committed
fix: brev ls all showing all instances in org
1 parent 39ce87a commit 316c141

2 files changed

Lines changed: 50 additions & 55 deletions

File tree

pkg/cmd/ls/ls.go

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/brevdev/brev-cli/pkg/cmdcontext"
2525
"github.com/brevdev/brev-cli/pkg/config"
2626
"github.com/brevdev/brev-cli/pkg/entity"
27-
"github.com/brevdev/brev-cli/pkg/entity/virtualproject"
2827
breverrors "github.com/brevdev/brev-cli/pkg/errors"
2928
"github.com/brevdev/brev-cli/pkg/featureflag"
3029
"github.com/brevdev/brev-cli/pkg/store"
@@ -383,27 +382,14 @@ func (ls Ls) RunUser(_ bool) error {
383382
return nil
384383
}
385384

386-
func (ls Ls) ShowAllWorkspaces(org *entity.Organization, otherOrgs []entity.Organization, user *entity.User, allWorkspaces []entity.Workspace, gpuLookup map[string]string) {
387-
userWorkspaces := store.FilterForUserWorkspaces(allWorkspaces, user.ID)
388-
ls.displayWorkspacesAndHelp(org, otherOrgs, userWorkspaces, allWorkspaces, gpuLookup)
389-
390-
projects := virtualproject.NewVirtualProjects(allWorkspaces)
391-
392-
var unjoinedProjects []virtualproject.VirtualProject
393-
for _, p := range projects {
394-
wks := p.GetUserWorkspaces(user.ID)
395-
if len(wks) == 0 {
396-
unjoinedProjects = append(unjoinedProjects, p)
397-
}
398-
}
399-
400-
displayProjects(ls.terminal, org.Name, unjoinedProjects)
385+
func (ls Ls) ShowAllWorkspaces(org *entity.Organization, otherOrgs []entity.Organization, workspacesToShow []entity.Workspace, gpuLookup map[string]string) {
386+
ls.displayWorkspacesAndHelp(org, otherOrgs, workspacesToShow, workspacesToShow, true, gpuLookup)
401387
}
402388

403389
func (ls Ls) ShowUserWorkspaces(org *entity.Organization, otherOrgs []entity.Organization, user *entity.User, allWorkspaces []entity.Workspace, gpuLookup map[string]string) {
404390
userWorkspaces := store.FilterForUserWorkspaces(allWorkspaces, user.ID)
405391

406-
ls.displayWorkspacesAndHelp(org, otherOrgs, userWorkspaces, allWorkspaces, gpuLookup)
392+
ls.displayWorkspacesAndHelp(org, otherOrgs, userWorkspaces, allWorkspaces, false, gpuLookup)
407393
}
408394

409395
func (ls Ls) ShowOrgWorkspaces(org *entity.Organization, workspaces []entity.Workspace, gpuLookup map[string]string) {
@@ -419,10 +405,10 @@ func (ls Ls) ShowOrgWorkspaces(org *entity.Organization, workspaces []entity.Wor
419405
displayLsResetBreadCrumb(ls.terminal, workspaces)
420406
}
421407

422-
func (ls Ls) displayWorkspacesAndHelp(org *entity.Organization, otherOrgs []entity.Organization, userWorkspaces []entity.Workspace, allWorkspaces []entity.Workspace, gpuLookup map[string]string) {
423-
if len(userWorkspaces) == 0 {
408+
func (ls Ls) displayWorkspacesAndHelp(org *entity.Organization, otherOrgs []entity.Organization, workspacesToDisplay []entity.Workspace, allWorkspaces []entity.Workspace, showAll bool, gpuLookup map[string]string) {
409+
if len(workspacesToDisplay) == 0 {
424410
ls.terminal.Vprint(ls.terminal.Yellow("No instances in org %s\n", org.Name))
425-
if len(allWorkspaces) > 0 {
411+
if !showAll && len(allWorkspaces) > 0 {
426412
ls.terminal.Vprintf("%s", ls.terminal.Green("See teammates' instances:\n"))
427413
ls.terminal.Vprintf("%s", ls.terminal.Yellow("\tbrev ls --all\n"))
428414
} else {
@@ -434,13 +420,17 @@ func (ls Ls) displayWorkspacesAndHelp(org *entity.Organization, otherOrgs []enti
434420
ls.terminal.Vprintf("%s", ls.terminal.Yellow(fmt.Sprintf("\tbrev set %s\n", getOtherOrg(otherOrgs, *org).Name)))
435421
}
436422
} else {
437-
ls.terminal.Vprintf("You have %d instances in Org %s\n", len(userWorkspaces), ls.terminal.Yellow(org.Name))
438-
displayWorkspacesTable(ls.terminal, userWorkspaces, gpuLookup)
423+
if showAll {
424+
ls.terminal.Vprintf("%d instances in Org %s\n", len(workspacesToDisplay), ls.terminal.Yellow(org.Name))
425+
} else {
426+
ls.terminal.Vprintf("You have %d instances in Org %s\n", len(workspacesToDisplay), ls.terminal.Yellow(org.Name))
427+
}
428+
displayWorkspacesTable(ls.terminal, workspacesToDisplay, gpuLookup)
439429

440430
fmt.Print("\n")
441431

442-
displayLsResetBreadCrumb(ls.terminal, userWorkspaces)
443-
// displayLsConnectBreadCrumb(ls.terminal, userWorkspaces)
432+
displayLsResetBreadCrumb(ls.terminal, workspacesToDisplay)
433+
// displayLsConnectBreadCrumb(ls.terminal, workspacesToDisplay)
444434
}
445435
}
446436

@@ -548,7 +538,7 @@ func (ls Ls) RunWorkspaces(cliAuth auth.CLIAuth, org *entity.Organization, showA
548538
return breverrors.WrapAndTrace(err)
549539
}
550540
if showAll {
551-
ls.ShowAllWorkspaces(org, orgs, user, allWorkspaces, gpuLookup)
541+
ls.ShowAllWorkspaces(org, orgs, workspacesToShow, gpuLookup)
552542
if len(nodes) > 0 {
553543
ls.terminal.Vprintf("\nYou have %d external node(s) in Org %s\n", len(nodes), ls.terminal.Yellow(org.Name))
554544
displayNodesTable(ls.terminal, nodes, ls.piped)
@@ -680,23 +670,6 @@ func (ls Ls) RunHosts(org *entity.Organization) error {
680670
return nil
681671
}
682672

683-
func displayProjects(t *terminal.Terminal, orgName string, projects []virtualproject.VirtualProject) {
684-
if len(projects) > 0 {
685-
fmt.Print("\n")
686-
t.Vprintf("%d other projects in Org %s\n", len(projects), t.Yellow(orgName))
687-
displayProjectsTable(projects)
688-
689-
fmt.Print("\n")
690-
t.Vprintf("%s", t.Green("Join a project:\n")+
691-
t.Yellow(fmt.Sprintf("\tbrev start %s\n", projects[0].Name)))
692-
} else {
693-
t.Vprintf("no other projects in Org %s\n", t.Yellow(orgName))
694-
fmt.Print("\n")
695-
t.Vprintf("%s", t.Green("Invite a teamate:\n")+
696-
t.Yellow("\tbrev invite"))
697-
}
698-
}
699-
700673
func getBrevTableOptions() table.Options {
701674
options := table.OptionsDefault
702675
options.DrawBorder = false
@@ -793,19 +766,6 @@ func displayOrgTable(t *terminal.Terminal, orgs []entity.Organization, currentOr
793766
ta.Render()
794767
}
795768

796-
func displayProjectsTable(projects []virtualproject.VirtualProject) {
797-
ta := table.NewWriter()
798-
ta.SetOutputMirror(os.Stdout)
799-
ta.Style().Options = getBrevTableOptions()
800-
header := table.Row{"NAME", "MEMBERS"}
801-
ta.AppendHeader(header)
802-
for _, p := range projects {
803-
workspaceRow := []table.Row{{p.Name, p.GetUniqueUserCount()}}
804-
ta.AppendRows(workspaceRow)
805-
}
806-
ta.Render()
807-
}
808-
809769
func getStatusColoredText(t *terminal.Terminal, status string) string {
810770
switch status {
811771
case entity.Running, entity.Ready, string(entity.Completed):

pkg/cmd/ls/ls_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,41 @@ func TestRunLs_OrgsJSON(t *testing.T) {
401401
}
402402
}
403403

404+
// TestRunLs_ShowAllTable verifies that --all lists every org instance in table output.
405+
func TestRunLs_ShowAllTable(t *testing.T) {
406+
s := newTestStore()
407+
s.workspaces = []entity.Workspace{
408+
{
409+
ID: "ws-mine",
410+
Name: "my-ws",
411+
Status: entity.Running,
412+
VerbBuildStatus: entity.Completed,
413+
CreatedByUserID: "u1",
414+
},
415+
{
416+
ID: "ws-other",
417+
Name: "other-ws",
418+
Status: entity.Running,
419+
VerbBuildStatus: entity.Completed,
420+
CreatedByUserID: "u2",
421+
},
422+
}
423+
term := terminal.New()
424+
425+
out := captureStdout(t, func() {
426+
err := RunLs(term, resolveTestCLIAuth(t, s), s, nil, "", true, false)
427+
if err != nil {
428+
t.Fatalf("RunLs --all returned error: %v", err)
429+
}
430+
})
431+
if !strings.Contains(out, "my-ws") || !strings.Contains(out, "other-ws") {
432+
t.Fatalf("expected both workspaces in table output, got:\n%s", out)
433+
}
434+
if strings.Contains(out, "brev ls --all") {
435+
t.Fatal("should not suggest brev ls --all when already using --all")
436+
}
437+
}
438+
404439
// TestRunLs_ShowAll verifies that --all includes workspaces from other users.
405440
func TestRunLs_ShowAllJSON(t *testing.T) {
406441
s := newTestStore()

0 commit comments

Comments
 (0)