Skip to content

Commit

Permalink
keep folder
Browse files Browse the repository at this point in the history
  • Loading branch information
xxf098 committed Jan 19, 2023
1 parent 82c2425 commit 05fe136
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions do_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actionflow

import (
"fmt"
"path/filepath"
"testing"

"cuelang.org/go/cue"
Expand Down Expand Up @@ -197,3 +198,20 @@ func TestKeep(t *testing.T) {
t.Fatal(err)
}
}

func TestKeep1(t *testing.T) {
err := flowTest("./testcues/keep.cue", "keepSub")
if err != nil {
t.Fatal(err)
}
}

func TestKeep2(t *testing.T) {
p := "/home/abc/github/def/sub/trials/*"
name := "/home/abc/github/def/sub/trials/abc.txt"
m, err := filepath.Match(p, name)
if err != nil {
t.Fatal(err)
}
fmt.Println(m)
}
24 changes: 24 additions & 0 deletions plan/task/keep.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task

import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
Expand All @@ -21,6 +22,8 @@ func init() {
type keepTask struct {
}

// keep files: /abc/def/*.txt
// keep folder: /abc/def/hij/
func (t *keepTask) Run(ctx context.Context, v *cue.Value) (*cue.Value, error) {
paths := []string{}
pValue := v.LookupPath(cue.ParsePath("path"))
Expand Down Expand Up @@ -50,6 +53,10 @@ func (t *keepTask) Run(ctx context.Context, v *cue.Value) (*cue.Value, error) {
fullPaths := []string{}
for _, path := range paths {
if p, err := filepath.Abs(path); err == nil {
// keep path Separator
if os.IsPathSeparator(path[len(path)-1]) {
p = fmt.Sprintf("%s%s", p, path[len(path)-1:])
}
fullPaths = append(fullPaths, p)
}
}
Expand All @@ -58,6 +65,10 @@ func (t *keepTask) Run(ctx context.Context, v *cue.Value) (*cue.Value, error) {
dirs := []string{}
for _, v := range fullPaths {
dir := filepath.Dir(v)
// set /abc/def/ parent to /abc
if len(v) > 1 && os.IsPathSeparator(v[len(v)-1]) {
dir = filepath.Dir(dir)
}
found := false
for _, dir1 := range dirs {
if dir1 == dir {
Expand Down Expand Up @@ -115,7 +126,11 @@ func (t *keepTask) Name() string {

func reverseGlob(rootDir string, patterns []string) ([]string, error) {
paths := []string{}
dir := ":"
filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
if strings.HasPrefix(path, dir) {
return nil
}
if m, err := filepath.Match(rootDir, path); err != nil || m {
return nil
}
Expand All @@ -125,6 +140,15 @@ func reverseGlob(rootDir string, patterns []string) ([]string, error) {
if err != nil || m {
return nil
}
// /abc/def == /abc/def/
if os.IsPathSeparator(p[len(p)-1]) {
if strings.HasPrefix(path, p) || (d.IsDir() && fmt.Sprintf("%s%s", path, p[len(p)-1:]) == p) {
return nil
}
}
}
if d.IsDir() {
dir = fmt.Sprintf("%s%c", path, filepath.Separator)
}
paths = append(paths, path)
return nil
Expand Down
4 changes: 4 additions & 0 deletions testcues/keep.cue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ actionflow.#Plan & {
@$()
path: "./testkeep/foo*.txt"
}

keepSub: core.#Keep & {
path: "./sub/trials/"
}

}
}

0 comments on commit 05fe136

Please sign in to comment.