blob: d650c595bd2e1cfdf768d4b4c7309f0a36980f62 [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 (
20 flags "github.com/jessevdk/go-flags"
21 "github.com/opencord/cordctl/cli/version"
22 "github.com/opencord/cordctl/format"
23)
24
25type VersionDetails struct {
26 Version string `json:"version"`
27 GoVersion string `json:"goversion"`
28 GitCommit string `json:"gitcommit"`
Scott Baker63ce82e2019-05-15 09:01:42 -070029 GitDirty string `json:"gitdirty"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070030 BuildTime string `json:"buildtime"`
31 Os string `json:"os"`
32 Arch string `json:"arch"`
33}
34
Scott Baker6cf525a2019-05-09 12:25:08 -070035type CoreVersionDetails struct {
36 Version string `json:"version"`
37 PythonVersion string `json:"goversion"`
38 GitCommit string `json:"gitcommit"`
39 BuildTime string `json:"buildtime"`
40 Os string `json:"os"`
41 Arch string `json:"arch"`
Scott Baker87efa392019-06-18 17:09:46 -070042 DjangoVersion string `json:"djangoversion"`
Scott Baker6cf525a2019-05-09 12:25:08 -070043}
44
Scott Baker2c0ebda2019-05-06 16:55:47 -070045type VersionOutput struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070046 Client VersionDetails `json:"client"`
47 Server CoreVersionDetails `json:"server"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070048}
49
50type VersionOpts struct {
Matteo Scandolo722930f2019-05-21 15:55:31 -070051 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
52 ClientOnly bool `short:"c" long:"client-only" description:"Print only client version"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070053}
54
55var versionOpts = VersionOpts{}
56
57var versionInfo = VersionOutput{
58 Client: VersionDetails{
59 Version: version.Version,
60 GoVersion: version.GoVersion,
61 GitCommit: version.GitCommit,
Scott Baker63ce82e2019-05-15 09:01:42 -070062 GitDirty: version.GitDirty,
Scott Baker2c0ebda2019-05-06 16:55:47 -070063 Os: version.Os,
64 Arch: version.Arch,
65 BuildTime: version.BuildTime,
66 },
Scott Baker6cf525a2019-05-09 12:25:08 -070067 Server: CoreVersionDetails{
68 Version: "unknown",
69 PythonVersion: "unknown",
70 GitCommit: "unknown",
71 Os: "unknown",
72 Arch: "unknown",
73 BuildTime: "unknown",
Scott Baker87efa392019-06-18 17:09:46 -070074 DjangoVersion: "unknown",
Scott Baker6cf525a2019-05-09 12:25:08 -070075 },
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
Matteo Scandolo722930f2019-05-21 15:55:31 -070082const ClientFormat = `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}}
Matteo Scandolo722930f2019-05-21 15:55:31 -070089`
90const ServerFormat = `
Scott Baker6cf525a2019-05-09 12:25:08 -070091Server:
92 Version {{.Server.Version}}
93 Python version: {{.Server.PythonVersion}}
Scott Baker87efa392019-06-18 17:09:46 -070094 Django version: {{.Server.DjangoVersion}}
Scott Baker6cf525a2019-05-09 12:25:08 -070095 Git commit: {{.Server.GitCommit}}
96 Built: {{.Server.BuildTime}}
97 OS/Arch: {{.Server.Os}}/{{.Server.Arch}}
Scott Baker2c0ebda2019-05-06 16:55:47 -070098`
99
Matteo Scandolo722930f2019-05-21 15:55:31 -0700100const DefaultFormat = ClientFormat + ServerFormat
101
Scott Baker2c0ebda2019-05-06 16:55:47 -0700102func (options *VersionOpts) Execute(args []string) error {
Matteo Scandolo722930f2019-05-21 15:55:31 -0700103 if !options.ClientOnly {
Scott Baker867aa302019-06-19 13:18:45 -0700104 conn, descriptor, err := InitClient(INIT_NO_VERSION_CHECK)
Matteo Scandolo722930f2019-05-21 15:55:31 -0700105 if err != nil {
106 return err
107 }
108 defer conn.Close()
109
Scott Baker867aa302019-06-19 13:18:45 -0700110 d, err := GetVersion(conn, descriptor)
Matteo Scandolo722930f2019-05-21 15:55:31 -0700111 if err != nil {
112 return err
113 }
114
115 versionInfo.Server.Version = d.GetFieldByName("version").(string)
116 versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
117 versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
118 versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
119 versionInfo.Server.Os = d.GetFieldByName("os").(string)
120 versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
Scott Baker87efa392019-06-18 17:09:46 -0700121
122 // djangoVersion was added to GetVersion() in xos-core 3.3.1-dev
123 djangoVersion, err := d.TryGetFieldByName("djangoVersion")
124 if err == nil {
125 versionInfo.Server.DjangoVersion = djangoVersion.(string)
126 }
Scott Baker6cf525a2019-05-09 12:25:08 -0700127 }
Scott Baker6cf525a2019-05-09 12:25:08 -0700128
Scott Baker2c0ebda2019-05-06 16:55:47 -0700129 result := CommandResult{
Matteo Scandolo722930f2019-05-21 15:55:31 -0700130 // Format: format.Format(DefaultFormat),
Scott Baker2c0ebda2019-05-06 16:55:47 -0700131 OutputAs: toOutputType(options.OutputAs),
132 Data: versionInfo,
133 }
134
Matteo Scandolo722930f2019-05-21 15:55:31 -0700135 if options.ClientOnly {
136 result.Format = format.Format(ClientFormat)
137 } else {
138 result.Format = format.Format(DefaultFormat)
139 }
140
Scott Baker2c0ebda2019-05-06 16:55:47 -0700141 GenerateOutput(&result)
142 return nil
143}