Skip to content

Commit

Permalink
Help improvements (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe authored Aug 14, 2019
1 parent 0b8b1e1 commit 52747e0
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 77 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func newCompletionCmd() *completionCmd {

cc.cmd = &cobra.Command{
Use: "completion",
Short: "Generates bash and zsh completion scripts",
Short: "Generate bash and zsh completion scripts",
Args: validators.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if cc.shell == "zsh" {
Expand Down
7 changes: 3 additions & 4 deletions pkg/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/cobra"

"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/requests"
"github.com/stripe/stripe-cli/pkg/validators"
)
Expand All @@ -21,9 +20,9 @@ func newDeleteCmd() *deleteCmd {
gc.reqs.Method = http.MethodDelete
gc.reqs.Profile = &Config.Profile
gc.reqs.Cmd = &cobra.Command{
Use: "delete",
Use: "delete <path>",
Args: validators.ExactArgs(1),
Short: "Make DELETE requests to the Stripe API using your test mode key.",
Short: "Make a DELETE request to the Stripe API",
Long: fmt.Sprintf(`%s
Make DELETE requests to the Stripe API using your test mode key.
Expand All @@ -37,7 +36,7 @@ https://stripe.com/docs/api
To delete a charge:
$ stripe delete /customers/cus_FROPkgsHVRRspg`,
ansi.Italic("⚠️ The Stripe CLI is in beta! Have feedback? Let us know, run: 'stripe feedback'. ⚠️"),
getBanner(),
),
RunE: gc.reqs.RunRequestsCmd,
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/cobra"

"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/requests"
"github.com/stripe/stripe-cli/pkg/validators"
)
Expand All @@ -21,9 +20,9 @@ func newGetCmd() *getCmd {
gc.reqs.Method = http.MethodGet
gc.reqs.Profile = &Config.Profile
gc.reqs.Cmd = &cobra.Command{
Use: "get",
Use: "get <path>",
Args: validators.ExactArgs(1),
Short: "Make GET requests to the Stripe API using your test mode key.",
Short: "Make a GET request to the Stripe API",
Long: fmt.Sprintf(`%s
Make GET requests to the Stripe API using your test mode key.
Expand All @@ -41,7 +40,7 @@ To get a charge:
To get 50 charges:
$ stripe get --limit 50 /charges`,
ansi.Italic("⚠️ The Stripe CLI is in beta! Have feedback? Let us know, run: 'stripe feedback'. ⚠️"),
getBanner(),
),

RunE: gc.reqs.RunRequestsCmd,
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/proxy"
"github.com/stripe/stripe-cli/pkg/requests"
"github.com/stripe/stripe-cli/pkg/stripe"
Expand Down Expand Up @@ -40,7 +39,7 @@ func newListenCmd() *listenCmd {
lc.cmd = &cobra.Command{
Use: "listen",
Args: validators.NoArgs,
Short: "Listens for webhook events sent from Stripe to help test your integration.",
Short: "Listen for webhook events",
Long: fmt.Sprintf(`%s
The listen command lets you watch for and forward webhook events from Stripe.
Expand All @@ -57,7 +56,7 @@ Start listening for 'charge.created' and 'charge.updated' events and forward
to your localhost:
$ stripe listen --events charge.created,charge.updated --forward-to localhost:9000/events`,
ansi.Italic("⚠️ The Stripe CLI is in beta! Have feedback? Let us know, run: 'stripe feedback'. ⚠️"),
getBanner(),
),
RunE: lc.runListenCmd,
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/cobra"

"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/requests"
"github.com/stripe/stripe-cli/pkg/validators"
)
Expand All @@ -21,9 +20,9 @@ func newPostCmd() *postCmd {
gc.reqs.Method = http.MethodPost
gc.reqs.Profile = &Config.Profile
gc.reqs.Cmd = &cobra.Command{
Use: "post",
Use: "post <path>",
Args: validators.ExactArgs(1),
Short: "Make POST requests to the Stripe API using your test mode key.",
Short: "Make a POST request to the Stripe API",
Long: fmt.Sprintf(`%s
Make POST requests to the Stripe API using your test mode key.
Expand All @@ -33,13 +32,14 @@ Currently, you can only POST data in test mode.
For a full list of supported paths, see the API reference:
https://stripe.com/docs/api
`,

Example:
$ stripe post /payment_intents -d amount=2000 -d currency=usd -d "payment_method_types[]=card"`,

ansi.Italic("⚠️ The Stripe CLI is in beta! Have feedback? Let us know, run: 'stripe feedback'. ⚠️"),
getBanner(),
),
Example: `stripe post /payment_intents \
-d amount=2000 \
-d currency=usd \
-d "payment_method_types[]=card"`,
RunE: gc.reqs.RunRequestsCmd,
}

Expand Down
52 changes: 2 additions & 50 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/config"
"github.com/stripe/stripe-cli/pkg/version"
)
Expand Down Expand Up @@ -45,61 +44,14 @@ If you're working on multiple projects, you can run the login command with the
--project-name flag:
$ stripe login --project-name rocket-rides`,
ansi.Italic("⚠️ The Stripe CLI is in beta! Have feedback? Let us know, run: 'stripe feedback'. ⚠️"),
getBanner(),
),
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
rootCmd.SetUsageTemplate(fmt.Sprintf(`%s{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
%s
{{.NameAndAliases}}{{end}}{{if .HasExample}}
%s
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{if .Annotations}}
%s{{range $index, $cmd := .Commands}}{{if (eq (index $.Annotations $cmd.Name) "http")}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}
%s{{range $index, $cmd := .Commands}}{{if (eq (index $.Annotations $cmd.Name) "webhooks")}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}
%s{{range $index, $cmd := .Commands}}{{if (eq (index $.Annotations $cmd.Name) "stripe")}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}
%s{{range $index, $cmd := .Commands}}{{if (not (index $.Annotations $cmd.Name))}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}{{else}}
%s{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
%s
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
%s
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
%s{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`,
ansi.Bold("Usage:"),
ansi.Bold("Aliases:"),
ansi.Bold("Examples:"),
ansi.Bold("HTTP commands:"),
ansi.Bold("Webhook commands:"),
ansi.Bold("Stripe commands:"),
ansi.Bold("Other commands:"),
ansi.Bold("Available commands:"),
ansi.Bold("Flags:"),
ansi.Bold("Global flags:"),
ansi.Bold("Additional help topics:"),
))
rootCmd.SetUsageTemplate(getUsageTemplate())
rootCmd.SetVersionTemplate(version.Template)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
87 changes: 87 additions & 0 deletions pkg/cmd/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"

"github.com/stripe/stripe-cli/pkg/ansi"
)

func init() {
cobra.AddTemplateFunc("wrappedInheritedFlagUsages", wrappedInheritedFlagUsages)
cobra.AddTemplateFunc("wrappedLocalFlagUsages", wrappedLocalFlagUsages)
}

func getBanner() string {
return ansi.Italic("⚠️ The Stripe CLI is in beta! Share your feedback with `stripe feedback` ⚠️")
}

func getUsageTemplate() string {
return fmt.Sprintf(`%s{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
%s
{{.NameAndAliases}}{{end}}{{if .HasExample}}
%s
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{if .Annotations}}
%s{{range $index, $cmd := .Commands}}{{if (eq (index $.Annotations $cmd.Name) "http")}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}
%s{{range $index, $cmd := .Commands}}{{if (eq (index $.Annotations $cmd.Name) "webhooks")}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}
%s{{range $index, $cmd := .Commands}}{{if (eq (index $.Annotations $cmd.Name) "stripe")}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}
%s{{range $index, $cmd := .Commands}}{{if (not (index $.Annotations $cmd.Name))}}
{{rpad $cmd.Name $cmd.NamePadding }} {{$cmd.Short}}{{end}}{{end}}{{else}}
%s{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
%s
{{wrappedLocalFlagUsages . | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
%s
{{wrappedInheritedFlagUsages . | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
%s{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`,
ansi.Bold("Usage:"),
ansi.Bold("Aliases:"),
ansi.Bold("Examples:"),
ansi.Bold("HTTP commands:"),
ansi.Bold("Webhook commands:"),
ansi.Bold("Stripe commands:"),
ansi.Bold("Other commands:"),
ansi.Bold("Available commands:"),
ansi.Bold("Flags:"),
ansi.Bold("Global flags:"),
ansi.Bold("Additional help topics:"),
)
}

func getTerminalWidth() int {
var width int
width, _, err := terminal.GetSize(0)
if err != nil {
width = 80
}
return width
}

func wrappedInheritedFlagUsages(cmd *cobra.Command) string {
return cmd.InheritedFlags().FlagUsagesWrapped(getTerminalWidth())
}

func wrappedLocalFlagUsages(cmd *cobra.Command) string {
return cmd.LocalFlags().FlagUsagesWrapped(getTerminalWidth())
}
11 changes: 4 additions & 7 deletions pkg/cmd/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type triggerCmd struct {
func newTriggerCmd() *triggerCmd {
tc := &triggerCmd{}
tc.cmd = &cobra.Command{
Use: "trigger",
Use: "trigger <event>",
Args: validators.ExactArgs(1),
ValidArgs: []string{
"charge.captured",
Expand All @@ -49,10 +49,6 @@ Cause a specific webhook event to be created and sent. Webhooks tested through
the trigger command will also create all necessary side-effect events that are
needed to create the triggered event.
Trigger a payment_intent.created event:
$ stripe trigger payment_intent.created
%s
charge.captured
charge.failed
Expand All @@ -70,10 +66,11 @@ Trigger a payment_intent.created event:
payment_intent.payment_failed
payment_intent.succeeded
payment_method.attached`,
ansi.Italic("⚠️ The Stripe CLI is in beta! Have feedback? Let us know, run: 'stripe feedback'. ⚠️"),
getBanner(),
ansi.Bold("Supported events:"),
),
RunE: tc.runTriggerCmd,
Example: `stripe trigger payment_intent.created`,
RunE: tc.runTriggerCmd,
}

// Hidden configuration flags, useful for dev/debugging
Expand Down

0 comments on commit 52747e0

Please sign in to comment.