gitee.com/mirrors/gauge@v1.0.6/cmd/config.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  
    23  	"github.com/getgauge/gauge/config"
    24  	"github.com/getgauge/gauge/logger"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var (
    29  	configCmd = &cobra.Command{
    30  		Use:     "config [flags] [args]",
    31  		Short:   "Change global configurations",
    32  		Long:    `Change global configurations.`,
    33  		Example: `  gauge config check_updates false`,
    34  		Run: func(cmd *cobra.Command, args []string) {
    35  			if list || machineReadable {
    36  				text, err := config.List(machineReadable)
    37  				if err != nil {
    38  					logger.Fatalf(true, err.Error())
    39  				}
    40  				logger.Infof(true, text)
    41  				return
    42  			}
    43  			if len(args) == 0 {
    44  				exit(fmt.Errorf("Config command needs argument(s)."), cmd.UsageString())
    45  			}
    46  			if len(args) == 1 {
    47  				text, err := config.GetProperty(args[0])
    48  				if err != nil {
    49  					logger.Fatalf(true, err.Error())
    50  				}
    51  				logger.Infof(true, text)
    52  				return
    53  			}
    54  			if err := config.Update(args[0], args[1]); err != nil {
    55  				logger.Fatalf(true, err.Error())
    56  			}
    57  		},
    58  		DisableAutoGenTag: true,
    59  	}
    60  	list bool
    61  )
    62  
    63  func init() {
    64  	GaugeCmd.AddCommand(configCmd)
    65  	configCmd.Flags().BoolVarP(&list, "list", "", false, "List all global properties")
    66  	configCmd.Flags().BoolVarP(&machineReadable, "machine-readable", "m", false, "Print all properties in JSON format")
    67  }