|
| 1 | +// Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License.AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "log/slog" |
| 9 | + |
| 10 | + "github.com/gitpod-io/local-app/pkg/config" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var configSetCmd = &cobra.Command{ |
| 15 | + Use: "set", |
| 16 | + Short: "Set an individual config value in the config file", |
| 17 | + Long: `Set an individual config value in the config file. |
| 18 | +
|
| 19 | +Example: |
| 20 | + # Disable telemetry |
| 21 | + local-app config set --telemetry=false |
| 22 | +
|
| 23 | + # Disable autoupdate |
| 24 | + local-app config set --autoupdate=false |
| 25 | +
|
| 26 | + # Enable telemetry and autoupdate |
| 27 | + local-app config set --telemetry=true --autoupdate=true |
| 28 | +`, |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + cmd.SilenceUsage = true |
| 31 | + |
| 32 | + var update bool |
| 33 | + cfg := config.FromContext(cmd.Context()) |
| 34 | + if cmd.Flags().Changed("autoupdate") { |
| 35 | + cfg.Autoupdate = configSetOpts.Autoupdate |
| 36 | + update = true |
| 37 | + } |
| 38 | + if cmd.Flags().Changed("telemetry") { |
| 39 | + cfg.Telemetry.Enabled = configSetOpts.Telemetry |
| 40 | + update = true |
| 41 | + } |
| 42 | + if !update { |
| 43 | + return cmd.Help() |
| 44 | + } |
| 45 | + |
| 46 | + slog.Debug("updating config") |
| 47 | + err := config.SaveConfig(cfg.Filename, cfg) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + return nil |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +var configSetOpts struct { |
| 56 | + Autoupdate bool |
| 57 | + Telemetry bool |
| 58 | +} |
| 59 | + |
| 60 | +func init() { |
| 61 | + configCmd.AddCommand(configSetCmd) |
| 62 | + configSetCmd.Flags().BoolVar(&configSetOpts.Autoupdate, "autoupdate", true, "enable/disable autoupdate") |
| 63 | + configSetCmd.Flags().BoolVar(&configSetOpts.Telemetry, "telemetry", true, "enable/disable telemetry") |
| 64 | +} |
0 commit comments