Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Ciena Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "encoding/json" |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 21 | "strings" |
| 22 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 23 | "github.com/golang/protobuf/ptypes/empty" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 24 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltctl/internal/pkg/cli/version" |
| 26 | "github.com/opencord/voltctl/pkg/format" |
kesavand | 8ec4fc0 | 2021-01-27 09:10:22 -0500 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type VersionDetails struct { |
| 31 | Version string `json:"version"` |
| 32 | GoVersion string `json:"goversion"` |
| 33 | VcsRef string `json:"gitcommit"` |
| 34 | VcsDirty string `json:"gitdirty"` |
| 35 | BuildTime string `json:"buildtime"` |
| 36 | Os string `json:"os"` |
| 37 | Arch string `json:"arch"` |
| 38 | } |
| 39 | |
| 40 | type VersionOutput struct { |
| 41 | Client VersionDetails `json:"client"` |
| 42 | Cluster VersionDetails `json:"cluster"` |
| 43 | } |
| 44 | |
| 45 | type VersionOpts struct { |
| 46 | OutputOptions |
| 47 | ClientOnly bool `long:"clientonly" description:"Display only client version information"` |
| 48 | } |
| 49 | |
| 50 | var versionOpts = VersionOpts{} |
| 51 | |
| 52 | var versionInfo = VersionOutput{ |
| 53 | Client: VersionDetails{ |
| 54 | Version: version.Version, |
| 55 | GoVersion: version.GoVersion, |
| 56 | VcsRef: version.VcsRef, |
| 57 | VcsDirty: version.VcsDirty, |
| 58 | Os: version.Os, |
| 59 | Arch: version.Arch, |
| 60 | BuildTime: version.BuildTime, |
| 61 | }, |
| 62 | Cluster: VersionDetails{ |
| 63 | Version: "unknown-version", |
| 64 | GoVersion: "unknown-goversion", |
| 65 | VcsRef: "unknown-vcsref", |
| 66 | VcsDirty: "unknown-vcsdirty", |
| 67 | Os: "unknown-os", |
| 68 | Arch: "unknown-arch", |
| 69 | BuildTime: "unknown-buildtime", |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | func RegisterVersionCommands(parent *flags.Parser) { |
| 74 | _, err := parent.AddCommand("version", "display version", "Display client and server version", &versionOpts) |
| 75 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 76 | Error.Fatalf("Unable to register version command: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | const ClientOnlyFormat = `Client: |
| 81 | Version {{.Version}} |
| 82 | Go version: {{.GoVersion}} |
| 83 | Vcs reference: {{.VcsRef}} |
| 84 | Vcs dirty: {{.VcsDirty}} |
| 85 | Built: {{.BuildTime}} |
| 86 | OS/Arch: {{.Os}}/{{.Arch}} |
| 87 | ` |
| 88 | |
| 89 | const DefaultFormat = `Client: |
| 90 | Version {{.Client.Version}} |
| 91 | Go version: {{.Client.GoVersion}} |
| 92 | Vcs reference: {{.Client.VcsRef}} |
| 93 | Vcs dirty: {{.Client.VcsDirty}} |
| 94 | Built: {{.Client.BuildTime}} |
| 95 | OS/Arch: {{.Client.Os}}/{{.Client.Arch}} |
| 96 | |
| 97 | Cluster: |
| 98 | Version {{.Cluster.Version}} |
| 99 | Go version: {{.Cluster.GoVersion}} |
| 100 | Vcs feference: {{.Cluster.VcsRef}} |
| 101 | Vcs dirty: {{.Cluster.VcsDirty}} |
| 102 | Built: {{.Cluster.BuildTime}} |
| 103 | OS/Arch: {{.Cluster.Os}}/{{.Cluster.Arch}} |
| 104 | ` |
| 105 | |
| 106 | func (options *VersionOpts) clientOnlyVersion(args []string) error { |
| 107 | outputFormat := CharReplacer.Replace(options.Format) |
| 108 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 109 | outputFormat = GetCommandOptionWithDefault("version", "format", ClientOnlyFormat) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 110 | } |
| 111 | if options.Quiet { |
| 112 | outputFormat = "{{.Version}}" |
| 113 | } |
| 114 | |
| 115 | result := CommandResult{ |
| 116 | Format: format.Format(outputFormat), |
| 117 | OutputAs: toOutputType(options.OutputAs), |
| 118 | NameLimit: options.NameLimit, |
| 119 | Data: versionInfo.Client, |
| 120 | } |
| 121 | |
| 122 | GenerateOutput(&result) |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | func (options *VersionOpts) Execute(args []string) error { |
| 127 | if options.ClientOnly { |
| 128 | return options.clientOnlyVersion(args) |
| 129 | } |
| 130 | |
| 131 | conn, err := NewConnection() |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | defer conn.Close() |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 136 | |
| 137 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 138 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 139 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 140 | defer cancel() |
| 141 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 142 | voltha, err := client.GetVoltha(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 143 | if err != nil { |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 144 | return err |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | info := make(map[string]interface{}) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 148 | err = json.Unmarshal([]byte(voltha.Version), &info) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 149 | if err != nil { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 150 | versionInfo.Cluster.Version = strings.ReplaceAll(voltha.Version, "\n", "") |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 151 | } else { |
| 152 | var ok bool |
| 153 | if _, ok = info["version"]; ok { |
| 154 | versionInfo.Cluster.Version = info["version"].(string) |
| 155 | } else { |
| 156 | versionInfo.Cluster.Version = "unknown-version" |
| 157 | } |
| 158 | |
| 159 | if _, ok = info["goversion"]; ok { |
| 160 | versionInfo.Cluster.GoVersion = info["goversion"].(string) |
| 161 | } else { |
| 162 | versionInfo.Cluster.GoVersion = "unknown-goversion" |
| 163 | } |
| 164 | |
| 165 | if _, ok = info["vcsref"]; ok { |
| 166 | versionInfo.Cluster.VcsRef = info["vcsref"].(string) |
| 167 | } else { |
| 168 | versionInfo.Cluster.VcsRef = "unknown-vcsref" |
| 169 | } |
| 170 | |
| 171 | if _, ok = info["vcsdirty"]; ok { |
| 172 | versionInfo.Cluster.VcsDirty = info["vcsdirty"].(string) |
| 173 | } else { |
| 174 | versionInfo.Cluster.VcsDirty = "unknown-vcsdirty" |
| 175 | } |
| 176 | |
| 177 | if _, ok = info["buildtime"]; ok { |
| 178 | versionInfo.Cluster.BuildTime = info["buildtime"].(string) |
| 179 | } else { |
| 180 | versionInfo.Cluster.BuildTime = "unknown-buildtime" |
| 181 | } |
| 182 | |
| 183 | if _, ok = info["os"]; ok { |
| 184 | versionInfo.Cluster.Os = info["os"].(string) |
| 185 | } else { |
| 186 | versionInfo.Cluster.Os = "unknown-os" |
| 187 | } |
| 188 | |
| 189 | if _, ok = info["arch"]; ok { |
| 190 | versionInfo.Cluster.Arch = info["arch"].(string) |
| 191 | } else { |
| 192 | versionInfo.Cluster.Arch = "unknown-arch" |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | outputFormat := CharReplacer.Replace(options.Format) |
| 197 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 198 | outputFormat = GetCommandOptionWithDefault("version", "format", DefaultFormat) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 199 | } |
| 200 | if options.Quiet { |
| 201 | outputFormat = "{{.Client.Version}}" |
| 202 | } |
| 203 | |
| 204 | result := CommandResult{ |
| 205 | Format: format.Format(outputFormat), |
| 206 | OutputAs: toOutputType(options.OutputAs), |
| 207 | NameLimit: options.NameLimit, |
| 208 | Data: versionInfo, |
| 209 | } |
| 210 | |
| 211 | GenerateOutput(&result) |
| 212 | return nil |
| 213 | } |