-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Group bundle run flags by job and pipeline types (#1174)
## Changes Group bundle run flags by job and pipeline types ## Tests ``` Run a resource (e.g. a job or a pipeline) Usage: databricks bundle run [flags] KEY Job Flags: --dbt-commands strings A list of commands to execute for jobs with DBT tasks. --jar-params strings A list of parameters for jobs with Spark JAR tasks. --notebook-params stringToString A map from keys to values for jobs with notebook tasks. (default []) --params stringToString comma separated k=v pairs for job parameters (default []) --pipeline-params stringToString A map from keys to values for jobs with pipeline tasks. (default []) --python-named-params stringToString A map from keys to values for jobs with Python wheel tasks. (default []) --python-params strings A list of parameters for jobs with Python tasks. --spark-submit-params strings A list of parameters for jobs with Spark submit tasks. --sql-params stringToString A map from keys to values for jobs with SQL tasks. (default []) Pipeline Flags: --full-refresh strings List of tables to reset and recompute. --full-refresh-all Perform a full graph reset and recompute. --refresh strings List of tables to update. --refresh-all Perform a full graph update. Flags: -h, --help help for run --no-wait Don't wait for the run to complete. Global Flags: --debug enable debug logging -o, --output type output type: text or json (default text) -p, --profile string ~/.databrickscfg profile -t, --target string bundle target to use (if applicable) --var strings set values for variables defined in bundle config. Example: --var="foo=bar" ```
- Loading branch information
1 parent
4131069
commit 2bbb644
Showing
7 changed files
with
170 additions
and
11 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
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,83 @@ | ||
package cmdgroup | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"text/template" | ||
"unicode" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type CommandWithGroupFlag struct { | ||
cmd *cobra.Command | ||
flagGroups []*FlagGroup | ||
} | ||
|
||
func (c *CommandWithGroupFlag) Command() *cobra.Command { | ||
return c.cmd | ||
} | ||
|
||
func (c *CommandWithGroupFlag) FlagGroups() []*FlagGroup { | ||
return c.flagGroups | ||
} | ||
|
||
func NewCommandWithGroupFlag(cmd *cobra.Command) *CommandWithGroupFlag { | ||
cmdWithFlagGroups := &CommandWithGroupFlag{cmd: cmd, flagGroups: make([]*FlagGroup, 0)} | ||
cmd.SetUsageFunc(func(c *cobra.Command) error { | ||
err := tmpl(c.OutOrStderr(), c.UsageTemplate(), cmdWithFlagGroups) | ||
if err != nil { | ||
c.PrintErrln(err) | ||
} | ||
return nil | ||
}) | ||
cmd.SetUsageTemplate(usageTemplate) | ||
return cmdWithFlagGroups | ||
} | ||
|
||
func (c *CommandWithGroupFlag) AddFlagGroup(name string) *FlagGroup { | ||
fg := &FlagGroup{name: name, flagSet: pflag.NewFlagSet(name, pflag.ContinueOnError)} | ||
c.flagGroups = append(c.flagGroups, fg) | ||
return fg | ||
} | ||
|
||
type FlagGroup struct { | ||
name string | ||
description string | ||
flagSet *pflag.FlagSet | ||
} | ||
|
||
func (c *FlagGroup) Name() string { | ||
return c.name | ||
} | ||
|
||
func (c *FlagGroup) Description() string { | ||
return c.description | ||
} | ||
|
||
func (c *FlagGroup) SetDescription(description string) { | ||
c.description = description | ||
} | ||
|
||
func (c *FlagGroup) FlagSet() *pflag.FlagSet { | ||
return c.flagSet | ||
} | ||
|
||
var templateFuncs = template.FuncMap{ | ||
"trim": strings.TrimSpace, | ||
"trimRightSpace": trimRightSpace, | ||
"trimTrailingWhitespaces": trimRightSpace, | ||
} | ||
|
||
func trimRightSpace(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmdgroup | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCommandFlagGrouping(t *testing.T) { | ||
cmd := &cobra.Command{ | ||
Use: "test [flags]", | ||
Short: "test command", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Do nothing | ||
}, | ||
} | ||
|
||
wrappedCmd := NewCommandWithGroupFlag(cmd) | ||
jobGroup := wrappedCmd.AddFlagGroup("Job") | ||
fs := jobGroup.FlagSet() | ||
fs.String("job-name", "", "Name of the job") | ||
fs.String("job-type", "", "Type of the job") | ||
|
||
pipelineGroup := wrappedCmd.AddFlagGroup("Pipeline") | ||
fs = pipelineGroup.FlagSet() | ||
fs.String("pipeline-name", "", "Name of the pipeline") | ||
fs.String("pipeline-type", "", "Type of the pipeline") | ||
|
||
cmd.Flags().BoolP("bool", "b", false, "Bool flag") | ||
|
||
buf := bytes.NewBuffer(nil) | ||
cmd.SetOutput(buf) | ||
cmd.Usage() | ||
|
||
expected := `Usage: | ||
test [flags] | ||
Job Flags: | ||
--job-name string Name of the job | ||
--job-type string Type of the job | ||
Pipeline Flags: | ||
--pipeline-name string Name of the pipeline | ||
--pipeline-type string Type of the pipeline | ||
Flags: | ||
-b, --bool Bool flag` | ||
require.Equal(t, expected, buf.String()) | ||
} |
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,14 @@ | ||
package cmdgroup | ||
|
||
const usageTemplate = `Usage:{{if .Command.Runnable}} | ||
{{.Command.UseLine}}{{end}} | ||
{{range .FlagGroups}} | ||
{{.Name}} Flags:{{if not (eq .Description "")}} | ||
{{.Description}}{{end}} | ||
{{.FlagSet.FlagUsages | trimTrailingWhitespaces}} | ||
{{end}} | ||
{{if .Command.HasAvailableLocalFlags}}Flags: | ||
{{.Command.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .Command.HasAvailableInheritedFlags}} | ||
Global Flags: | ||
{{.Command.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}` |