diff --git a/components/local-app/cmd/organization-list.go b/components/local-app/cmd/organization-list.go index 95f9312ce15a43..3808e544e58910 100644 --- a/components/local-app/cmd/organization-list.go +++ b/components/local-app/cmd/organization-list.go @@ -59,5 +59,5 @@ var listOrganizationCommand = &cobra.Command{ func init() { orgCmd.AddCommand(listOrganizationCommand) - listOrganizationCommand.Flags().StringVarP(&orgListOutputField, "field", "f", "", "output a specific field of the organization") + listOrganizationCommand.Flags().StringVarP(&orgListOutputField, "field", "f", "", "output a specific field of the organizations") } diff --git a/components/local-app/cmd/workspace-list.go b/components/local-app/cmd/workspace-list.go index b30d5d5520f763..d1c4a1893785b9 100644 --- a/components/local-app/cmd/workspace-list.go +++ b/components/local-app/cmd/workspace-list.go @@ -9,6 +9,7 @@ import ( "fmt" "log/slog" "os" + "reflect" "time" "strings" @@ -50,6 +51,15 @@ func getWorkspaceBranch(ws *v1.Workspace) string { return value } +type WorkspaceDisplayData struct { + Repository string + Branch string + Id string + Status string +} + +var wsListOutputField string + // listWorkspaceCommand lists all available workspaces var listWorkspaceCommand = &cobra.Command{ Use: "list", @@ -79,10 +89,29 @@ var listWorkspaceCommand = &cobra.Command{ repository := getWorkspaceRepo(workspace) branch := getWorkspaceBranch(workspace) - table.Append([]string{repository, branch, workspace.WorkspaceId, TranslatePhase(workspace.GetStatus().Instance.Status.Phase.String())}) + wsData := WorkspaceDisplayData{ + Repository: repository, + Branch: branch, + Id: workspace.WorkspaceId, + Status: TranslatePhase(workspace.GetStatus().Instance.Status.Phase.String()), + } + + if wsListOutputField != "" { + wsListOutputField = common.CapitalizeFirst(wsListOutputField) + val := reflect.ValueOf(wsData) + if fieldVal := val.FieldByName(wsListOutputField); fieldVal.IsValid() { + fmt.Printf("%v\n", fieldVal.Interface()) + } else { + return fmt.Errorf("Field '%s' is an invalid field for workspaces", wsListOutputField) + } + } else { + table.Append([]string{wsData.Repository, wsData.Branch, wsData.Id, wsData.Status}) + } } - table.Render() + if wsListOutputField == "" { + table.Render() + } return nil }, @@ -90,4 +119,5 @@ var listWorkspaceCommand = &cobra.Command{ func init() { wsCmd.AddCommand(listWorkspaceCommand) + listWorkspaceCommand.Flags().StringVarP(&wsListOutputField, "field", "f", "", "output a specific field of the workspaces") }