Skip to content

Commit

Permalink
feat: add shell argument completions
Browse files Browse the repository at this point in the history
Add basic positonal argument completions to all commands, and root and
lint flag argument completions.
  • Loading branch information
scop authored and daveshanley committed Oct 20, 2022
1 parent 64576f3 commit d281db9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func GetDashboardCommand() *cobra.Command {
Short: "Show vacuum dashboard for linting report",
Long: "Interactive console dashboard to explore linting report in detail",
Example: "vacuum dashboard my-awesome-spec.yaml",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return []string{"yaml", "yml", "json"}, cobra.ShellCompDirectiveFilterFileExt
},
RunE: func(cmd *cobra.Command, args []string) error {

// check for file args
Expand Down
10 changes: 10 additions & 0 deletions cmd/generate_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func GetGenerateRulesetCommand() *cobra.Command {
Short: "Generate a vacuum RuleSet",
Long: "Generate a YAML ruleset containing 'all', or 'recommended' rules",
Example: "vacuum generate-ruleset recommended | all <ruleset-output-name>",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return []string{"recommended", "all"}, cobra.ShellCompDirectiveNoFileComp
case 1:
return []string{"yaml", "yml"}, cobra.ShellCompDirectiveFilterFileExt
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
RunE: func(cmd *cobra.Command, args []string) error {

PrintBanner()
Expand Down
10 changes: 10 additions & 0 deletions cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func GetHTMLReportCommand() *cobra.Command {
Long: "Generate an interactive and useful HTML report. Default output " +
"filename is 'report.html' located in the working directory.",
Example: "vacuum html-report <my-awesome-spec.yaml> <report.html>",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return []string{"yaml", "yml", "json"}, cobra.ShellCompDirectiveFilterFileExt
case 1:
return []string{"html", "htm"}, cobra.ShellCompDirectiveFilterFileExt
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
RunE: func(cmd *cobra.Command, args []string) error {

PrintBanner()
Expand Down
29 changes: 29 additions & 0 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func GetLintCommand() *cobra.Command {
Use: "lint",
Short: "Lint an OpenAPI specification",
Long: `Lint an OpenAPI specification, the output of the response will be in the terminal`,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return []string{"yaml", "yml", "json"}, cobra.ShellCompDirectiveFilterFileExt
},
RunE: func(cmd *cobra.Command, args []string) error {

detailsFlag, _ := cmd.Flags().GetBool("details")
Expand Down Expand Up @@ -182,6 +188,29 @@ func GetLintCommand() *cobra.Command {
cmd.Flags().BoolP("silent", "x", false, "Show nothing except the result.")
cmd.Flags().StringP("fail-severity", "n", "error", "Results of this level or above will trigger a failure exit code")

regErr := cmd.RegisterFlagCompletionFunc("category", cobra.FixedCompletions([]string{
model.CategoryAll,
model.CategoryDescriptions,
model.CategoryExamples,
model.CategoryInfo,
model.CategoryOperations,
model.CategorySchemas,
model.CategorySecurity,
model.CategoryTags,
model.CategoryValidation,
}, cobra.ShellCompDirectiveNoFileComp))
if regErr != nil {
panic(regErr)
}
regErr = cmd.RegisterFlagCompletionFunc("fail-severity", cobra.FixedCompletions([]string{
"info",
"warn",
"error",
}, cobra.ShellCompDirectiveNoFileComp))
if regErr != nil {
panic(regErr)
}

return cmd
}

Expand Down
13 changes: 13 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ func init() {
rootCmd.PersistentFlags().StringP("ruleset", "r", "", "Path to a spectral ruleset configuration")
rootCmd.PersistentFlags().StringP("functions", "f", "", "Path to custom functions")

regErr := rootCmd.RegisterFlagCompletionFunc("functions", cobra.FixedCompletions(
[]string{"so"}, cobra.ShellCompDirectiveFilterFileExt,
))
if regErr != nil {
panic(regErr)
}
regErr = rootCmd.RegisterFlagCompletionFunc("ruleset", cobra.FixedCompletions(
[]string{"yaml", "yml"}, cobra.ShellCompDirectiveFilterFileExt,
))
if regErr != nil {
panic(regErr)
}

rootCmd.AddCommand(GetLintCommand())
rootCmd.AddCommand(GetVacuumReportCommand())
rootCmd.AddCommand(GetSpectralReportCommand())
Expand Down
10 changes: 10 additions & 0 deletions cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func GetSpectralReportCommand() *cobra.Command {
Long: "Generate a JSON report using the same model as Spectral. Default output " +
"filename is 'vacuum-spectral-report.json' located in the working directory.",
Example: "vacuum report my-awesome-spec.yaml <vacuum-spectral-report.json>",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return []string{"yaml", "yml", "json"}, cobra.ShellCompDirectiveFilterFileExt
case 1:
return []string{"json"}, cobra.ShellCompDirectiveFilterFileExt
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
RunE: func(cmd *cobra.Command, args []string) error {

PrintBanner()
Expand Down
6 changes: 6 additions & 0 deletions cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func GetVacuumReportCommand() *cobra.Command {
Long: "Generate a full report of a linting run. This can be used as a result set, or can be used to replay a linting run. " +
"the default filename is 'vacuum-report-MM-DD-YY-HH_MM_SS.json' located in the working directory.",
Example: "vacuum report <my-awesome-spec.yaml> <report-prefix>",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return []string{"yaml", "yml", "json"}, cobra.ShellCompDirectiveFilterFileExt
}
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {

PrintBanner()
Expand Down

0 comments on commit d281db9

Please sign in to comment.