Skip to content

Commit

Permalink
Fixes category filtering for unnamed cells (#598)
Browse files Browse the repository at this point in the history
* Enhances readability
* Fixes category filtering
* Adds more textar test cases

Closes: #580
  • Loading branch information
pastuxso authored Jun 3, 2024
1 parent cd962ab commit 699148d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 46 deletions.
72 changes: 35 additions & 37 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stateful/runme/v3/internal/project"
"github.com/stateful/runme/v3/internal/runner/client"
"github.com/stateful/runme/v3/internal/tui"
"github.com/stateful/runme/v3/pkg/document"
)

type CommandExportExtractMatch struct {
Expand All @@ -41,7 +42,7 @@ func runCmd() *cobra.Command {
parallel bool
replaceScripts []string
serverAddr string
categories []string
cmdCategories []string
getRunnerOpts func() ([]client.RunnerOption, error)
runIndex int
)
Expand All @@ -56,7 +57,7 @@ func runCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
runWithIndex := fFileMode && runIndex >= 0

runMany := runAll || (len(categories) > 0 && len(args) == 0)
runMany := runAll || (len(cmdCategories) > 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 @@ -86,50 +87,31 @@ func runCmd() *cobra.Command {
block := task.CodeBlock

// Check if to run all and if the block should be excluded
if runAll && len(categories) == 0 && block.ExcludeFromRunAll() {
if runAll && len(cmdCategories) == 0 && block.ExcludeFromRunAll() {
// Skip the task if it should be excluded from run all
continue
}

// Check if categories are specified and if the block should be excluded
if len(categories) > 0 && block.ExcludeFromRunAll() {
if len(cmdCategories) > 0 && block.ExcludeFromRunAll() {
// Skip the task if it should be excluded based on categories
continue
}

// Check if the block matches any of the specified categories
if len(categories) > 0 {
bcats := block.Categories()
if len(cmdCategories) > 0 {
blockCategories := block.Categories()
fm, _ := block.Document().Frontmatter()
fmCategories := resolveFrontmatterCategories(fm)
match := false

if fm != nil && fm.Category != "" {
// Check if the frontmatter category matches any block category
for _, bcat := range bcats {
if fm.Category == bcat {
match = true
break
}
}
if !match {
// Check if the frontmatter category matches any specified category
for _, cat := range categories {
if fm.Category == cat {
match = true
break
}
}
}
} else {
// Check if any block category matches any specified category
for _, bcat := range bcats {
for _, cat := range categories {
if bcat == cat {
match = true
break
}
}
if len(fmCategories) > 0 && containsCategories(fmCategories, cmdCategories) {
if len(blockCategories) == 0 {
match = true
} else {
match = containsCategories(fmCategories, blockCategories)
}
} else if containsCategories(blockCategories, cmdCategories) {
match = true
}

if !match {
Expand Down Expand Up @@ -239,7 +221,7 @@ func runCmd() *cobra.Command {
}

if runMany {
err := confirmExecution(cmd, len(runTasks), parallel, categories)
err := confirmExecution(cmd, len(runTasks), parallel, cmdCategories)
if err != nil {
return err
}
Expand Down Expand Up @@ -267,8 +249,8 @@ func runCmd() *cobra.Command {
if runMany && parallel {
scriptRunText = "Running"
blockNames = []string{blockColor.Sprint("all tasks")}
if len(categories) > 0 {
blockNames = []string{blockColor.Sprintf("tasks for categories %s", categories)}
if len(cmdCategories) > 0 {
blockNames = []string{blockColor.Sprintf("tasks for categories %s", cmdCategories)}
}
}

Expand Down Expand Up @@ -347,7 +329,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().StringArrayVarP(&categories, "category", "c", nil, "Run from a specific category.")
cmd.Flags().StringArrayVarP(&cmdCategories, "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 @@ -652,3 +634,19 @@ func confirmExecution(cmd *cobra.Command, numTasks int, parallel bool, categorie

return nil
}

func resolveFrontmatterCategories(fm *document.Frontmatter) []string {
if fm != nil && fm.Category != "" {
return []string{fm.Category}
}
return []string{}
}

func containsCategories(s1 []string, s2 []string) bool {
for _, element := range s2 {
if strings.Contains(strings.Join(s1, ","), element) {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion internal/runner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ func readLoop(
func runnerWinsizeToPty(winsize *runnerv1.Winsize) *pty.Winsize {
if winsize == nil {
// sane default
return &pty.Winsize{Cols: 80}
return &pty.Winsize{Rows: 24, Cols: 80}
}

return &pty.Winsize{
Expand Down
33 changes: 25 additions & 8 deletions testdata/categories/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ exec runme run -c buzz -c bar --filename=CATEGORIES.md
cmp stdout buzz-bar-list.txt
! stderr .

env SHELL=/bin/bash
exec runme run --all --allow-unnamed --skip-prompts --category solution-2
cmp stdout doc-category-with-unnamed.txt
! stderr .

env SHELL=/bin/bash
exec runme run --all --skip-prompts --category solution-2
cmp stdout doc-category.txt
Expand Down Expand Up @@ -65,7 +70,7 @@ $ stty -opost
$ echo "Deploy solution1"
```

```sh {"category":"delete-solution"}
```sh {"name":"foo","category":"delete-solution"}
echo "Delete solution"
```

Expand All @@ -78,18 +83,25 @@ category: solution-2

Installation steps for Solution 2:

```sh {"name":"install-solution2"}
```sh
$ stty -opost
$ echo "Install solution2"
```

Deployment steps for Solution 2:

```sh {"name":"deploy-solution2"}
```sh
$ stty -opost
$ echo "Deploy solution2"
```

Post-Deployment steps for Solution 2:

```sh {"name":"post-deployment-2"}
$ stty -opost
$ echo "Post-Deployment solution2"
```

```sh {"category":"delete-solution"}
$ echo "Delete solution"
```
Expand All @@ -112,10 +124,15 @@ bar!
► Running task print-buzz...
buzz!
► ✓ Task print-buzz exited with code 0
-- doc-category.txt --
► Running task install-solution2...
-- doc-category-with-unnamed.txt --
► Running task stty-opost...
Install solution2
► ✓ Task install-solution2 exited with code 0
► Running task deploy-solution2...
► ✓ Task stty-opost exited with code 0
► Running task stty-opost-2...
Deploy solution2
► ✓ Task deploy-solution2 exited with code 0
► ✓ Task stty-opost-2 exited with code 0
► Running task post-deployment-2...
Post-Deployment solution2
► ✓ Task post-deployment-2 exited with code 0
-- doc-category.txt --
Post-Deployment solution2

0 comments on commit 699148d

Please sign in to comment.