Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | flags "github.com/jessevdk/go-flags" |
| 21 | "github.com/opencord/cordctl/cli/version" |
| 22 | "github.com/opencord/cordctl/format" |
| 23 | ) |
| 24 | |
| 25 | type VersionDetails struct { |
| 26 | Version string `json:"version"` |
| 27 | GoVersion string `json:"goversion"` |
| 28 | GitCommit string `json:"gitcommit"` |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 29 | GitDirty string `json:"gitdirty"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 30 | BuildTime string `json:"buildtime"` |
| 31 | Os string `json:"os"` |
| 32 | Arch string `json:"arch"` |
| 33 | } |
| 34 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 35 | type 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 Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame] | 42 | DjangoVersion string `json:"djangoversion"` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 45 | type VersionOutput struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 46 | Client VersionDetails `json:"client"` |
| 47 | Server CoreVersionDetails `json:"server"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | type VersionOpts struct { |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 51 | 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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | var versionOpts = VersionOpts{} |
| 56 | |
| 57 | var versionInfo = VersionOutput{ |
| 58 | Client: VersionDetails{ |
| 59 | Version: version.Version, |
| 60 | GoVersion: version.GoVersion, |
| 61 | GitCommit: version.GitCommit, |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 62 | GitDirty: version.GitDirty, |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 63 | Os: version.Os, |
| 64 | Arch: version.Arch, |
| 65 | BuildTime: version.BuildTime, |
| 66 | }, |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 67 | Server: CoreVersionDetails{ |
| 68 | Version: "unknown", |
| 69 | PythonVersion: "unknown", |
| 70 | GitCommit: "unknown", |
| 71 | Os: "unknown", |
| 72 | Arch: "unknown", |
| 73 | BuildTime: "unknown", |
Scott Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame] | 74 | DjangoVersion: "unknown", |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 75 | }, |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func RegisterVersionCommands(parent *flags.Parser) { |
| 79 | parent.AddCommand("version", "display version", "Display client version", &versionOpts) |
| 80 | } |
| 81 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 82 | const ClientFormat = `Client: |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 83 | Version {{.Client.Version}} |
| 84 | Go version: {{.Client.GoVersion}} |
| 85 | Git commit: {{.Client.GitCommit}} |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 86 | Git dirty: {{.Client.GitDirty}} |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 87 | Built: {{.Client.BuildTime}} |
| 88 | OS/Arch: {{.Client.Os}}/{{.Client.Arch}} |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 89 | ` |
| 90 | const ServerFormat = ` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 91 | Server: |
| 92 | Version {{.Server.Version}} |
| 93 | Python version: {{.Server.PythonVersion}} |
Scott Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame] | 94 | Django version: {{.Server.DjangoVersion}} |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 95 | Git commit: {{.Server.GitCommit}} |
| 96 | Built: {{.Server.BuildTime}} |
| 97 | OS/Arch: {{.Server.Os}}/{{.Server.Arch}} |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 98 | ` |
| 99 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 100 | const DefaultFormat = ClientFormat + ServerFormat |
| 101 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 102 | func (options *VersionOpts) Execute(args []string) error { |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 103 | if !options.ClientOnly { |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 104 | conn, descriptor, err := InitClient(INIT_NO_VERSION_CHECK) |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | defer conn.Close() |
| 109 | |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 110 | d, err := GetVersion(conn, descriptor) |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 111 | 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 Baker | 87efa39 | 2019-06-18 17:09:46 -0700 | [diff] [blame] | 121 | |
| 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 127 | } |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 128 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 129 | result := CommandResult{ |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 130 | // Format: format.Format(DefaultFormat), |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 131 | OutputAs: toOutputType(options.OutputAs), |
| 132 | Data: versionInfo, |
| 133 | } |
| 134 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 135 | if options.ClientOnly { |
| 136 | result.Format = format.Format(ClientFormat) |
| 137 | } else { |
| 138 | result.Format = format.Format(DefaultFormat) |
| 139 | } |
| 140 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 141 | GenerateOutput(&result) |
| 142 | return nil |
| 143 | } |