From 34b8c530a7e1b37984f53df609337ceb6da57e79 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Tue, 13 Feb 2024 15:36:39 -0500 Subject: [PATCH 1/3] Enable running multiple categories from the CLI --- internal/cmd/run.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/internal/cmd/run.go b/internal/cmd/run.go index 00a939c3f..d722fc8f5 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -21,7 +21,6 @@ import ( "github.com/stateful/runme/internal/project" "github.com/stateful/runme/internal/runner/client" "github.com/stateful/runme/internal/tui" - "golang.org/x/exp/slices" ) const ( @@ -45,7 +44,7 @@ func runCmd() *cobra.Command { parallel bool replaceScripts []string serverAddr string - category string + categories []string getRunnerOpts func() ([]client.RunnerOption, error) runIndex int ) @@ -60,7 +59,7 @@ func runCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { runWithIndex := fFileMode && runIndex >= 0 - runMany := runAll || (category != "" && len(args) == 0) + runMany := runAll || (len(categories) > 0 && len(args) == 0) if !runMany && len(args) == 0 && !runWithIndex { return errors.New("must provide at least one command to run") } @@ -89,16 +88,26 @@ func runCmd() *cobra.Command { for _, task := range tasks { block := task.CodeBlock - if runAll && block.ExcludeFromRunAll() { + if runAll && len(categories) == 0 && block.ExcludeFromRunAll() { continue } - if category != "" { + if len(categories) > 0 { if block.ExcludeFromRunAll() { continue } - if !slices.Contains(strings.Split(block.Category(), ","), category) { + bcats := strings.Split(block.Category(), ",") + match := false + for _, bcat := range bcats { + for _, cat := range categories { + if bcat == cat { + match = true + } + } + } + + if !match { continue } } @@ -201,7 +210,7 @@ func runCmd() *cobra.Command { } if runMany { - err := confirmExecution(cmd, len(runTasks), parallel, category) + err := confirmExecution(cmd, len(runTasks), parallel, categories) if err != nil { return err } @@ -229,8 +238,8 @@ func runCmd() *cobra.Command { if runMany && parallel { scriptRunText = "Running" blockNames = []string{blockColor.Sprint("all tasks")} - if category != "" { - blockNames = []string{blockColor.Sprintf("tasks for category %s", category)} + if len(categories) > 0 { + blockNames = []string{blockColor.Sprintf("tasks for category %s", categories)} } } @@ -312,7 +321,7 @@ func runCmd() *cobra.Command { cmd.Flags().BoolVarP(¶llel, "parallel", "p", false, "Run tasks in parallel.") cmd.Flags().BoolVarP(&runAll, "all", "a", false, "Run all commands.") cmd.Flags().BoolVar(&skipPrompts, "skip-prompts", false, "Skip prompting for variables.") - cmd.Flags().StringVarP(&category, "category", "c", "", "Run from a specific category.") + cmd.Flags().StringArrayVarP(&categories, "category", "c", nil, "Run from a specific category.") cmd.Flags().IntVarP(&runIndex, "index", "i", -1, "Index of command to run, 0-based. (Ignored in project mode)") cmd.PreRun = func(cmd *cobra.Command, args []string) { skipPromptsExplicitly = cmd.Flags().Changed("skip-prompts") @@ -589,10 +598,10 @@ func promptForRun(cmd *cobra.Command, tasks []project.Task) (project.Task, error return *result.(blockPromptItem).task, nil } -func confirmExecution(cmd *cobra.Command, numTasks int, parallel bool, category string) error { +func confirmExecution(cmd *cobra.Command, numTasks int, parallel bool, categories []string) error { text := fmt.Sprintf("Run all %d tasks", numTasks) - if category != "" { - text = fmt.Sprintf("Run %d tasks for category %s", numTasks, category) + if categories != nil { + text = fmt.Sprintf("Run %d tasks for categories: %s", numTasks, strings.Join(categories, ", ")) } if parallel { text += " (in parallel)" From fcb6f8ea84a0eac21fe5dd3b133de5b425d36c5a Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Tue, 13 Feb 2024 16:50:08 -0500 Subject: [PATCH 2/3] Add multi category test case --- testdata/categories/basic.txtar | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/testdata/categories/basic.txtar b/testdata/categories/basic.txtar index 1f3551930..6f178dc5d 100644 --- a/testdata/categories/basic.txtar +++ b/testdata/categories/basic.txtar @@ -8,6 +8,11 @@ exec runme run --all --category=bar --filename=CATEGORIES.md cmp stdout bar-list.txt ! stderr . +env SHELL=/bin/bash +exec runme run -c buzz -c bar --filename=CATEGORIES.md +cmp stdout buzz-bar-list.txt +! stderr . + -- CATEGORIES.md -- ```bash {"category":"foo","name":"set-env"} @@ -29,6 +34,11 @@ $ stty -opost $ echo "excluded!" ``` +```bash {"category":"buzz","name":"print-buzz"} +$ stty -opost +$ echo "buzz!" +``` + -- foo-bar-list.txt -- ► Running task set-env... ► ✓ Task set-env exited with code 0 @@ -40,3 +50,10 @@ bar! ► ✓ Task print-bar exited with code 0 -- bar-list.txt -- bar! +-- buzz-bar-list.txt -- + ► Running task print-bar... +bar! + ► ✓ Task print-bar exited with code 0 + ► Running task print-buzz... +buzz! + ► ✓ Task print-buzz exited with code 0 From 4620f67c9111587e100537f69b6b3daaa0c02f71 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Thu, 15 Feb 2024 10:12:48 -0500 Subject: [PATCH 3/3] Plural in output message Co-authored-by: Adam Babik --- internal/cmd/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/run.go b/internal/cmd/run.go index d722fc8f5..b34d7344f 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -239,7 +239,7 @@ func runCmd() *cobra.Command { scriptRunText = "Running" blockNames = []string{blockColor.Sprint("all tasks")} if len(categories) > 0 { - blockNames = []string{blockColor.Sprintf("tasks for category %s", categories)} + blockNames = []string{blockColor.Sprintf("tasks for categories %s", categories)} } }