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 ( |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 21 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 22 | "github.com/fullstorydev/grpcurl" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 23 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 24 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 25 | "github.com/opencord/cordctl/cli/version" |
| 26 | "github.com/opencord/cordctl/format" |
| 27 | ) |
| 28 | |
| 29 | type VersionDetails struct { |
| 30 | Version string `json:"version"` |
| 31 | GoVersion string `json:"goversion"` |
| 32 | GitCommit string `json:"gitcommit"` |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 33 | GitDirty string `json:"gitdirty"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 34 | BuildTime string `json:"buildtime"` |
| 35 | Os string `json:"os"` |
| 36 | Arch string `json:"arch"` |
| 37 | } |
| 38 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 39 | type 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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 48 | type VersionOutput struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 49 | Client VersionDetails `json:"client"` |
| 50 | Server CoreVersionDetails `json:"server"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | type VersionOpts struct { |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 54 | 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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | var versionOpts = VersionOpts{} |
| 59 | |
| 60 | var versionInfo = VersionOutput{ |
| 61 | Client: VersionDetails{ |
| 62 | Version: version.Version, |
| 63 | GoVersion: version.GoVersion, |
| 64 | GitCommit: version.GitCommit, |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 65 | GitDirty: version.GitDirty, |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 66 | Os: version.Os, |
| 67 | Arch: version.Arch, |
| 68 | BuildTime: version.BuildTime, |
| 69 | }, |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 70 | Server: CoreVersionDetails{ |
| 71 | Version: "unknown", |
| 72 | PythonVersion: "unknown", |
| 73 | GitCommit: "unknown", |
| 74 | Os: "unknown", |
| 75 | Arch: "unknown", |
| 76 | BuildTime: "unknown", |
| 77 | }, |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func RegisterVersionCommands(parent *flags.Parser) { |
| 81 | parent.AddCommand("version", "display version", "Display client version", &versionOpts) |
| 82 | } |
| 83 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 84 | const ClientFormat = `Client: |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 85 | Version {{.Client.Version}} |
| 86 | Go version: {{.Client.GoVersion}} |
| 87 | Git commit: {{.Client.GitCommit}} |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 88 | Git dirty: {{.Client.GitDirty}} |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 89 | Built: {{.Client.BuildTime}} |
| 90 | OS/Arch: {{.Client.Os}}/{{.Client.Arch}} |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 91 | ` |
| 92 | const ServerFormat = ` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 93 | Server: |
| 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 Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 99 | ` |
| 100 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 101 | const DefaultFormat = ClientFormat + ServerFormat |
| 102 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 103 | func (options *VersionOpts) Execute(args []string) error { |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 104 | if !options.ClientOnly { |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 105 | conn, descriptor, err := InitReflectionClient() |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | defer conn.Close() |
| 110 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 111 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 112 | defer cancel() |
| 113 | |
| 114 | headers := GenerateHeaders() |
| 115 | |
| 116 | h := &RpcEventHandler{} |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 117 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetVersion", headers, h, h.GetParams) |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 118 | 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 137 | } |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 138 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 139 | result := CommandResult{ |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 140 | // Format: format.Format(DefaultFormat), |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 141 | OutputAs: toOutputType(options.OutputAs), |
| 142 | Data: versionInfo, |
| 143 | } |
| 144 | |
Matteo Scandolo | 722930f | 2019-05-21 15:55:31 -0700 | [diff] [blame] | 145 | if options.ClientOnly { |
| 146 | result.Format = format.Format(ClientFormat) |
| 147 | } else { |
| 148 | result.Format = format.Format(DefaultFormat) |
| 149 | } |
| 150 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 151 | GenerateOutput(&result) |
| 152 | return nil |
| 153 | } |