blob: 1ca0fbf8f4fc590bd9500f4b9b1376b7e63a11c4 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package commands
17
18import (
19 "context"
20 "encoding/json"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 "github.com/fullstorydev/grpcurl"
22 flags "github.com/jessevdk/go-flags"
23 "github.com/jhump/protoreflect/dynamic"
Scott Baker2b0ad652019-08-21 14:57:07 -070024 "github.com/opencord/voltctl/internal/pkg/cli/version"
25 "github.com/opencord/voltctl/pkg/format"
Zack Williamse940c7a2019-08-21 14:25:39 -070026 "strings"
27)
28
29type VersionDetails struct {
30 Version string `json:"version"`
31 GoVersion string `json:"goversion"`
32 VcsRef string `json:"gitcommit"`
33 VcsDirty string `json:"gitdirty"`
34 BuildTime string `json:"buildtime"`
35 Os string `json:"os"`
36 Arch string `json:"arch"`
37}
38
39type VersionOutput struct {
40 Client VersionDetails `json:"client"`
41 Cluster VersionDetails `json:"cluster"`
42}
43
44type VersionOpts struct {
45 OutputOptions
46 ClientOnly bool `long:"clientonly" description:"Display only client version information"`
47}
48
49var versionOpts = VersionOpts{}
50
51var versionInfo = VersionOutput{
52 Client: VersionDetails{
53 Version: version.Version,
54 GoVersion: version.GoVersion,
55 VcsRef: version.VcsRef,
56 VcsDirty: version.VcsDirty,
57 Os: version.Os,
58 Arch: version.Arch,
59 BuildTime: version.BuildTime,
60 },
61 Cluster: VersionDetails{
62 Version: "unknown-version",
63 GoVersion: "unknown-goversion",
64 VcsRef: "unknown-vcsref",
65 VcsDirty: "unknown-vcsdirty",
66 Os: "unknown-os",
67 Arch: "unknown-arch",
68 BuildTime: "unknown-buildtime",
69 },
70}
71
72func RegisterVersionCommands(parent *flags.Parser) {
73 _, err := parent.AddCommand("version", "display version", "Display client and server version", &versionOpts)
74 if err != nil {
75 panic(err)
76 }
77}
78
79const ClientOnlyFormat = `Client:
80 Version {{.Version}}
81 Go version: {{.GoVersion}}
82 Vcs reference: {{.VcsRef}}
83 Vcs dirty: {{.VcsDirty}}
84 Built: {{.BuildTime}}
85 OS/Arch: {{.Os}}/{{.Arch}}
86`
87
88const DefaultFormat = `Client:
89 Version {{.Client.Version}}
90 Go version: {{.Client.GoVersion}}
91 Vcs reference: {{.Client.VcsRef}}
92 Vcs dirty: {{.Client.VcsDirty}}
93 Built: {{.Client.BuildTime}}
94 OS/Arch: {{.Client.Os}}/{{.Client.Arch}}
95
96Cluster:
97 Version {{.Cluster.Version}}
98 Go version: {{.Cluster.GoVersion}}
99 Vcs feference: {{.Cluster.VcsRef}}
100 Vcs dirty: {{.Cluster.VcsDirty}}
101 Built: {{.Cluster.BuildTime}}
102 OS/Arch: {{.Cluster.Os}}/{{.Cluster.Arch}}
103`
104
105func (options *VersionOpts) clientOnlyVersion(args []string) error {
106 outputFormat := CharReplacer.Replace(options.Format)
107 if outputFormat == "" {
108 outputFormat = ClientOnlyFormat
109 }
110 if options.Quiet {
111 outputFormat = "{{.Version}}"
112 }
113
114 result := CommandResult{
115 Format: format.Format(outputFormat),
116 OutputAs: toOutputType(options.OutputAs),
117 NameLimit: options.NameLimit,
118 Data: versionInfo.Client,
119 }
120
121 GenerateOutput(&result)
122 return nil
123}
124
125func (options *VersionOpts) Execute(args []string) error {
126 if options.ClientOnly {
127 return options.clientOnlyVersion(args)
128 }
129
130 conn, err := NewConnection()
131 if err != nil {
132 return err
133 }
134 defer conn.Close()
135 descriptor, method, err := GetMethod("version")
136 if err != nil {
137 return err
138 }
139
140 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
141 defer cancel()
142
143 h := &RpcEventHandler{}
144 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams)
145 if err != nil {
146 return err
147 }
148
149 if h.Status != nil && h.Status.Err() != nil {
150 return h.Status.Err()
151 }
152
153 d, err := dynamic.AsDynamicMessage(h.Response)
154 if err != nil {
155 return err
156 }
157 version, err := d.TryGetFieldByName("version")
158 if err != nil {
159 return err
160 }
161
162 info := make(map[string]interface{})
163 err = json.Unmarshal([]byte(version.(string)), &info)
164 if err != nil {
165 versionInfo.Cluster.Version = strings.ReplaceAll(version.(string), "\n", "")
166 } else {
167 var ok bool
168 if _, ok = info["version"]; ok {
169 versionInfo.Cluster.Version = info["version"].(string)
170 } else {
171 versionInfo.Cluster.Version = "unknown-version"
172 }
173
174 if _, ok = info["goversion"]; ok {
175 versionInfo.Cluster.GoVersion = info["goversion"].(string)
176 } else {
177 versionInfo.Cluster.GoVersion = "unknown-goversion"
178 }
179
180 if _, ok = info["vcsref"]; ok {
181 versionInfo.Cluster.VcsRef = info["vcsref"].(string)
182 } else {
183 versionInfo.Cluster.VcsRef = "unknown-vcsref"
184 }
185
186 if _, ok = info["vcsdirty"]; ok {
187 versionInfo.Cluster.VcsDirty = info["vcsdirty"].(string)
188 } else {
189 versionInfo.Cluster.VcsDirty = "unknown-vcsdirty"
190 }
191
192 if _, ok = info["buildtime"]; ok {
193 versionInfo.Cluster.BuildTime = info["buildtime"].(string)
194 } else {
195 versionInfo.Cluster.BuildTime = "unknown-buildtime"
196 }
197
198 if _, ok = info["os"]; ok {
199 versionInfo.Cluster.Os = info["os"].(string)
200 } else {
201 versionInfo.Cluster.Os = "unknown-os"
202 }
203
204 if _, ok = info["arch"]; ok {
205 versionInfo.Cluster.Arch = info["arch"].(string)
206 } else {
207 versionInfo.Cluster.Arch = "unknown-arch"
208 }
209 }
210
211 outputFormat := CharReplacer.Replace(options.Format)
212 if outputFormat == "" {
213 outputFormat = DefaultFormat
214 }
215 if options.Quiet {
216 outputFormat = "{{.Client.Version}}"
217 }
218
219 result := CommandResult{
220 Format: format.Format(outputFormat),
221 OutputAs: toOutputType(options.OutputAs),
222 NameLimit: options.NameLimit,
223 Data: versionInfo,
224 }
225
226 GenerateOutput(&result)
227 return nil
228}