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