github.com/getgauge/gauge@v1.6.9/cmd/version.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package cmd 8 9 import ( 10 "encoding/json" 11 "fmt" 12 "path/filepath" 13 14 "github.com/getgauge/gauge/logger" 15 "github.com/getgauge/gauge/plugin/pluginInfo" 16 "github.com/getgauge/gauge/version" 17 "github.com/spf13/cobra" 18 ) 19 20 var ( 21 versionCmd = &cobra.Command{ 22 Use: "version [flags]", 23 Short: "Print Gauge and plugin versions", 24 Long: `Print Gauge and plugin versions.`, 25 Example: ` gauge version 26 gauge version -m`, 27 Run: func(cmd *cobra.Command, args []string) { 28 printVersion() 29 }, 30 PersistentPostRun: func(cmd *cobra.Command, args []string) { /* noop */ }, 31 DisableAutoGenTag: true, 32 } 33 ) 34 35 func init() { 36 GaugeCmd.AddCommand(versionCmd) 37 } 38 39 func printVersion() { 40 if machineReadable { 41 printJSONVersion() 42 return 43 } 44 printTextVersion() 45 } 46 47 func printJSONVersion() { 48 type pluginJSON struct { 49 Name string `json:"name"` 50 Version string `json:"version"` 51 } 52 type versionJSON struct { 53 Version string `json:"version"` 54 CommitHash string `json:"commitHash"` 55 Plugins []*pluginJSON `json:"plugins"` 56 } 57 gaugeVersion := versionJSON{version.FullVersion(), version.CommitHash, make([]*pluginJSON, 0)} 58 allPluginsWithVersion, err := pluginInfo.GetAllInstalledPluginsWithVersion() 59 if err != nil { 60 logger.Errorf(false, "Error fetching plugins info: %s", err.Error()) 61 62 } 63 for _, pluginInfo := range allPluginsWithVersion { 64 gaugeVersion.Plugins = append(gaugeVersion.Plugins, &pluginJSON{pluginInfo.Name, filepath.Base(pluginInfo.Path)}) 65 } 66 b, err := json.MarshalIndent(gaugeVersion, "", " ") 67 if err != nil { 68 fmt.Println("Error fetching version info as JSON:", err.Error()) 69 return 70 } 71 // logger can not be used, since it breaks the json format, 72 // The logger adds out, nessage as key which vscode plugin does not understand. 73 fmt.Printf("%s\n\n", string(b)) 74 } 75 76 func printTextVersion() { 77 logger.Infof(true, "Gauge version: %s", version.FullVersion()) 78 v := version.CommitHash 79 if v != "" { 80 logger.Infof(true, "Commit Hash: %s\n", v) 81 82 } 83 logger.Infof(true, "Plugins\n-------") 84 allPluginsWithVersion, err := pluginInfo.GetAllInstalledPluginsWithVersion() 85 if err != nil { 86 logger.Infof(true, "No plugins found\nPlugins can be installed with `gauge install {plugin-name}`") 87 } 88 for _, pluginInfo := range allPluginsWithVersion { 89 logger.Infof(true, "%s (%s)", pluginInfo.Name, filepath.Base(pluginInfo.Path)) 90 } 91 }