github.com/fafucoder/cilium@v1.6.11/cilium/cmd/version.go (about) 1 // Copyright 2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "fmt" 19 "os" 20 21 "github.com/cilium/cilium/pkg/command" 22 "github.com/cilium/cilium/pkg/version" 23 24 "github.com/spf13/cobra" 25 ) 26 27 const notResponding = "Not responding" 28 29 var versionCmd = &cobra.Command{ 30 Use: "version", 31 Short: "Print version information", 32 Run: func(cmd *cobra.Command, args []string) { 33 getVersion(cmd, args) 34 }, 35 } 36 37 func init() { 38 rootCmd.AddCommand(versionCmd) 39 command.AddJSONOutput(versionCmd) 40 } 41 42 func getVersion(cmd *cobra.Command, args []string) { 43 // -o argument is set 44 if command.OutputJSON() { 45 data := struct { 46 Client version.CiliumVersion 47 Daemon version.CiliumVersion 48 }{ 49 getClientVersionAsStruct(), 50 getDaemonVersionAsStruct(), 51 } 52 if err := command.PrintOutput(data); err != nil { 53 os.Exit(1) 54 } 55 return 56 } 57 // default output 58 fmt.Printf("Client: %s\n", getClientVersionAsString()) 59 fmt.Printf("Daemon: %s\n", getDaemonVersionAsString()) 60 } 61 62 func getClientVersionAsString() string { 63 return version.Version 64 } 65 66 func getDaemonVersionAsString() string { 67 resp, err := client.Daemon.GetDebuginfo(nil) 68 if err != nil { 69 return notResponding 70 } 71 return resp.Payload.CiliumVersion 72 } 73 74 func getClientVersionAsStruct() version.CiliumVersion { 75 return version.GetCiliumVersion() 76 } 77 78 func getDaemonVersionAsStruct() version.CiliumVersion { 79 data := getDaemonVersionAsString() 80 if data == notResponding { 81 return version.CiliumVersion{} 82 } 83 return version.FromString(data) 84 }