|
| 1 | +package execstage |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/sdsc-ordes/quitsh/pkg/cli" |
| 7 | + "github.com/sdsc-ordes/quitsh/pkg/cli/general" |
| 8 | + "github.com/sdsc-ordes/quitsh/pkg/component/stage" |
| 9 | + "github.com/sdsc-ordes/quitsh/pkg/dag" |
| 10 | + "github.com/sdsc-ordes/quitsh/pkg/errors" |
| 11 | + "github.com/sdsc-ordes/quitsh/pkg/toolchain" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +type ( |
| 17 | + Option func(*opts) |
| 18 | + |
| 19 | + opts struct { |
| 20 | + name string |
| 21 | + modify func(cmd *cobra.Command) |
| 22 | + } |
| 23 | +) |
| 24 | + |
| 25 | +// AddCmdGeneral adds the general `exec-stage` command to execute all targets |
| 26 | +// in given stages. |
| 27 | +func AddCmdGeneral( |
| 28 | + cli cli.ICLI, |
| 29 | + parent *cobra.Command, |
| 30 | + execArgs *dag.ExecArgs, |
| 31 | +) { |
| 32 | + var compArgs general.ComponentArgs |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "exec-stage [stage...]", |
| 35 | + Short: "Execute all targets in a stage.", |
| 36 | + RunE: func(_ *cobra.Command, stages []string) error { |
| 37 | + for _, s := range stages { |
| 38 | + e := ExecuteStage(cli, &compArgs, stage.Stage(s), execArgs) |
| 39 | + if e != nil { |
| 40 | + return e |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return nil |
| 45 | + }, |
| 46 | + } |
| 47 | + general.AddFlagsExecArgs(cmd, execArgs) |
| 48 | + general.AddFlagsComponentArgs(cmd, &compArgs) |
| 49 | + |
| 50 | + parent.AddCommand(cmd) |
| 51 | +} |
| 52 | + |
| 53 | +// AddCmdAlias adds a generalized command similar to `AddCmdGeneral` but |
| 54 | +// with name either from [WithName] or defaulted to `stage`. |
| 55 | +// Its possible to modify the command with [WithModifications]. |
| 56 | +func AddCmdAlias( |
| 57 | + cli cli.ICLI, |
| 58 | + parent *cobra.Command, |
| 59 | + stage stage.Stage, |
| 60 | + execArgs *dag.ExecArgs, |
| 61 | + opt ...Option, |
| 62 | +) { |
| 63 | + var o opts |
| 64 | + o.Apply(opt...) |
| 65 | + |
| 66 | + if o.name == "" { |
| 67 | + o.name = stage.String() |
| 68 | + } |
| 69 | + |
| 70 | + var compArgs general.ComponentArgs |
| 71 | + |
| 72 | + cmd := &cobra.Command{ |
| 73 | + Use: o.name, |
| 74 | + Short: fmt.Sprintf("Execute all targets in stage %v.", stage), |
| 75 | + RunE: func(_ *cobra.Command, _args []string) error { |
| 76 | + return ExecuteStage(cli, &compArgs, stage, execArgs) |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + general.AddFlagsExecArgs(cmd, execArgs) |
| 81 | + general.AddFlagsComponentArgs(cmd, &compArgs) |
| 82 | + |
| 83 | + if o.modify != nil { |
| 84 | + o.modify(cmd) |
| 85 | + } |
| 86 | + |
| 87 | + parent.AddCommand(cmd) |
| 88 | +} |
| 89 | + |
| 90 | +// ExecuteStage executes all targets found with `compArgs` which belong to stage `stage`. |
| 91 | +func ExecuteStage( |
| 92 | + cl cli.ICLI, |
| 93 | + compArgs *general.ComponentArgs, |
| 94 | + stage stage.Stage, |
| 95 | + execArgs *dag.ExecArgs, |
| 96 | +) error { |
| 97 | + comps, all, rootDir, err := cl.FindComponents(compArgs) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + targets, prios, err := dag.DefineExecutionOrder( |
| 103 | + all, rootDir, |
| 104 | + dag.WithTargetsByStageFromComponents(comps, stage), |
| 105 | + ) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } else if len(targets) == 0 { |
| 109 | + return errors.New("no targets selected") |
| 110 | + } |
| 111 | + |
| 112 | + var dispatcher toolchain.IDispatcher |
| 113 | + if !cl.RootArgs().SkipToolchainDispatch { |
| 114 | + dispatcher = cl.ToolchainDispatcher() |
| 115 | + } |
| 116 | + |
| 117 | + return dag.Execute( |
| 118 | + targets, |
| 119 | + prios, |
| 120 | + cl.RunnerFactory(), |
| 121 | + dispatcher, |
| 122 | + cl.Config(), |
| 123 | + rootDir, |
| 124 | + cl.RootArgs().Parallel, |
| 125 | + dag.WithTags(execArgs.Tags...), |
| 126 | + ) |
| 127 | +} |
| 128 | + |
| 129 | +// Apply applies all options. |
| 130 | +func (c *opts) Apply(options ...Option) { |
| 131 | + for _, f := range options { |
| 132 | + f(c) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// WithName sets the command name to use. |
| 137 | +func WithName(name string) Option { |
| 138 | + return func(o *opts) { |
| 139 | + o.name = name |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// WithModifications sets a modification function to change the command. |
| 144 | +func WithModifications(mod func(cmd *cobra.Command)) Option { |
| 145 | + return func(o *opts) { |
| 146 | + o.modify = mod |
| 147 | + } |
| 148 | +} |
0 commit comments