-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathview.go
More file actions
109 lines (90 loc) · 2.63 KB
/
view.go
File metadata and controls
109 lines (90 loc) · 2.63 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package view
import (
"errors"
"fmt"
"github.com/OctopusDeploy/cli/pkg/apiclient"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
const (
FlagTenant = "tenant"
FlagWeb = "web"
)
type ViewFlags struct {
Tenant *flag.Flag[string]
Web *flag.Flag[bool]
}
func NewViewFlags() *ViewFlags {
return &ViewFlags{
Tenant: flag.New[string](FlagTenant, false),
Web: flag.New[bool](FlagWeb, false),
}
}
func NewCmdView(f factory.Factory) *cobra.Command {
viewFlags := NewViewFlags()
cmd := &cobra.Command{
Use: "view {<name> | <id>}",
Short: "View a tenant",
Long: "View a tenant in Octopus Deploy",
Example: heredoc.Docf(`
$ %[1]s tenant view Tenants-1
$ %[1]s tenant view 'Tenant'
`, constants.ExecutableName),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 && viewFlags.Tenant.Value == "" {
viewFlags.Tenant.Value = args[0]
}
return viewRun(cmd, f, viewFlags)
},
}
flags := cmd.Flags()
flags.StringVarP(&viewFlags.Tenant.Value, viewFlags.Tenant.Name, "t", "", "Name or ID of the tenant to list variables for")
flags.BoolVarP(&viewFlags.Web.Value, viewFlags.Web.Name, "w", false, "Open in web browser")
return cmd
}
func viewRun(cmd *cobra.Command, f factory.Factory, flags *ViewFlags) error {
client, err := f.GetSpacedClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}
tenant := flags.Tenant.Value
if tenant == "" {
return errors.New("tenant must be specified")
}
selectedTenant, err := client.Tenants.GetByIdentifier(tenant)
if err != nil {
return err
}
out := cmd.OutOrStdout()
fmt.Fprintf(out, "%s %s\n", output.Bold(selectedTenant.Name), output.Dimf("(%s)", selectedTenant.ID))
if len(selectedTenant.TenantTags) > 0 {
fmt.Fprintf(out, "Tags: ")
}
for i, tag := range selectedTenant.TenantTags {
suffix := ", "
if i == len(selectedTenant.TenantTags)-1 {
suffix = ""
}
fmt.Fprintf(out, "%s%s", tag, suffix)
}
if len(selectedTenant.TenantTags) > 0 {
fmt.Fprintf(out, "\n")
}
if selectedTenant.Description == "" {
fmt.Fprintln(out, output.Dim(constants.NoDescription))
} else {
fmt.Fprintln(out, output.Dim(selectedTenant.Description))
}
link := fmt.Sprintf("%s/app#/%s/tenants/%s/overview", f.GetCurrentHost(), selectedTenant.SpaceID, selectedTenant.ID)
// footer
fmt.Fprintf(out, "View this tenant in Octopus Deploy: %s\n", output.Blue(link))
if flags.Web.Value {
browser.OpenURL(link)
}
return nil
}