github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/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  	"os"
    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  				exit(config.List(machineReadable))
    37  			}
    38  			if len(args) == 0 {
    39  				logger.Fatalf("Error: Config command needs argument(s).\n%s", cmd.UsageString())
    40  			}
    41  			if len(args) == 1 {
    42  				exit(config.GetProperty(args[0]))
    43  			}
    44  			if err := config.Update(args[0], args[1]); err != nil {
    45  				logger.Fatalf(err.Error())
    46  			}
    47  		},
    48  		DisableAutoGenTag: true,
    49  	}
    50  	list bool
    51  )
    52  
    53  func init() {
    54  	GaugeCmd.AddCommand(configCmd)
    55  	configCmd.Flags().BoolVarP(&list, "list", "", false, "List all global properties")
    56  	configCmd.Flags().BoolVarP(&machineReadable, "machine-readable", "m", false, "Print all properties in JSON format")
    57  }
    58  
    59  func exit(text string, err error) {
    60  	if err != nil {
    61  		logger.Fatalf(err.Error())
    62  	}
    63  	logger.Infof(text)
    64  	os.Exit(0)
    65  }