blob: 1941589809c758100f2a29ea8aeb20a54b2b80dc [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"
Matteo Scandolo722930f2019-05-21 15:55:31 -070021
Scott Baker6cf525a2019-05-09 12:25:08 -070022 "github.com/fullstorydev/grpcurl"
Scott Baker2c0ebda2019-05-06 16:55:47 -070023 flags "github.com/jessevdk/go-flags"
Scott Baker6cf525a2019-05-09 12:25:08 -070024 "github.com/jhump/protoreflect/dynamic"
Scott Baker2c0ebda2019-05-06 16:55:47 -070025 "github.com/opencord/cordctl/cli/version"
26 "github.com/opencord/cordctl/format"
27)
28
29type VersionDetails struct {
30 Version string `json:"version"`
31 GoVersion string `json:"goversion"`
32 GitCommit string `json:"gitcommit"`
Scott Baker63ce82e2019-05-15 09:01:42 -070033 GitDirty string `json:"gitdirty"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070034 BuildTime string `json:"buildtime"`
35 Os string `json:"os"`
36 Arch string `json:"arch"`
37}
38
Scott Baker6cf525a2019-05-09 12:25:08 -070039type CoreVersionDetails struct {
40 Version string `json:"version"`
41 PythonVersion string `json:"goversion"`
42 GitCommit string `json:"gitcommit"`
43 BuildTime string `json:"buildtime"`
44 Os string `json:"os"`
45 Arch string `json:"arch"`
46}
47
Scott Baker2c0ebda2019-05-06 16:55:47 -070048type VersionOutput struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070049 Client VersionDetails `json:"client"`
50 Server CoreVersionDetails `json:"server"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070051}
52
53type VersionOpts struct {
Matteo Scandolo722930f2019-05-21 15:55:31 -070054 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
55 ClientOnly bool `short:"c" long:"client-only" description:"Print only client version"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070056}
57
58var versionOpts = VersionOpts{}
59
60var versionInfo = VersionOutput{
61 Client: VersionDetails{
62 Version: version.Version,
63 GoVersion: version.GoVersion,
64 GitCommit: version.GitCommit,
Scott Baker63ce82e2019-05-15 09:01:42 -070065 GitDirty: version.GitDirty,
Scott Baker2c0ebda2019-05-06 16:55:47 -070066 Os: version.Os,
67 Arch: version.Arch,
68 BuildTime: version.BuildTime,
69 },
Scott Baker6cf525a2019-05-09 12:25:08 -070070 Server: CoreVersionDetails{
71 Version: "unknown",
72 PythonVersion: "unknown",
73 GitCommit: "unknown",
74 Os: "unknown",
75 Arch: "unknown",
76 BuildTime: "unknown",
77 },
Scott Baker2c0ebda2019-05-06 16:55:47 -070078}
79
80func RegisterVersionCommands(parent *flags.Parser) {
81 parent.AddCommand("version", "display version", "Display client version", &versionOpts)
82}
83
Matteo Scandolo722930f2019-05-21 15:55:31 -070084const ClientFormat = `Client:
Scott Baker6cf525a2019-05-09 12:25:08 -070085 Version {{.Client.Version}}
86 Go version: {{.Client.GoVersion}}
87 Git commit: {{.Client.GitCommit}}
Scott Baker63ce82e2019-05-15 09:01:42 -070088 Git dirty: {{.Client.GitDirty}}
Scott Baker6cf525a2019-05-09 12:25:08 -070089 Built: {{.Client.BuildTime}}
90 OS/Arch: {{.Client.Os}}/{{.Client.Arch}}
Matteo Scandolo722930f2019-05-21 15:55:31 -070091`
92const ServerFormat = `
Scott Baker6cf525a2019-05-09 12:25:08 -070093Server:
94 Version {{.Server.Version}}
95 Python version: {{.Server.PythonVersion}}
96 Git commit: {{.Server.GitCommit}}
97 Built: {{.Server.BuildTime}}
98 OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
Scott Baker2c0ebda2019-05-06 16:55:47 -070099`
100
Matteo Scandolo722930f2019-05-21 15:55:31 -0700101const DefaultFormat = ClientFormat + ServerFormat
102
Scott Baker2c0ebda2019-05-06 16:55:47 -0700103func (options *VersionOpts) Execute(args []string) error {
Matteo Scandolo722930f2019-05-21 15:55:31 -0700104
105 if !options.ClientOnly {
106 conn, err := NewConnection()
107 if err != nil {
108 return err
109 }
110 defer conn.Close()
111
112 descriptor, method, err := GetReflectionMethod(conn, "xos.utility.GetVersion")
113 if err != nil {
114 return err
115 }
116
117 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
118 defer cancel()
119
120 headers := GenerateHeaders()
121
122 h := &RpcEventHandler{}
123 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams)
124 if err != nil {
125 return err
126 }
127
128 if h.Status != nil && h.Status.Err() != nil {
129 return h.Status.Err()
130 }
131
132 d, err := dynamic.AsDynamicMessage(h.Response)
133 if err != nil {
134 return err
135 }
136
137 versionInfo.Server.Version = d.GetFieldByName("version").(string)
138 versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
139 versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
140 versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
141 versionInfo.Server.Os = d.GetFieldByName("os").(string)
142 versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
Scott Baker6cf525a2019-05-09 12:25:08 -0700143 }
Scott Baker6cf525a2019-05-09 12:25:08 -0700144
Scott Baker2c0ebda2019-05-06 16:55:47 -0700145 result := CommandResult{
Matteo Scandolo722930f2019-05-21 15:55:31 -0700146 // Format: format.Format(DefaultFormat),
Scott Baker2c0ebda2019-05-06 16:55:47 -0700147 OutputAs: toOutputType(options.OutputAs),
148 Data: versionInfo,
149 }
150
Matteo Scandolo722930f2019-05-21 15:55:31 -0700151 if options.ClientOnly {
152 result.Format = format.Format(ClientFormat)
153 } else {
154 result.Format = format.Format(DefaultFormat)
155 }
156
Scott Baker2c0ebda2019-05-06 16:55:47 -0700157 GenerateOutput(&result)
158 return nil
159}