-
Notifications
You must be signed in to change notification settings - Fork 135
/
cmd.go
146 lines (123 loc) · 3.66 KB
/
cmd.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
142
143
144
145
146
/*
Copyright (c) 2020 Red Hat, Inc.
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 cluster
import (
"bytes"
"fmt"
"os"
"regexp"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"
c "github.com/openshift-online/ocm-cli/pkg/cluster"
clusterpkg "github.com/openshift-online/ocm-cli/pkg/cluster"
"github.com/openshift-online/ocm-cli/pkg/dump"
"github.com/openshift-online/ocm-cli/pkg/ocm"
)
var args struct {
json bool
output bool
}
var Cmd = &cobra.Command{
Use: "cluster [flags] {NAME|ID|EXTERNAL_ID}",
Short: "Show details of a cluster",
Long: "Show details of a cluster identified by name, identifier or external identifier",
RunE: run,
}
func init() {
// Add flags to rootCmd:
flags := Cmd.Flags()
flags.BoolVar(
&args.output,
"output",
false,
"Output result into JSON file.",
)
flags.BoolVar(
&args.json,
"json",
false,
"Output the entire JSON structure",
)
}
func run(cmd *cobra.Command, argv []string) error {
// Check that there is exactly one cluster name, identifir or external identifier in the
// command line arguments:
if len(argv) != 1 {
fmt.Fprintf(
os.Stderr,
"Expected exactly one cluster name, identifier or external identifier "+
"is required\n",
)
os.Exit(1)
}
// Check that the cluster key (name, identifier or external identifier) given by the user
// is reasonably safe so that there is no risk of SQL injection:
key := argv[0]
if !keyRE.MatchString(key) {
fmt.Fprintf(
os.Stderr,
"Cluster name, identifier or external identifier '%s' isn't valid: it "+
"must contain only letters, digits, dashes and underscores\n",
key,
)
os.Exit(1)
}
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
return fmt.Errorf("Failed to create OCM connection: %v", err)
}
defer connection.Close()
cluster, err := c.GetCluster(connection, key)
if err != nil {
return fmt.Errorf("Can't retrieve cluster for key '%s': %v", key, err)
}
if args.output {
// Create a filename based on cluster name:
filename := fmt.Sprintf("cluster-%s.json", cluster.ID())
// Attempt to create file:
myFile, err := os.Create(filename)
if err != nil {
return fmt.Errorf("Failed to create file: %v", err)
}
// Dump encoder content into file:
err = cmv1.MarshalCluster(cluster, myFile)
if err != nil {
return fmt.Errorf("Failed to Marshal cluster into file: %v", err)
}
}
// Get full API response (JSON):
if args.json {
// Buffer for pretty output:
buf := new(bytes.Buffer)
fmt.Println()
// Convert cluster to JSON and dump to encoder:
err = cmv1.MarshalCluster(cluster, buf)
if err != nil {
return fmt.Errorf("Failed to Marshal cluster into JSON encoder: %v", err)
}
err = dump.Pretty(os.Stdout, buf.Bytes())
if err != nil {
return fmt.Errorf("Can't print body: %v", err)
}
} else {
err = clusterpkg.PrintClusterDescription(connection, cluster)
if err != nil {
return err
}
}
return nil
}
// Regular expression to check that the cluster key (name, identifier or external identifier) given
// by the user is reasonably safe and that there is no risk of SQL injection.
var keyRE = regexp.MustCompile(`^(\w|-)+$`)