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" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame^] | 21 | "github.com/golang/protobuf/ptypes/empty" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 22 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltctl/internal/pkg/cli/version" |
| 24 | "github.com/opencord/voltctl/pkg/format" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame^] | 25 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 26 | "strings" |
| 27 | ) |
| 28 | |
| 29 | type VersionDetails struct { |
| 30 | Version string `json:"version"` |
| 31 | GoVersion string `json:"goversion"` |
| 32 | VcsRef string `json:"gitcommit"` |
| 33 | VcsDirty string `json:"gitdirty"` |
| 34 | BuildTime string `json:"buildtime"` |
| 35 | Os string `json:"os"` |
| 36 | Arch string `json:"arch"` |
| 37 | } |
| 38 | |
| 39 | type VersionOutput struct { |
| 40 | Client VersionDetails `json:"client"` |
| 41 | Cluster VersionDetails `json:"cluster"` |
| 42 | } |
| 43 | |
| 44 | type VersionOpts struct { |
| 45 | OutputOptions |
| 46 | ClientOnly bool `long:"clientonly" description:"Display only client version information"` |
| 47 | } |
| 48 | |
| 49 | var versionOpts = VersionOpts{} |
| 50 | |
| 51 | var versionInfo = VersionOutput{ |
| 52 | Client: VersionDetails{ |
| 53 | Version: version.Version, |
| 54 | GoVersion: version.GoVersion, |
| 55 | VcsRef: version.VcsRef, |
| 56 | VcsDirty: version.VcsDirty, |
| 57 | Os: version.Os, |
| 58 | Arch: version.Arch, |
| 59 | BuildTime: version.BuildTime, |
| 60 | }, |
| 61 | Cluster: VersionDetails{ |
| 62 | Version: "unknown-version", |
| 63 | GoVersion: "unknown-goversion", |
| 64 | VcsRef: "unknown-vcsref", |
| 65 | VcsDirty: "unknown-vcsdirty", |
| 66 | Os: "unknown-os", |
| 67 | Arch: "unknown-arch", |
| 68 | BuildTime: "unknown-buildtime", |
| 69 | }, |
| 70 | } |
| 71 | |
| 72 | func RegisterVersionCommands(parent *flags.Parser) { |
| 73 | _, err := parent.AddCommand("version", "display version", "Display client and server version", &versionOpts) |
| 74 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 75 | Error.Fatalf("Unable to register version command: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | const ClientOnlyFormat = `Client: |
| 80 | Version {{.Version}} |
| 81 | Go version: {{.GoVersion}} |
| 82 | Vcs reference: {{.VcsRef}} |
| 83 | Vcs dirty: {{.VcsDirty}} |
| 84 | Built: {{.BuildTime}} |
| 85 | OS/Arch: {{.Os}}/{{.Arch}} |
| 86 | ` |
| 87 | |
| 88 | const DefaultFormat = `Client: |
| 89 | Version {{.Client.Version}} |
| 90 | Go version: {{.Client.GoVersion}} |
| 91 | Vcs reference: {{.Client.VcsRef}} |
| 92 | Vcs dirty: {{.Client.VcsDirty}} |
| 93 | Built: {{.Client.BuildTime}} |
| 94 | OS/Arch: {{.Client.Os}}/{{.Client.Arch}} |
| 95 | |
| 96 | Cluster: |
| 97 | Version {{.Cluster.Version}} |
| 98 | Go version: {{.Cluster.GoVersion}} |
| 99 | Vcs feference: {{.Cluster.VcsRef}} |
| 100 | Vcs dirty: {{.Cluster.VcsDirty}} |
| 101 | Built: {{.Cluster.BuildTime}} |
| 102 | OS/Arch: {{.Cluster.Os}}/{{.Cluster.Arch}} |
| 103 | ` |
| 104 | |
| 105 | func (options *VersionOpts) clientOnlyVersion(args []string) error { |
| 106 | outputFormat := CharReplacer.Replace(options.Format) |
| 107 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 108 | outputFormat = GetCommandOptionWithDefault("version", "format", ClientOnlyFormat) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 109 | } |
| 110 | if options.Quiet { |
| 111 | outputFormat = "{{.Version}}" |
| 112 | } |
| 113 | |
| 114 | result := CommandResult{ |
| 115 | Format: format.Format(outputFormat), |
| 116 | OutputAs: toOutputType(options.OutputAs), |
| 117 | NameLimit: options.NameLimit, |
| 118 | Data: versionInfo.Client, |
| 119 | } |
| 120 | |
| 121 | GenerateOutput(&result) |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | func (options *VersionOpts) Execute(args []string) error { |
| 126 | if options.ClientOnly { |
| 127 | return options.clientOnlyVersion(args) |
| 128 | } |
| 129 | |
| 130 | conn, err := NewConnection() |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | defer conn.Close() |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame^] | 135 | |
| 136 | client := voltha.NewVolthaServiceClient(conn) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 137 | |
| 138 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 139 | defer cancel() |
| 140 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame^] | 141 | voltha, err := client.GetVoltha(ctx, &empty.Empty{}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 142 | if err != nil { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame^] | 143 | return nil |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | info := make(map[string]interface{}) |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame^] | 147 | err = json.Unmarshal([]byte(voltha.Version), &info) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 148 | if err != nil { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame^] | 149 | versionInfo.Cluster.Version = strings.ReplaceAll(voltha.Version, "\n", "") |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 150 | } else { |
| 151 | var ok bool |
| 152 | if _, ok = info["version"]; ok { |
| 153 | versionInfo.Cluster.Version = info["version"].(string) |
| 154 | } else { |
| 155 | versionInfo.Cluster.Version = "unknown-version" |
| 156 | } |
| 157 | |
| 158 | if _, ok = info["goversion"]; ok { |
| 159 | versionInfo.Cluster.GoVersion = info["goversion"].(string) |
| 160 | } else { |
| 161 | versionInfo.Cluster.GoVersion = "unknown-goversion" |
| 162 | } |
| 163 | |
| 164 | if _, ok = info["vcsref"]; ok { |
| 165 | versionInfo.Cluster.VcsRef = info["vcsref"].(string) |
| 166 | } else { |
| 167 | versionInfo.Cluster.VcsRef = "unknown-vcsref" |
| 168 | } |
| 169 | |
| 170 | if _, ok = info["vcsdirty"]; ok { |
| 171 | versionInfo.Cluster.VcsDirty = info["vcsdirty"].(string) |
| 172 | } else { |
| 173 | versionInfo.Cluster.VcsDirty = "unknown-vcsdirty" |
| 174 | } |
| 175 | |
| 176 | if _, ok = info["buildtime"]; ok { |
| 177 | versionInfo.Cluster.BuildTime = info["buildtime"].(string) |
| 178 | } else { |
| 179 | versionInfo.Cluster.BuildTime = "unknown-buildtime" |
| 180 | } |
| 181 | |
| 182 | if _, ok = info["os"]; ok { |
| 183 | versionInfo.Cluster.Os = info["os"].(string) |
| 184 | } else { |
| 185 | versionInfo.Cluster.Os = "unknown-os" |
| 186 | } |
| 187 | |
| 188 | if _, ok = info["arch"]; ok { |
| 189 | versionInfo.Cluster.Arch = info["arch"].(string) |
| 190 | } else { |
| 191 | versionInfo.Cluster.Arch = "unknown-arch" |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | outputFormat := CharReplacer.Replace(options.Format) |
| 196 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 197 | outputFormat = GetCommandOptionWithDefault("version", "format", DefaultFormat) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 198 | } |
| 199 | if options.Quiet { |
| 200 | outputFormat = "{{.Client.Version}}" |
| 201 | } |
| 202 | |
| 203 | result := CommandResult{ |
| 204 | Format: format.Format(outputFormat), |
| 205 | OutputAs: toOutputType(options.OutputAs), |
| 206 | NameLimit: options.NameLimit, |
| 207 | Data: versionInfo, |
| 208 | } |
| 209 | |
| 210 | GenerateOutput(&result) |
| 211 | return nil |
| 212 | } |