-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic schema validation and improved help output.
- Loading branch information
1 parent
5bdbd59
commit e3def98
Showing
6 changed files
with
154 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cmd | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"text/template" | ||
"unicode" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const customUsageTemplate = `Usage:{{if .Runnable}} | ||
{{.UseLine}}{{end}} | ||
Resources:{{resourceTypes | trimTrailingWhitespaces}}{{if .HasAvailableLocalFlags}} | ||
Flags: | ||
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} | ||
Global Flags: | ||
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}} | ||
` | ||
|
||
var templateFuncs = template.FuncMap{ | ||
"trimTrailingWhitespaces": trimTrailingWhitespaces, | ||
"resourceTypes": resourceTypes, | ||
} | ||
|
||
func customUsageFunc(cmd *cobra.Command) error { | ||
tryReadManifest() | ||
|
||
err := tmpl(cmd.OutOrStderr(), customUsageTemplate, cmd) | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
} | ||
return err | ||
} | ||
|
||
func resourceTypes() string { | ||
if len(manifest.Config.Resources) > 0 { | ||
return manifest.ResourceTypeNames() | ||
} | ||
return " unknown" | ||
} | ||
|
||
func trimTrailingWhitespaces(s string) string { | ||
return strings.TrimRightFunc(s, unicode.IsSpace) | ||
} | ||
|
||
// tmpl executes the given template text on data, writing the result to w. | ||
func tmpl(w io.Writer, text string, data interface{}) error { | ||
t := template.New("top") | ||
t.Funcs(templateFuncs) | ||
template.Must(t.Parse(text)) | ||
return t.Execute(w, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters