gitee.com/mirrors/gauge@v1.0.6/cmd/version.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "encoding/json" 22 "fmt" 23 "os" 24 "path/filepath" 25 26 "github.com/getgauge/gauge/track" 27 28 "github.com/getgauge/gauge/plugin/pluginInfo" 29 "github.com/getgauge/gauge/version" 30 "github.com/spf13/cobra" 31 ) 32 33 var ( 34 versionCmd = &cobra.Command{ 35 Use: "version [flags]", 36 Short: "Print Gauge and plugin versions", 37 Long: `Print Gauge and plugin versions.`, 38 Example: ` gauge version 39 gauge version -m`, 40 Run: func(cmd *cobra.Command, args []string) { 41 printVersion() 42 }, 43 PersistentPostRun: func(cmd *cobra.Command, args []string) { /* noop */ }, 44 DisableAutoGenTag: true, 45 } 46 ) 47 48 func init() { 49 GaugeCmd.AddCommand(versionCmd) 50 } 51 52 func printVersion() { 53 if machineReadable { 54 printJSONVersion() 55 return 56 } 57 printTextVersion() 58 } 59 60 func printJSONVersion() { 61 type pluginJSON struct { 62 Name string `json:"name"` 63 Version string `json:"version"` 64 } 65 type versionJSON struct { 66 Version string `json:"version"` 67 CommitHash string `json:"commitHash"` 68 Plugins []*pluginJSON `json:"plugins"` 69 TelemetryMessage string `json:"telemetry"` 70 } 71 gaugeVersion := versionJSON{version.FullVersion(), version.GetCommitHash(), make([]*pluginJSON, 0), track.GaugeTelemetryMessage} 72 allPluginsWithVersion, err := pluginInfo.GetAllInstalledPluginsWithVersion() 73 for _, pluginInfo := range allPluginsWithVersion { 74 gaugeVersion.Plugins = append(gaugeVersion.Plugins, &pluginJSON{pluginInfo.Name, filepath.Base(pluginInfo.Path)}) 75 } 76 b, err := json.MarshalIndent(gaugeVersion, "", " ") 77 if err != nil { 78 fmt.Println("error:", err) 79 } 80 fmt.Println(fmt.Sprintf("%s\n", string(b))) 81 } 82 83 func printTextVersion() { 84 fmt.Printf("Gauge version: %s\n", version.FullVersion()) 85 v := version.GetCommitHash() 86 if v != "" { 87 fmt.Printf("Commit Hash: %s\n", v) 88 89 } 90 fmt.Printf("\nPlugins\n-------\n") 91 allPluginsWithVersion, err := pluginInfo.GetAllInstalledPluginsWithVersion() 92 if err != nil { 93 fmt.Println("No plugins found") 94 fmt.Println("Plugins can be installed with `gauge install {plugin-name}`") 95 os.Exit(0) 96 } 97 for _, pluginInfo := range allPluginsWithVersion { 98 fmt.Printf("%s (%s)\n", pluginInfo.Name, filepath.Base(pluginInfo.Path)) 99 } 100 }