diff options
Diffstat (limited to 'cmd/ponzu/usage.go')
-rw-r--r-- | cmd/ponzu/usage.go | 252 |
1 files changed, 75 insertions, 177 deletions
diff --git a/cmd/ponzu/usage.go b/cmd/ponzu/usage.go index 1f1364d..da28b44 100644 --- a/cmd/ponzu/usage.go +++ b/cmd/ponzu/usage.go @@ -1,201 +1,99 @@ package main import ( - "encoding/json" "fmt" - "io/ioutil" - "path/filepath" - "time" -) - -var year = fmt.Sprintf("%d", time.Now().Year()) - -var usageHeader = ` -$ ponzu [flags] command <params> - -Ponzu is a powerful and efficient open-source HTTP server framework and CMS. It -provides automatic, free, and secure HTTP/2 over TLS (certificates obtained via -[Let's Encrypt](https://letsencrypt.org)), a useful CMS and scaffolding to -generate set-up code, and a fast HTTP API on which to build modern applications. - -Ponzu is released under the BSD-3-Clause license (see LICENSE). -(c) 2016 - ` + year + ` Boss Sauce Creative, LLC - -COMMANDS: - -` - -var usageHelp = ` -help, h (command) - - Help command will print the usage for Ponzu, or if a command is entered, it - will show only the usage for that specific command. - - Example: - $ ponzu help generate - - -` - -var usageNew = ` -new <directory> - - Creates a 'ponzu' directory, or one by the name supplied as a parameter - immediately following the 'new' option in the $GOPATH/src directory. Note: - 'new' depends on the program 'git' and possibly a network connection. If - there is no local repository to clone from at the local machine's $GOPATH, - 'new' will attempt to clone the 'github.com/ponzu-cms/ponzu' package from - over the network. - - Example: - $ ponzu new myProject - > New ponzu project created at $GOPATH/src/myProject - - Errors will be reported, but successful commands return nothing. - - -` - -var usageGenerate = ` -generate, gen, g <generator type (,...fields)> - - Generate boilerplate code for various Ponzu components, such as 'content'. - - Example: - $ ponzu gen content review title:"string" body:"string" rating:"int" tags:"[]string" - - The command above will generate a file 'content/review.go' with boilerplate - methods, as well as struct definition, and corresponding field tags like: - - type Review struct { - Title string ` + "`json:" + `"title"` + "`" + ` - Body string ` + "`json:" + `"body"` + "`" + ` - Rating int ` + "`json:" + `"rating"` + "`" + ` - Tags []string ` + "`json:" + `"tags"` + "`" + ` - } - - The generate command will intelligently parse more sophisticated field names - such as 'field_name' and convert it to 'FieldName' and vice versa, only where - appropriate as per common Go idioms. Errors will be reported, but successful - generate commands return nothing. - - -` - -var usageBuild = ` -[-gocmd=go] build - - From within your Ponzu project directory, running build will copy and move - the necessary files from your workspace into the vendored directory, and - will build/compile the project to then be run. - - Example: - $ ponzu build - (or) - $ ponzu -gocmd=go1.8rc1 build - - By providing the 'gocmd' flag, you can specify which Go command to build the - project, if testing a different release of Go. - - Errors will be reported, but successful build commands return nothing. - - -` - -var usageRun = ` -[[-port=8080] [--https|--devhttps]] run <service(,service)> - - Starts the 'ponzu' HTTP server for the JSON API, Admin System, or both. - The segments, separated by a comma, describe which services to start, either - 'admin' (Admin System / CMS backend) or 'api' (JSON API), and, optionally, - if the server should utilize TLS encryption - served over HTTPS, which is - automatically managed using Let's Encrypt (https://letsencrypt.org) - - Example: - $ ponzu run - (or) - $ ponzu -port=8080 --https run admin,api - (or) - $ ponzu run admin - (or) - $ ponzu -port=8888 run api - - Defaults to '-port=8080 run admin,api' (running Admin & API on port 8080, without TLS) - - Note: - Admin and API cannot run on separate processes unless you use a copy of the - database, since the first process to open it receives a lock. If you intend - to run the Admin and API on separate processes, you must call them with the - 'ponzu' command independently. - - -` - -var usageUpgrade = ` -upgrade - - Will backup your own custom project code (like content, addons, uploads, etc) so - we can safely re-clone Ponzu from the latest version you have or from the network - if necessary. Before running '$ ponzu upgrade', you should update the 'ponzu' - package by running '$ go get -u github.com/ponzu-cms/ponzu/...' - - Example: - $ ponzu upgrade + "sort" + "strings" + "text/template" + "unicode" + "github.com/spf13/cobra" +) -` +var templateFuncs = template.FuncMap{ + "rpad": rpad, + "trimTrailingWhitespaces": trimRightSpace, +} -var usageVersion = ` -[--cli] version, v +var tmpl = `{{with (or .Cmd.Long .Cmd.Short)}}{{. | trimTrailingWhitespaces}} - Prints the version of Ponzu your project is using. Must be called from - within a Ponzu project directory. +{{end}}{{if or .Cmd.Runnable .Cmd.HasSubCommands}}Usage:{{if .Cmd.Runnable}} + {{.Cmd.UseLine}}{{end}}{{if .Cmd.HasAvailableSubCommands}} + {{.Cmd.CommandPath}} [command]{{end}}{{if gt (len .Cmd.Aliases) 0}} - Example: - $ ponzu version - > Ponzu v0.7.1 - (or) - $ ponzu --cli version - > Ponzu v0.7.2 +Aliases: + {{.Cmd.NameAndAliases}}{{end}}{{if .Cmd.HasExample}} +Examples: +{{.Cmd.Example}}{{end}}{{if .Cmd.HasAvailableSubCommands}} -` +Available Commands:{{range .Cmd.Commands}}{{if (or .IsAvailableCommand false)}} + {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableLocalFlags}} -var usageAdd = ` -add, a <import path> +Flags for all commands: +{{.Cmd.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{range .Subs}}{{if (and .IsAvailableCommand .HasAvailableLocalFlags)}} - Downloads addon from specified import path to $GOPATH/src and copys it to the - current project's ./addons directory. Must be called from within a - Ponzu project directory. +Flags for '{{.Name}}' command: +{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{end}}{{if .Cmd.HasHelpSubCommands}} - Example: - $ ponzu add github.com/bosssauce/fbscheduler +Additional help topics:{{range .Cmd.Commands}}{{if .Cmd.IsAdditionalHelpTopicCommand}} + {{rpad .Cmd.CommandPath .Cmd.CommandPathPadding}} {{.Cmd.Short}}{{end}}{{end}}{{end}}{{if .Cmd.HasAvailableSubCommands}} +Use "{{.Cmd.CommandPath}} [command] --help" for more information about a command.{{end}} +{{end}}` -` +var helpCmd = &cobra.Command{ + Use: "help", + Short: "help about any command", + Run: func(cmd *cobra.Command, args []string) { + cmd, _, e := rootCmd.Find(args) + if cmd == nil || e != nil { + rootCmd.Printf("Unknown help topic %#q\n", args) + rootCmd.Usage() + return + } + t := template.New("help") + t.Funcs(templateFuncs) + template.Must(t.Parse(tmpl)) + if len(args) > 0 { + rootCmd.HelpFunc()(cmd, args) + return + } -func ponzu(isCLI bool) (map[string]interface{}, error) { - kv := make(map[string]interface{}) + sortByName := func(i, j int) bool { return cmds[i].Name() < cmds[j].Name() } + sort.Slice(cmds, sortByName) - info := filepath.Join("cmd", "ponzu", "ponzu.json") - if isCLI { - gopath, err := getGOPATH() + err := t.Execute(cmd.OutOrStdout(), struct { + Cmd *cobra.Command + Subs []*cobra.Command + }{ + Cmd: rootCmd, + Subs: cmds}) if err != nil { - return nil, err + cmd.Println(err) } - repo := filepath.Join(gopath, "src", "github.com", "ponzu-cms", "ponzu") - info = filepath.Join(repo, "cmd", "ponzu", "ponzu.json") - } + }, +} + +var cmds []*cobra.Command + +// RegisterCmdlineCommand adds a cobra command to the root command and makes it +// known to the main package +func RegisterCmdlineCommand(cmd *cobra.Command) { + rootCmd.AddCommand(cmd) + cmds = append(cmds, cmd) +} - b, err := ioutil.ReadFile(info) - if err != nil { - return nil, err - } +func init() { + rootCmd.AddCommand(helpCmd) +} - err = json.Unmarshal(b, &kv) - if err != nil { - return nil, err - } +// rpad adds padding to the right of a string. +func rpad(s string, padding int) string { + template := fmt.Sprintf("%%-%ds", padding) + return fmt.Sprintf(template, s) +} - return kv, nil +func trimRightSpace(s string) string { + return strings.TrimRightFunc(s, unicode.IsSpace) } |