Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing multiple categories #498

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)
Expand All @@ -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")
}
Expand Down Expand Up @@ -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(), ",")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add block.Categories() and mark block.Category() as deprecated using // Deprecated: Use Categories() instead.

match := false
for _, bcat := range bcats {
for _, cat := range categories {
if bcat == cat {
match = true
}
}
}

if !match {
continue
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 categories %s", categories)}
}
}

Expand Down Expand Up @@ -312,7 +321,7 @@ func runCmd() *cobra.Command {
cmd.Flags().BoolVarP(&parallel, "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")
Expand Down Expand Up @@ -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)"
Expand Down
17 changes: 17 additions & 0 deletions testdata/categories/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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
Expand All @@ -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
Loading