github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/config.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"sort"
     6  
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/terminal"
    12  
    13  	. "code.cloudfoundry.org/cli/cf/i18n"
    14  )
    15  
    16  type ConfigCommands struct {
    17  	ui     terminal.UI
    18  	config coreconfig.ReadWriter
    19  }
    20  
    21  func init() {
    22  	commandregistry.Register(&ConfigCommands{})
    23  }
    24  
    25  func (cmd *ConfigCommands) MetaData() commandregistry.CommandMetadata {
    26  	fs := make(map[string]flags.FlagSet)
    27  	fs["async-timeout"] = &flags.IntFlag{Name: "async-timeout", Usage: T("Timeout for async HTTP requests")}
    28  	fs["trace"] = &flags.StringFlag{Name: "trace", Usage: T("Trace HTTP requests")}
    29  	fs["color"] = &flags.StringFlag{Name: "color", Usage: T("Enable or disable color")}
    30  	fs["locale"] = &flags.StringFlag{Name: "locale", Usage: T("Set default locale. If LOCALE is 'CLEAR', previous locale is deleted.")}
    31  
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "config",
    34  		Description: T("Write default values to the config"),
    35  		Usage: []string{
    36  			T("CF_NAME config [--async-timeout TIMEOUT_IN_MINUTES] [--trace (true | false | path/to/file)] [--color (true | false)] [--locale (LOCALE | CLEAR)]"),
    37  		},
    38  		Flags: fs,
    39  	}
    40  }
    41  
    42  func (cmd *ConfigCommands) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    43  	reqs := []requirements.Requirement{}
    44  	return reqs, nil
    45  }
    46  
    47  func (cmd *ConfigCommands) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    48  	cmd.ui = deps.UI
    49  	cmd.config = deps.Config
    50  	return cmd
    51  }
    52  
    53  func (cmd *ConfigCommands) Execute(context flags.FlagContext) error {
    54  	if !context.IsSet("trace") && !context.IsSet("async-timeout") && !context.IsSet("color") && !context.IsSet("locale") {
    55  		return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("config"))
    56  	}
    57  
    58  	if context.IsSet("async-timeout") {
    59  		asyncTimeout := context.Int("async-timeout")
    60  		if asyncTimeout < 0 {
    61  			return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("config"))
    62  		}
    63  
    64  		cmd.config.SetAsyncTimeout(uint(asyncTimeout))
    65  	}
    66  
    67  	if context.IsSet("trace") {
    68  		cmd.config.SetTrace(context.String("trace"))
    69  	}
    70  
    71  	if context.IsSet("color") {
    72  		value := context.String("color")
    73  		switch value {
    74  		case "true":
    75  			cmd.config.SetColorEnabled("true")
    76  		case "false":
    77  			cmd.config.SetColorEnabled("false")
    78  		default:
    79  			return errors.New(T("Incorrect Usage") + "\n\n" + commandregistry.Commands.CommandUsage("config"))
    80  		}
    81  	}
    82  
    83  	if context.IsSet("locale") {
    84  		locale := context.String("locale")
    85  
    86  		if locale == "CLEAR" {
    87  			cmd.config.SetLocale("")
    88  			return nil
    89  		}
    90  
    91  		if IsSupportedLocale(locale) {
    92  			cmd.config.SetLocale(locale)
    93  			return nil
    94  		}
    95  
    96  		unsupportedLocaleMessage := T("Could not find locale '{{.UnsupportedLocale}}'. The known locales are:\n", map[string]interface{}{
    97  			"UnsupportedLocale": locale,
    98  		})
    99  		supportedLocales := SupportedLocales()
   100  		sort.Strings(supportedLocales)
   101  		for i := range supportedLocales {
   102  			unsupportedLocaleMessage = unsupportedLocaleMessage + "\n" + supportedLocales[i]
   103  		}
   104  
   105  		return errors.New(unsupportedLocaleMessage)
   106  	}
   107  	return nil
   108  }