github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/config.go (about)

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