blob: 38d2f7a4bb423bce666e488a01181256ac57fd86 [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 if !options.ClientOnly {
Scott Baker14c8f182019-05-22 18:05:29 -0700105 conn, descriptor, err := InitReflectionClient()
Matteo Scandolo722930f2019-05-21 15:55:31 -0700106 if err != nil {
107 return err
108 }
109 defer conn.Close()
110
Matteo Scandolo722930f2019-05-21 15:55:31 -0700111 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
112 defer cancel()
113
114 headers := GenerateHeaders()
115
116 h := &RpcEventHandler{}
Scott Baker14c8f182019-05-22 18:05:29 -0700117 err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetVersion", headers, h, h.GetParams)
Matteo Scandolo722930f2019-05-21 15:55:31 -0700118 if err != nil {
119 return err
120 }
121
122 if h.Status != nil && h.Status.Err() != nil {
123 return h.Status.Err()
124 }
125
126 d, err := dynamic.AsDynamicMessage(h.Response)
127 if err != nil {
128 return err
129 }
130
131 versionInfo.Server.Version = d.GetFieldByName("version").(string)
132 versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
133 versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
134 versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
135 versionInfo.Server.Os = d.GetFieldByName("os").(string)
136 versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
Scott Baker6cf525a2019-05-09 12:25:08 -0700137 }
Scott Baker6cf525a2019-05-09 12:25:08 -0700138
Scott Baker2c0ebda2019-05-06 16:55:47 -0700139 result := CommandResult{
Matteo Scandolo722930f2019-05-21 15:55:31 -0700140 // Format: format.Format(DefaultFormat),
Scott Baker2c0ebda2019-05-06 16:55:47 -0700141 OutputAs: toOutputType(options.OutputAs),
142 Data: versionInfo,
143 }
144
Matteo Scandolo722930f2019-05-21 15:55:31 -0700145 if options.ClientOnly {
146 result.Format = format.Format(ClientFormat)
147 } else {
148 result.Format = format.Format(DefaultFormat)
149 }
150
Scott Baker2c0ebda2019-05-06 16:55:47 -0700151 GenerateOutput(&result)
152 return nil
153}