github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/app/app.go (about) 1 package app 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/cloudfoundry/cli/cf" 9 "github.com/cloudfoundry/cli/cf/command_metadata" 10 "github.com/cloudfoundry/cli/cf/command_runner" 11 "github.com/cloudfoundry/cli/cf/errors" 12 . "github.com/cloudfoundry/cli/cf/i18n" 13 "github.com/cloudfoundry/cli/cf/terminal" 14 "github.com/cloudfoundry/cli/cf/trace" 15 "github.com/codegangsta/cli" 16 ) 17 18 const UnknownCommand = "cf: '%s' is not a registered command. See 'cf help'" 19 20 func NewApp(cmdRunner command_runner.Runner, metadatas ...command_metadata.CommandMetadata) (app *cli.App) { 21 helpCommand := cli.Command{ 22 Name: "help", 23 ShortName: "h", 24 Description: T("Show help"), 25 Usage: fmt.Sprintf(T("{{.Command}} help [COMMAND]", map[string]interface{}{"Command": cf.Name()})), 26 Action: func(c *cli.Context) { 27 args := c.Args() 28 if len(args) > 0 { 29 cli.ShowCommandHelp(c, args[0]) 30 } else { 31 showAppHelp(appHelpTemplate(), c.App) 32 } 33 }, 34 } 35 36 cli.AppHelpTemplate = appHelpTemplate() 37 cli.HelpPrinter = ShowHelp 38 39 trace.Logger.Printf("\n%s\n%s\n\n", terminal.HeaderColor(T("VERSION:")), cf.Version) 40 41 app = cli.NewApp() 42 app.Usage = Usage() 43 app.Version = cf.Version + "-" + cf.BuiltOnDate 44 app.Action = helpCommand.Action 45 app.CommandNotFound = func(c *cli.Context, command string) { 46 panic(errors.Exception{ 47 Message: fmt.Sprintf(UnknownCommand, command), 48 DisplayCrashDialog: false, 49 }) 50 } 51 52 compiledAtTime, err := time.Parse("2006-01-02T03:04:05+00:00", cf.BuiltOnDate) 53 54 if err == nil { 55 app.Compiled = compiledAtTime 56 } else { 57 err = nil 58 app.Compiled = time.Now() 59 } 60 61 app.Commands = []cli.Command{helpCommand} 62 63 for _, metadata := range metadatas { 64 app.Commands = append(app.Commands, getCommand(metadata, cmdRunner)) 65 } 66 return 67 } 68 69 func getCommand(metadata command_metadata.CommandMetadata, runner command_runner.Runner) cli.Command { 70 return cli.Command{ 71 Name: metadata.Name, 72 ShortName: metadata.ShortName, 73 Description: metadata.Description, 74 Usage: strings.Replace(metadata.Usage, "CF_NAME", cf.Name(), -1), 75 Action: func(context *cli.Context) { 76 err := runner.RunCmdByName(metadata.Name, context) 77 if err != nil { 78 panic(terminal.QuietPanic) 79 } 80 }, 81 Flags: metadata.Flags, 82 SkipFlagParsing: metadata.SkipFlagParsing, 83 } 84 } 85 86 func Usage() string { 87 return T("A command line tool to interact with Cloud Foundry") 88 } 89 90 func appHelpTemplate() string { 91 return `{{.Title "` + T("NAME:") + `"}} 92 {{.Name}} - {{.Usage}} 93 94 {{.Title "` + T("USAGE:") + `"}} 95 ` + T("[environment variables]") + ` {{.Name}} ` + T("[global options] command [arguments...] [command options]") + ` 96 97 {{.Title "` + T("VERSION:") + `"}} 98 {{.Version}} 99 100 {{.Title "` + T("BUILD TIME:") + `"}} 101 {{.Compiled}} 102 {{range .Commands}} 103 {{.SubTitle .Name}}{{range .CommandSubGroups}} 104 {{range .}} {{.Name}} {{.Description}} 105 {{end}}{{end}}{{end}} 106 {{.Title "` + T("ENVIRONMENT VARIABLES:") + `"}} 107 CF_COLOR=false ` + T("Do not colorize output") + ` 108 CF_HOME=path/to/dir/ ` + T("Override path to default config directory") + ` 109 CF_PLUGIN_HOME=path/to/dir/ ` + T("Override path to default plugin config directory") + ` 110 CF_STAGING_TIMEOUT=15 ` + T("Max wait time for buildpack staging, in minutes") + ` 111 CF_STARTUP_TIMEOUT=5 ` + T("Max wait time for app instance startup, in minutes") + ` 112 CF_TRACE=true ` + T("Print API request diagnostics to stdout") + ` 113 CF_TRACE=path/to/trace.log ` + T("Append API request diagnostics to a log file") + ` 114 HTTP_PROXY=proxy.example.com:8080 ` + T("Enable HTTP proxying for API requests") + ` 115 116 {{.Title "` + T("GLOBAL OPTIONS:") + `"}} 117 --version, -v ` + T("Print the version") + ` 118 --help, -h ` + T("Show help") + ` 119 ` 120 }