blob: 4af5a8a33c6852678f85751aabcdc5b11a57f8c2 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package commands
18
19import (
Scott Baker6cf525a2019-05-09 12:25:08 -070020 "context"
21 "github.com/fullstorydev/grpcurl"
Scott Baker2c0ebda2019-05-06 16:55:47 -070022 flags "github.com/jessevdk/go-flags"
Scott Baker6cf525a2019-05-09 12:25:08 -070023 "github.com/jhump/protoreflect/dynamic"
Scott Baker2c0ebda2019-05-06 16:55:47 -070024 "github.com/opencord/cordctl/cli/version"
25 "github.com/opencord/cordctl/format"
26)
27
28type VersionDetails struct {
29 Version string `json:"version"`
30 GoVersion string `json:"goversion"`
31 GitCommit string `json:"gitcommit"`
Scott Baker63ce82e2019-05-15 09:01:42 -070032 GitDirty string `json:"gitdirty"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070033 BuildTime string `json:"buildtime"`
34 Os string `json:"os"`
35 Arch string `json:"arch"`
36}
37
Scott Baker6cf525a2019-05-09 12:25:08 -070038type CoreVersionDetails struct {
39 Version string `json:"version"`
40 PythonVersion string `json:"goversion"`
41 GitCommit string `json:"gitcommit"`
42 BuildTime string `json:"buildtime"`
43 Os string `json:"os"`
44 Arch string `json:"arch"`
45}
46
Scott Baker2c0ebda2019-05-06 16:55:47 -070047type VersionOutput struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070048 Client VersionDetails `json:"client"`
49 Server CoreVersionDetails `json:"server"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070050}
51
52type VersionOpts struct {
53 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
54}
55
56var versionOpts = VersionOpts{}
57
58var versionInfo = VersionOutput{
59 Client: VersionDetails{
60 Version: version.Version,
61 GoVersion: version.GoVersion,
62 GitCommit: version.GitCommit,
Scott Baker63ce82e2019-05-15 09:01:42 -070063 GitDirty: version.GitDirty,
Scott Baker2c0ebda2019-05-06 16:55:47 -070064 Os: version.Os,
65 Arch: version.Arch,
66 BuildTime: version.BuildTime,
67 },
Scott Baker6cf525a2019-05-09 12:25:08 -070068 Server: CoreVersionDetails{
69 Version: "unknown",
70 PythonVersion: "unknown",
71 GitCommit: "unknown",
72 Os: "unknown",
73 Arch: "unknown",
74 BuildTime: "unknown",
75 },
Scott Baker2c0ebda2019-05-06 16:55:47 -070076}
77
78func RegisterVersionCommands(parent *flags.Parser) {
79 parent.AddCommand("version", "display version", "Display client version", &versionOpts)
80}
81
82const DefaultFormat = `Client:
Scott Baker6cf525a2019-05-09 12:25:08 -070083 Version {{.Client.Version}}
84 Go version: {{.Client.GoVersion}}
85 Git commit: {{.Client.GitCommit}}
Scott Baker63ce82e2019-05-15 09:01:42 -070086 Git dirty: {{.Client.GitDirty}}
Scott Baker6cf525a2019-05-09 12:25:08 -070087 Built: {{.Client.BuildTime}}
88 OS/Arch: {{.Client.Os}}/{{.Client.Arch}}
89
90Server:
91 Version {{.Server.Version}}
92 Python version: {{.Server.PythonVersion}}
93 Git commit: {{.Server.GitCommit}}
94 Built: {{.Server.BuildTime}}
95 OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
Scott Baker2c0ebda2019-05-06 16:55:47 -070096`
97
98func (options *VersionOpts) Execute(args []string) error {
Scott Baker6cf525a2019-05-09 12:25:08 -070099 conn, err := NewConnection()
100 if err != nil {
101 return err
102 }
103 defer conn.Close()
104
105 descriptor, method, err := GetReflectionMethod(conn, "xos.utility.GetVersion")
106 if err != nil {
107 return err
108 }
109
110 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
111 defer cancel()
112
113 headers := GenerateHeaders()
114
115 h := &RpcEventHandler{}
116 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams)
117 if err != nil {
118 return err
119 }
120
121 if h.Status != nil && h.Status.Err() != nil {
122 return h.Status.Err()
123 }
124
125 d, err := dynamic.AsDynamicMessage(h.Response)
126 if err != nil {
127 return err
128 }
129
130 versionInfo.Server.Version = d.GetFieldByName("version").(string)
131 versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
132 versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
133 versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
134 versionInfo.Server.Os = d.GetFieldByName("os").(string)
135 versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
136
Scott Baker2c0ebda2019-05-06 16:55:47 -0700137 result := CommandResult{
138 Format: format.Format(DefaultFormat),
139 OutputAs: toOutputType(options.OutputAs),
140 Data: versionInfo,
141 }
142
143 GenerateOutput(&result)
144 return nil
145}