Skip to content

Commit

Permalink
Revert "pkg/tool/file: add Mkdir, MkdirAll"
Browse files Browse the repository at this point in the history
This reverts commit 23cc102.

Reason: CL 530736 accidentally submitted without running trybots (but
was +2-ed). If the tests passed this would perhaps be less of an issue
but unfortunately the Windows build fails.

Signed-off-by: Paul Jolly <paul@myitcv.io>
Change-Id: I1f6ba72ddb2fac369b97c7f73f3a5d31a6f2cbc9
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/531318
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
myitcv committed Jan 17, 2022
1 parent 23cc102 commit 34c4f9c
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 152 deletions.
24 changes: 0 additions & 24 deletions pkg/tool/file/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions pkg/tool/file/file.cue
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,3 @@ Glob: {
glob: !=""
files: [...string]
}

// Mkdir creates a directory at the specified path.
Mkdir: {
$id: "tool/file.Mkdir"

// The directory path to create.
// If path is already a directory, Mkdir does nothing.
// If path already exists and is not a directory, Mkdir will return an error.
path: string

// When true any necessary parents are created as well.
createParents: bool | *false

// Directory mode and permission bits.
permissions: int | *0o755
}

// MkdirAll creates a directory at the specified path along with any necessary
// parents.
// If path is already a directory, MkdirAll does nothing.
// If path already exists and is not a directory, MkdirAll will return an error.
MkdirAll: Mkdir & {
createParents: true
}
30 changes: 0 additions & 30 deletions pkg/tool/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"path/filepath"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal/task"
)

Expand All @@ -32,20 +31,17 @@ func init() {
task.Register("tool/file.Append", newAppendCmd)
task.Register("tool/file.Create", newCreateCmd)
task.Register("tool/file.Glob", newGlobCmd)
task.Register("tool/file.Mkdir", newMkdirCmd)
}

func newReadCmd(v cue.Value) (task.Runner, error) { return &cmdRead{}, nil }
func newAppendCmd(v cue.Value) (task.Runner, error) { return &cmdAppend{}, nil }
func newCreateCmd(v cue.Value) (task.Runner, error) { return &cmdCreate{}, nil }
func newGlobCmd(v cue.Value) (task.Runner, error) { return &cmdGlob{}, nil }
func newMkdirCmd(v cue.Value) (task.Runner, error) { return &cmdMkdir{}, nil }

type cmdRead struct{}
type cmdAppend struct{}
type cmdCreate struct{}
type cmdGlob struct{}
type cmdMkdir struct{}

func (c *cmdRead) Run(ctx *task.Context) (res interface{}, err error) {
filename := ctx.String("filename")
Expand Down Expand Up @@ -115,29 +111,3 @@ func (c *cmdGlob) Run(ctx *task.Context) (res interface{}, err error) {
files := map[string]interface{}{"files": m}
return files, err
}

func (c *cmdMkdir) Run(ctx *task.Context) (res interface{}, err error) {
path := ctx.String("path")
mode := ctx.Int64("permissions")
createParents, _ := ctx.Lookup("createParents").Bool()

if ctx.Err != nil {
return nil, ctx.Err
}

if createParents {
if err := os.MkdirAll(path, os.FileMode(mode)); err != nil {
return nil, errors.Wrapf(err, ctx.Obj.Pos(), "failed to create directory")
}
} else {
dir, err := os.Stat(path)
if err == nil && dir.IsDir() {
return nil, nil
}
if err := os.Mkdir(path, os.FileMode(mode)); err != nil {
return nil, errors.Wrapf(err, ctx.Obj.Pos(), "failed to create directory")
}
}

return nil, nil
}
65 changes: 0 additions & 65 deletions pkg/tool/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,68 +137,3 @@ func TestGlob(t *testing.T) {
t.Errorf("got %v; want %v", got, want)
}
}

func TestMkdir(t *testing.T) {
baseDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(baseDir)

// simple dir creation
d1 := filepath.Join(baseDir, "foo")
v := parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: "%s"}`, d1))
_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
if err != nil {
t.Fatal(err)
}
fi1, err := os.Stat(d1)
if err != nil {
t.Fatal(err)
}
if !fi1.IsDir() {
t.Fatal("not a directory")
}
expectedMode1 := os.ModeDir | 0755
if int(fi1.Mode()) != int(expectedMode1) {
t.Fatal("wrong permissions", fi1.Mode())
}

// dir already exists
v = parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: "%s"}`, d1))
_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
if err != nil {
t.Fatal(err)
}

// create parents
// set permissions
d2 := filepath.Join(baseDir, "bar/x")
v = parse(t, "tool/file.MkdirAll", fmt.Sprintf(`{path: "%s", permissions: 0o700}`, d2))
_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
if err != nil {
t.Fatal(err)
}
fi2, err := os.Stat(d2)
if err != nil {
t.Fatal(err)
}
if !fi2.IsDir() {
t.Fatal("not a directory")
}
expectedMode2 := os.ModeDir | 0700
if int(fi2.Mode()) != int(expectedMode2) {
t.Fatal("wrong permissions", fi2.Mode())
}

// file at same path
f, err := ioutil.TempFile(baseDir, "")
if err != nil {
t.Fatal(err)
}
v = parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: "%s"}`, f.Name()))
_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
if err == nil {
t.Fatal("should not create directory at existing filepath")
}
}
9 changes: 0 additions & 9 deletions pkg/tool/file/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34c4f9c

Please sign in to comment.