gitee.com/mirrors/gauge@v1.0.6/cmd/telemetry.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 "fmt" 22 "os" 23 "strings" 24 25 "strconv" 26 27 "github.com/getgauge/gauge/config" 28 "github.com/getgauge/gauge/logger" 29 "github.com/spf13/cobra" 30 ) 31 32 const gaugeTelemetryEnabled = "GAUGE_TELEMETRY_ENABLED" 33 34 var telemetryEnv = os.Getenv(gaugeTelemetryEnabled) 35 36 func telemetryEnabled() bool { 37 boolValue, err := strconv.ParseBool(strings.TrimSpace(telemetryEnv)) 38 if err != nil { 39 return config.TelemetryEnabled() 40 } 41 return boolValue 42 } 43 44 var ( 45 telemetryCmd = &cobra.Command{ 46 Use: "telemetry [command]", 47 Short: "Configure options for sending anonymous usage stats", 48 Long: `Configure options for sending anonymous usage stats.`, 49 Example: ` gauge telemetry on 50 gauge telemetry off 51 gauge telemetry`, 52 Run: func(cmd *cobra.Command, args []string) { 53 if len(args) != 0 { 54 exit(nil, cmd.UsageString()) 55 } 56 fmt.Println(map[bool]string{true: "on", false: "off"}[telemetryEnabled()]) 57 }, 58 PersistentPostRun: func(cmd *cobra.Command, args []string) { 59 if v, err := strconv.ParseBool(strings.TrimSpace(telemetryEnv)); err == nil { 60 logger.Infof(true, "ENV[%s]=%t. Overrides telemetry configuration.", gaugeTelemetryEnabled, v) 61 } 62 }, 63 DisableAutoGenTag: true, 64 } 65 66 onCmd = &cobra.Command{ 67 Use: "on", 68 Short: "Turn telemetry on", 69 Long: "Turn telemetry on.", 70 Example: " gauge telemetry on", 71 Run: func(cmd *cobra.Command, args []string) { 72 if err := config.UpdateTelemetry("true"); err != nil { 73 logger.Fatalf(true, err.Error()) 74 } 75 config.RecordTelemetryConsentSet() 76 }, 77 DisableAutoGenTag: true, 78 } 79 80 offCmd = &cobra.Command{ 81 Use: "off", 82 Short: "Turn telemetry off", 83 Long: "Turn telemetry off.", 84 Example: " gauge telemetry off", 85 Run: func(cmd *cobra.Command, args []string) { 86 if err := config.UpdateTelemetry("false"); err != nil { 87 logger.Fatalf(true, err.Error()) 88 } 89 config.RecordTelemetryConsentSet() 90 }, 91 DisableAutoGenTag: true, 92 } 93 94 logCmd = &cobra.Command{ 95 Use: "log <value>", 96 Short: "Enable/disable telemetry logging", 97 Long: "Enable/disable telemetry logging.", 98 Example: ` gauge telemetry log true 99 gauge telemetry log false`, 100 Run: func(cmd *cobra.Command, args []string) { 101 if len(args) < 1 { 102 fmt.Println(config.TelemetryLogEnabled()) 103 return 104 } 105 if _, err := strconv.ParseBool(args[0]); err != nil { 106 exit(fmt.Errorf("Invalid argument. The valid options are true or false."), cmd.UsageString()) 107 } 108 config.UpdateTelemetryLoggging(args[0]) 109 }, 110 PersistentPostRun: func(cmd *cobra.Command, args []string) {}, 111 DisableAutoGenTag: true, 112 } 113 ) 114 115 func init() { 116 telemetryCmd.AddCommand(onCmd) 117 telemetryCmd.AddCommand(offCmd) 118 telemetryCmd.AddCommand(logCmd) 119 GaugeCmd.AddCommand(telemetryCmd) 120 }