- Notifications
You must be signed in to change notification settings - Fork928
fix: Standardize and wrap example descriptions at 80 chars#2894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -101,12 +101,16 @@ func Root() *cobra.Command { | ||
_, _ = fmt.Fprintln(cmd.ErrOrStderr()) | ||
} | ||
}, | ||
Example: formatExamples( | ||
example{ | ||
Description: "Start a Coder server", | ||
Command: "coder server", | ||
}, | ||
example{ | ||
Description: "Get started by creating a template from an example", | ||
Command: "coder templates init", | ||
}, | ||
), | ||
} | ||
cmd.AddCommand( | ||
@@ -158,9 +162,8 @@ func Root() *cobra.Command { | ||
// versionCmd prints the coder version | ||
func versionCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "version", | ||
Short: "Show coder version", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var str strings.Builder | ||
_, _ = str.WriteString(fmt.Sprintf("Coder %s", buildinfo.Version())) | ||
@@ -370,6 +373,34 @@ Use "{{.CommandPath}} [command] --help" for more information about a command. | ||
{{end}}` | ||
} | ||
// example represents a standard example for command usage, to be used | ||
// with formatExamples. | ||
type example struct { | ||
Description string | ||
Command string | ||
} | ||
// formatExamples formats the exampels as width wrapped bulletpoint | ||
// descriptions with the command underneath. | ||
func formatExamples(examples ...example) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Neat way to do this. Much better than hoping the visual display is correct!
AbhineetJain marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
wrap := cliui.Styles.Wrap.Copy() | ||
wrap.PaddingLeft(4) | ||
var sb strings.Builder | ||
for i, e := range examples { | ||
if len(e.Description) > 0 { | ||
_, _ = sb.WriteString(" - " + wrap.Render(e.Description + ":")[4:] + "\n\n ") | ||
} | ||
// We add 1 space here because `cliui.Styles.Code` adds an extra | ||
// space. This makes the code block align at an even 2 or 6 | ||
// spaces for symmetry. | ||
_, _ = sb.WriteString(" " + cliui.Styles.Code.Render(fmt.Sprintf("$ %s", e.Command))) | ||
if i < len(examples)-1 { | ||
_, _ = sb.WriteString("\n\n") | ||
} | ||
} | ||
return sb.String() | ||
} | ||
// FormatCobraError colorizes and adds "--help" docs to cobra commands. | ||
func FormatCobraError(err error, cmd *cobra.Command) string { | ||
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath()) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cli | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
func Test_formatExamples(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
examples []example | ||
wantMatches []string | ||
}{ | ||
{ | ||
name: "No examples", | ||
examples: nil, | ||
wantMatches: nil, | ||
}, | ||
{ | ||
name: "Output examples", | ||
examples: []example{ | ||
{ | ||
Description: "Hello world", | ||
Command: "echo hello", | ||
}, | ||
{ | ||
Description: "Bye bye", | ||
Command: "echo bye", | ||
}, | ||
}, | ||
wantMatches: []string{ | ||
"Hello world", "echo hello", | ||
"Bye bye", "echo bye", | ||
}, | ||
}, | ||
{ | ||
name: "No description outputs commands", | ||
examples: []example{ | ||
{ | ||
Command: "echo hello", | ||
}, | ||
}, | ||
wantMatches: []string{ | ||
"echo hello", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
got := formatExamples(tt.examples...) | ||
if len(tt.wantMatches) == 0 { | ||
require.Empty(t, got) | ||
} else { | ||
for _, want := range tt.wantMatches { | ||
require.Contains(t, got, want) | ||
AbhineetJain marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
} | ||
}) | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.