-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathview.go
More file actions
161 lines (138 loc) · 4.39 KB
/
view.go
File metadata and controls
161 lines (138 loc) · 4.39 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package view
import (
"fmt"
"strings"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/apiclient"
"github.com/OctopusDeploy/cli/pkg/cmd/environment/helper"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/usage"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
const (
FlagWeb = "web"
)
type ViewFlags struct {
Web *flag.Flag[bool]
}
func NewViewFlags() *ViewFlags {
return &ViewFlags{
Web: flag.New[bool](FlagWeb, false),
}
}
type ViewOptions struct {
Client *client.Client
Host string
idOrName string
flags *ViewFlags
Command *cobra.Command
}
func NewCmdView(f factory.Factory) *cobra.Command {
viewFlags := NewViewFlags()
cmd := &cobra.Command{
Args: usage.ExactArgs(1),
Use: "view {<name> | <id>}",
Short: "View an environment",
Long: "View an environment in Octopus Deploy",
Example: heredoc.Docf(`
$ %[1]s environment view 'Production'
$ %[1]s environment view Environments-102
`, constants.ExecutableName),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := f.GetSystemClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}
opts := &ViewOptions{
client,
f.GetCurrentHost(),
args[0],
viewFlags,
cmd,
}
return viewRun(opts)
},
}
flags := cmd.Flags()
flags.BoolVarP(&viewFlags.Web.Value, viewFlags.Web.Name, "w", false, "Open in web browser")
return cmd
}
func viewRun(opts *ViewOptions) error {
environment, err := helper.GetByIDOrName(opts.Client.Environments, opts.idOrName)
if err != nil {
return err
}
return output.PrintResource(environment, opts.Command, output.Mappers[*environments.Environment]{
Json: func(env *environments.Environment) any {
return EnvironmentAsJson{
Id: env.GetID(),
Slug: env.Slug,
Name: env.Name,
Description: env.Description,
UseGuidedFailure: env.UseGuidedFailure,
AllowDynamicInfrastructure: env.AllowDynamicInfrastructure,
WebUrl: generateWebUrl(opts.Host, env),
}
},
Table: output.TableDefinition[*environments.Environment]{
Header: []string{"NAME", "SLUG", "DESCRIPTION", "GUIDED FAILURE", "DYNAMIC INFRASTRUCTURE", "WEB URL"},
Row: func(env *environments.Environment) []string {
description := env.Description
if description == "" {
description = constants.NoDescription
}
return []string{
output.Bold(env.Name),
env.Slug,
description,
getBoolToString(env.UseGuidedFailure, "Enabled", "Disabled"),
getBoolToString(env.UseGuidedFailure, "Allowed", "Disallowed"),
output.Blue(generateWebUrl(opts.Host, env)),
}
},
},
Basic: func(env *environments.Environment) string {
var result strings.Builder
// header
result.WriteString(fmt.Sprintf("%s %s\n", output.Bold(env.Name), output.Dimf("(%s)", env.GetID())))
// metadata
if len(env.Description) == 0 {
result.WriteString(fmt.Sprintf("%s\n", output.Dim(constants.NoDescription)))
} else {
result.WriteString(fmt.Sprintf("%s\n", output.Dim(env.Description)))
}
url := generateWebUrl(opts.Host, env)
result.WriteString(fmt.Sprintf("View this environment in Octopus Deploy: %s\n", output.Blue(url)))
if opts.flags.Web.Value {
browser.OpenURL(url)
}
return result.String()
},
})
}
type EnvironmentAsJson struct {
Id string `json:"Id"`
Slug string `json:"Slug"`
Name string `json:"Name"`
Description string `json:"Description"`
UseGuidedFailure bool `json:"UseGuidedFailure"`
AllowDynamicInfrastructure bool `json:"AllowDynamicInfrastructure"`
WebUrl string `json:"WebUrl"`
}
func generateWebUrl(host string, env *environments.Environment) string {
return util.GenerateWebURL(host, env.SpaceID, fmt.Sprintf("infrastructure/environments/%s", env.GetID()))
}
func getBoolToString(value bool, trueString string, falseString string) string {
if value {
return trueString
} else {
return falseString
}
}