Fix validateMixedArgs for phases

pull/58/head
fabriziopandini 2018-11-15 11:38:18 +01:00
parent 4fdac19603
commit 807338e96a
3 changed files with 17 additions and 5 deletions

View File

@ -178,7 +178,7 @@ func NewCmdInit(out io.Writer) *cobra.Command {
// sets the data builder function, that will be used by the runner
// both when running the entire workflow or single phases
initRunner.SetDataInitializer(func() (workflow.RunData, error) {
initRunner.SetDataInitializer(func(cmd *cobra.Command) (workflow.RunData, error) {
return newInitData(cmd, initOptions, out)
})

View File

@ -19,6 +19,8 @@ package workflow
import (
"errors"
"fmt"
"github.com/spf13/cobra"
)
var myWorkflowRunner = NewRunner()
@ -100,7 +102,7 @@ func ExampleRunner_Run() {
// Defines the method that creates the runtime data shared
// among all the phases included in the workflow
myWorkflowRunner.SetDataInitializer(func() (RunData, error) {
myWorkflowRunner.SetDataInitializer(func(cmd *cobra.Command) (RunData, error) {
return myWorkflowData{data: "some data"}, nil
})

View File

@ -53,13 +53,17 @@ type Runner struct {
// runDataInitializer defines a function that creates the runtime data shared
// among all the phases included in the workflow
runDataInitializer func() (RunData, error)
runDataInitializer func(*cobra.Command) (RunData, error)
// runData is part of the internal state of the runner and it is used for implementing
// a singleton in the InitData methods (thus avoiding to initialize data
// more than one time)
runData RunData
// runCmd is part of the internal state of the runner and it is used to track the
// command that will trigger the runner (only if the runner is BindToCommand).
runCmd *cobra.Command
// cmdAdditionalFlags holds additional, shared flags that could be added to the subcommands generated
// for phases. Flags could be inherited from the parent command too or added directly to each phase
cmdAdditionalFlags *pflag.FlagSet
@ -166,7 +170,8 @@ func (e *Runner) computePhaseRunFlags() (map[string]bool, error) {
// SetDataInitializer allows to setup a function that initialize the runtime data shared
// among all the phases included in the workflow.
func (e *Runner) SetDataInitializer(builder func() (RunData, error)) {
// The method will receive in input the cmd that triggers the Runner (only if the runner is BindToCommand)
func (e *Runner) SetDataInitializer(builder func(cmd *cobra.Command) (RunData, error)) {
e.runDataInitializer = builder
}
@ -176,7 +181,7 @@ func (e *Runner) SetDataInitializer(builder func() (RunData, error)) {
func (e *Runner) InitData() (RunData, error) {
if e.runData == nil && e.runDataInitializer != nil {
var err error
if e.runData, err = e.runDataInitializer(); err != nil {
if e.runData, err = e.runDataInitializer(e.runCmd); err != nil {
return nil, err
}
}
@ -315,6 +320,8 @@ func (e *Runner) BindToCommand(cmd *cobra.Command) {
Example: p.Example,
Aliases: p.Aliases,
Run: func(cmd *cobra.Command, args []string) {
// overrides the command triggering the Runner using the phaseCmd
e.runCmd = cmd
e.Options.FilterPhases = []string{p.generatedName}
if err := e.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
@ -360,6 +367,9 @@ func (e *Runner) BindToCommand(cmd *cobra.Command) {
// adds phase related flags to the main command
cmd.Flags().StringSliceVar(&e.Options.SkipPhases, "skip-phases", nil, "List of phases to be skipped")
// keep tracks of the command triggering the runner
e.runCmd = cmd
}
func inheritsFlags(sourceFlags, targetFlags *pflag.FlagSet, cmdFlags []string) {