-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathshow.go
141 lines (114 loc) · 4.22 KB
/
show.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
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
/*
Copyright 2023 The Radius Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package show
import (
"context"
"github.com/radius-project/radius/pkg/cli"
"github.com/radius-project/radius/pkg/cli/clients"
"github.com/radius-project/radius/pkg/cli/clierrors"
"github.com/radius-project/radius/pkg/cli/cmd/commonflags"
"github.com/radius-project/radius/pkg/cli/connections"
"github.com/radius-project/radius/pkg/cli/framework"
"github.com/radius-project/radius/pkg/cli/objectformats"
"github.com/radius-project/radius/pkg/cli/output"
"github.com/radius-project/radius/pkg/cli/workspaces"
"github.com/spf13/cobra"
)
// NewCommand creates an instance of the command and runner for the `rad env show` command.
//
// NewCommand creates a new Cobra command and a Runner object to show environment details, with flags for workspace,
// resource group, environment name and output.
func NewCommand(factory framework.Factory) (*cobra.Command, framework.Runner) {
runner := NewRunner(factory)
cmd := &cobra.Command{
Use: "show",
Short: "Show environment details",
Long: `Show environment details. Shows the user's default environment by default.`,
Args: cobra.MaximumNArgs(1),
Example: `
# Show current environment
rad env show
# Show specified environment
rad env show my-env
# Show specified environment in a specified resource group
rad env show my-env --group my-env
`,
RunE: framework.RunCommand(runner),
}
commonflags.AddWorkspaceFlag(cmd)
commonflags.AddResourceGroupFlag(cmd)
commonflags.AddEnvironmentNameFlag(cmd)
commonflags.AddOutputFlag(cmd)
return cmd, runner
}
// Runner is the runner implementation for the `rad env show` command.
type Runner struct {
ConfigHolder *framework.ConfigHolder
ConnectionFactory connections.Factory
Workspace *workspaces.Workspace
Output output.Interface
EnvironmentName string
Format string
}
// NewRunner creates a new instance of the `rad env show` runner.
func NewRunner(factory framework.Factory) *Runner {
return &Runner{
ConnectionFactory: factory.GetConnectionFactory(),
ConfigHolder: factory.GetConfigHolder(),
Output: factory.GetOutput(),
}
}
// Validate runs validation for the `rad env show` command.
//
// Validate checks the request object for a workspace, scope, environment name, and output format, and sets the
// corresponding fields in the Runner struct if they are found. If any of these fields are not found, an error is returned.
func (r *Runner) Validate(cmd *cobra.Command, args []string) error {
workspace, err := cli.RequireWorkspace(cmd, r.ConfigHolder.Config, r.ConfigHolder.DirectoryConfig)
if err != nil {
return err
}
r.Workspace = workspace
// Allow '--group' to override scope
scope, err := cli.RequireScope(cmd, *r.Workspace)
if err != nil {
return err
}
r.Workspace.Scope = scope
r.EnvironmentName, err = cli.RequireEnvironmentNameArgs(cmd, args, *workspace)
if err != nil {
return err
}
format, err := cli.RequireOutput(cmd)
if err != nil {
return err
}
r.Format = format
return nil
}
// Run runs the `rad env run` command.
//
// Run attempts to retrieve environment details from an ApplicationsManagementClient and write the details to an
// output in a specified format, returning an error if any of these operations fail.
func (r *Runner) Run(ctx context.Context) error {
client, err := r.ConnectionFactory.CreateApplicationsManagementClient(ctx, *r.Workspace)
if err != nil {
return err
}
env, err := client.GetEnvironment(ctx, r.EnvironmentName)
if clients.Is404Error(err) {
return clierrors.Message("The environment %q was not found or has been deleted.", r.EnvironmentName)
} else if err != nil {
return err
}
return r.Output.WriteFormatted(r.Format, env, objectformats.GetResourceTableFormat())
}