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" |
| 21 | "github.com/fullstorydev/grpcurl" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 22 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 23 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 24 | "github.com/opencord/cordctl/cli/version" |
| 25 | "github.com/opencord/cordctl/format" |
| 26 | ) |
| 27 | |
| 28 | type VersionDetails struct { |
| 29 | Version string `json:"version"` |
| 30 | GoVersion string `json:"goversion"` |
| 31 | GitCommit string `json:"gitcommit"` |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 32 | GitDirty string `json:"gitdirty"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 33 | BuildTime string `json:"buildtime"` |
| 34 | Os string `json:"os"` |
| 35 | Arch string `json:"arch"` |
| 36 | } |
| 37 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 38 | type CoreVersionDetails struct { |
| 39 | Version string `json:"version"` |
| 40 | PythonVersion string `json:"goversion"` |
| 41 | GitCommit string `json:"gitcommit"` |
| 42 | BuildTime string `json:"buildtime"` |
| 43 | Os string `json:"os"` |
| 44 | Arch string `json:"arch"` |
| 45 | } |
| 46 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 47 | type VersionOutput struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 48 | Client VersionDetails `json:"client"` |
| 49 | Server CoreVersionDetails `json:"server"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | type VersionOpts struct { |
| 53 | OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 54 | } |
| 55 | |
| 56 | var versionOpts = VersionOpts{} |
| 57 | |
| 58 | var versionInfo = VersionOutput{ |
| 59 | Client: VersionDetails{ |
| 60 | Version: version.Version, |
| 61 | GoVersion: version.GoVersion, |
| 62 | GitCommit: version.GitCommit, |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 63 | GitDirty: version.GitDirty, |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 64 | Os: version.Os, |
| 65 | Arch: version.Arch, |
| 66 | BuildTime: version.BuildTime, |
| 67 | }, |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 68 | Server: CoreVersionDetails{ |
| 69 | Version: "unknown", |
| 70 | PythonVersion: "unknown", |
| 71 | GitCommit: "unknown", |
| 72 | Os: "unknown", |
| 73 | Arch: "unknown", |
| 74 | BuildTime: "unknown", |
| 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 | |
| 82 | const DefaultFormat = `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}} |
| 89 | |
| 90 | Server: |
| 91 | Version {{.Server.Version}} |
| 92 | Python version: {{.Server.PythonVersion}} |
| 93 | Git commit: {{.Server.GitCommit}} |
| 94 | Built: {{.Server.BuildTime}} |
| 95 | OS/Arch: {{.Server.Os}}/{{.Server.Arch}} |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 96 | ` |
| 97 | |
| 98 | func (options *VersionOpts) Execute(args []string) error { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 99 | conn, err := NewConnection() |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | defer conn.Close() |
| 104 | |
| 105 | descriptor, method, err := GetReflectionMethod(conn, "xos.utility.GetVersion") |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | |
| 110 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 111 | defer cancel() |
| 112 | |
| 113 | headers := GenerateHeaders() |
| 114 | |
| 115 | h := &RpcEventHandler{} |
| 116 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams) |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | if h.Status != nil && h.Status.Err() != nil { |
| 122 | return h.Status.Err() |
| 123 | } |
| 124 | |
| 125 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | |
| 130 | versionInfo.Server.Version = d.GetFieldByName("version").(string) |
| 131 | versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string) |
| 132 | versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string) |
| 133 | versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string) |
| 134 | versionInfo.Server.Os = d.GetFieldByName("os").(string) |
| 135 | versionInfo.Server.Arch = d.GetFieldByName("arch").(string) |
| 136 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 137 | result := CommandResult{ |
| 138 | Format: format.Format(DefaultFormat), |
| 139 | OutputAs: toOutputType(options.OutputAs), |
| 140 | Data: versionInfo, |
| 141 | } |
| 142 | |
| 143 | GenerateOutput(&result) |
| 144 | return nil |
| 145 | } |