Skip to content

Commit 3408f2d

Browse files
feat: Space view command support -f param (#531)
1 parent c42d7d3 commit 3408f2d

1 file changed

Lines changed: 65 additions & 22 deletions

File tree

pkg/cmd/space/view/view.go

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package view
33
import (
44
"fmt"
55
"github.com/OctopusDeploy/cli/pkg/apiclient"
6-
"io"
76
"strings"
87

98
"github.com/OctopusDeploy/cli/pkg/factory"
@@ -20,9 +19,8 @@ import (
2019
type ViewOptions struct {
2120
Client *client.Client
2221
Host string
23-
IO io.Writer
2422
Selector string
25-
Space *spaces.Space
23+
Command *cobra.Command
2624
}
2725

2826
func NewCmdView(f factory.Factory) *cobra.Command {
@@ -45,8 +43,8 @@ func NewCmdView(f factory.Factory) *cobra.Command {
4543

4644
opts.Client = client
4745
opts.Host = f.GetCurrentHost()
48-
opts.IO = cmd.OutOrStdout()
4946
opts.Selector = args[0]
47+
opts.Command = cmd
5048

5149
return viewRun(opts)
5250
},
@@ -61,35 +59,80 @@ func viewRun(opts *ViewOptions) error {
6159
return err
6260
}
6361

64-
return printHumanSpacePreview(opts.Host, space, opts.IO)
62+
host := opts.Host
63+
return output.PrintResource(space, opts.Command, output.Mappers[*spaces.Space]{
64+
Json: func(item *spaces.Space) any {
65+
return SpaceAsJson{
66+
Id: item.GetID(),
67+
Name: item.Name,
68+
Description: item.Description,
69+
TaskQueue: getTaskQueueStatus(item),
70+
WebUrl: generateWebUrl(host, item),
71+
}
72+
},
73+
Table: output.TableDefinition[*spaces.Space]{
74+
Header: []string{"NAME", "DESCRIPTION", "TASK QUEUE", "WEB URL"},
75+
Row: func(item *spaces.Space) []string {
76+
description := item.Description
77+
if description == "" {
78+
description = constants.NoDescription
79+
}
80+
81+
return []string{output.Bold(item.Name), description, getTaskQueueStatusColored(item), output.Blue(generateWebUrl(host, item))}
82+
},
83+
},
84+
Basic: func(item *spaces.Space) string {
85+
return formatSpaceForBasic(host, item)
86+
},
87+
})
6588
}
6689

67-
func printHumanSpacePreview(host string, space *spaces.Space, out io.Writer) error {
68-
// header
69-
fmt.Fprintf(out, "%s %s\n", output.Bold(space.Name), output.Dimf("(%s)", space.GetID()))
70-
71-
// metadata
72-
if len(space.Description) == 0 {
73-
fmt.Fprintln(out, output.Dim(constants.NoDescription))
90+
type SpaceAsJson struct {
91+
Id string `json:"Id"`
92+
Name string `json:"Name"`
93+
Description string `json:"Description"`
94+
TaskQueue string `json:"TaskQueue"`
95+
WebUrl string `json:"WebUrl"`
96+
}
7497

75-
} else {
76-
fmt.Fprintln(out, output.Dim(space.Description))
98+
func getTaskQueueStatus(space *spaces.Space) string {
99+
if space.TaskQueueStopped {
100+
return "Stopped"
77101
}
102+
return "Running"
103+
}
78104

79-
// task processing
80-
taskQueue := output.Green("Running")
105+
func getTaskQueueStatusColored(space *spaces.Space) string {
81106
if space.TaskQueueStopped {
82-
taskQueue = output.Yellow("Stopped")
107+
return output.Yellow("Stopped")
83108
}
84-
fmt.Fprintf(out, "Task Processing Status: %s\n", taskQueue)
109+
return output.Green("Running")
110+
}
85111

86-
// BUG: the hypermedia link, "Web" is not represented correctly in Octopus REST API
112+
func generateWebUrl(host string, space *spaces.Space) string {
87113
link := strings.Split(space.Links["Web"], "/app#/")
88114
webLink := "/app#/configuration/" + link[1]
89-
spaceURL := host + webLink
115+
return host + webLink
116+
}
117+
118+
func formatSpaceForBasic(host string, space *spaces.Space) string {
119+
var result strings.Builder
120+
121+
// header
122+
result.WriteString(fmt.Sprintf("%s %s\n", output.Bold(space.Name), output.Dimf("(%s)", space.GetID())))
123+
124+
// metadata
125+
if len(space.Description) == 0 {
126+
result.WriteString(fmt.Sprintf("%s\n", output.Dim(constants.NoDescription)))
127+
} else {
128+
result.WriteString(fmt.Sprintf("%s\n", output.Dim(space.Description)))
129+
}
130+
131+
// task processing
132+
result.WriteString(fmt.Sprintf("Task Processing Status: %s\n", getTaskQueueStatusColored(space)))
90133

91134
// footer
92-
fmt.Fprintf(out, "View this space in Octopus Deploy: %s\n", output.Blue(spaceURL))
135+
result.WriteString(fmt.Sprintf("View this space in Octopus Deploy: %s", output.Blue(generateWebUrl(host, space))))
93136

94-
return nil
137+
return result.String()
95138
}

0 commit comments

Comments
 (0)