blob: 9cc8b05b99bbfcf3e7fdcc8aebe63f567773d2d2 [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"`
32 BuildTime string `json:"buildtime"`
33 Os string `json:"os"`
34 Arch string `json:"arch"`
35}
36
Scott Baker6cf525a2019-05-09 12:25:08 -070037type CoreVersionDetails struct {
38 Version string `json:"version"`
39 PythonVersion string `json:"goversion"`
40 GitCommit string `json:"gitcommit"`
41 BuildTime string `json:"buildtime"`
42 Os string `json:"os"`
43 Arch string `json:"arch"`
44}
45
Scott Baker2c0ebda2019-05-06 16:55:47 -070046type VersionOutput struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070047 Client VersionDetails `json:"client"`
48 Server CoreVersionDetails `json:"server"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070049}
50
51type VersionOpts struct {
52 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
53}
54
55var versionOpts = VersionOpts{}
56
57var versionInfo = VersionOutput{
58 Client: VersionDetails{
59 Version: version.Version,
60 GoVersion: version.GoVersion,
61 GitCommit: version.GitCommit,
62 Os: version.Os,
63 Arch: version.Arch,
64 BuildTime: version.BuildTime,
65 },
Scott Baker6cf525a2019-05-09 12:25:08 -070066 Server: CoreVersionDetails{
67 Version: "unknown",
68 PythonVersion: "unknown",
69 GitCommit: "unknown",
70 Os: "unknown",
71 Arch: "unknown",
72 BuildTime: "unknown",
73 },
Scott Baker2c0ebda2019-05-06 16:55:47 -070074}
75
76func RegisterVersionCommands(parent *flags.Parser) {
77 parent.AddCommand("version", "display version", "Display client version", &versionOpts)
78}
79
80const DefaultFormat = `Client:
Scott Baker6cf525a2019-05-09 12:25:08 -070081 Version {{.Client.Version}}
82 Go version: {{.Client.GoVersion}}
83 Git commit: {{.Client.GitCommit}}
84 Built: {{.Client.BuildTime}}
85 OS/Arch: {{.Client.Os}}/{{.Client.Arch}}
86
87Server:
88 Version {{.Server.Version}}
89 Python version: {{.Server.PythonVersion}}
90 Git commit: {{.Server.GitCommit}}
91 Built: {{.Server.BuildTime}}
92 OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
Scott Baker2c0ebda2019-05-06 16:55:47 -070093`
94
95func (options *VersionOpts) Execute(args []string) error {
Scott Baker6cf525a2019-05-09 12:25:08 -070096 conn, err := NewConnection()
97 if err != nil {
98 return err
99 }
100 defer conn.Close()
101
102 descriptor, method, err := GetReflectionMethod(conn, "xos.utility.GetVersion")
103 if err != nil {
104 return err
105 }
106
107 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
108 defer cancel()
109
110 headers := GenerateHeaders()
111
112 h := &RpcEventHandler{}
113 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams)
114 if err != nil {
115 return err
116 }
117
118 if h.Status != nil && h.Status.Err() != nil {
119 return h.Status.Err()
120 }
121
122 d, err := dynamic.AsDynamicMessage(h.Response)
123 if err != nil {
124 return err
125 }
126
127 versionInfo.Server.Version = d.GetFieldByName("version").(string)
128 versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
129 versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
130 versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
131 versionInfo.Server.Os = d.GetFieldByName("os").(string)
132 versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
133
Scott Baker2c0ebda2019-05-06 16:55:47 -0700134 result := CommandResult{
135 Format: format.Format(DefaultFormat),
136 OutputAs: toOutputType(options.OutputAs),
137 Data: versionInfo,
138 }
139
140 GenerateOutput(&result)
141 return nil
142}