-
Notifications
You must be signed in to change notification settings - Fork 11
/
view.go
95 lines (77 loc) · 2.31 KB
/
view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package view
import (
"fmt"
"io"
"os"
"strings"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/usage"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
"github.com/spf13/cobra"
)
type ViewOptions struct {
Client *client.Client
Host string
IO io.Writer
Selector string
Space *spaces.Space
}
func NewCmdView(f factory.Factory) *cobra.Command {
opts := &ViewOptions{}
cmd := &cobra.Command{
Args: usage.ExactArgs(1),
Use: "view {<name> | <id>}",
Short: "View a space in an instance of Octopus Deploy",
Long: "View a space in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s space view Spaces-9000
$ %s space view Integrations
`), constants.ExecutableName, constants.ExecutableName),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := f.GetSystemClient()
if err != nil {
return err
}
opts.Client = client
opts.Host = os.Getenv("OCTOPUS_HOST")
opts.IO = cmd.OutOrStdout()
opts.Selector = args[0]
return viewRun(opts)
},
}
return cmd
}
func viewRun(opts *ViewOptions) error {
space, err := opts.Client.Spaces.GetByIDOrName(opts.Selector)
if err != nil {
return err
}
return printHumanSpacePreview(opts.Host, space, opts.IO)
}
func printHumanSpacePreview(host string, space *spaces.Space, out io.Writer) error {
// header
fmt.Fprintf(out, "%s %s\n", output.Bold(space.Name), output.Dimf("(%s)", space.GetID()))
// metadata
if len(space.Description) == 0 {
fmt.Fprintln(out, output.Dim("No description provided"))
} else {
fmt.Fprintln(out, output.Dim(space.Description))
}
// task processing
taskQueue := output.Green("Running")
if space.TaskQueueStopped {
taskQueue = output.Yellow("Stopped")
}
fmt.Fprintf(out, "Task Processing Status: %s\n", taskQueue)
// BUG: the hypermedia link, "Web" is not represented correctly in Octopus REST API
link := strings.Split(space.Links["Web"], "/app#/")
webLink := "/app#/configuration/" + link[1]
spaceURL := host + webLink
// footer
fmt.Fprintf(out, "View this space in Octopus Deploy: %s\n", output.Blue(spaceURL))
return nil
}