this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg/tool/file: add Mkdir, MkdirAll

Closes #420

Change-Id: I5b513e2b746ef3e3a1e9cb9013c8b7d649a367f2
Signed-off-by: Jean-Philippe Braun <eon@patapon.info>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/531316
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>

authored by

Jean-Philippe Braun and committed by
Paul Jolly
45c54f85 34c4f9cc

+144
+24
pkg/tool/file/doc.go
··· 66 66 // files: [...string] 67 67 // } 68 68 // 69 + // // Mkdir creates a directory at the specified path. 70 + // Mkdir: { 71 + // $id: "tool/file.Mkdir" 72 + // 73 + // // The directory path to create. 74 + // // If path is already a directory, Mkdir does nothing. 75 + // // If path already exists and is not a directory, Mkdir will return an error. 76 + // path: string 77 + // 78 + // // When true any necessary parents are created as well. 79 + // createParents: bool | *false 80 + // 81 + // // Directory mode and permission bits (before umask). 82 + // permissions: int | *0o755 83 + // } 84 + // 85 + // // MkdirAll creates a directory at the specified path along with any necessary 86 + // // parents. 87 + // // If path is already a directory, MkdirAll does nothing. 88 + // // If path already exists and is not a directory, MkdirAll will return an error. 89 + // MkdirAll: Mkdir & { 90 + // createParents: true 91 + // } 92 + // 69 93 package file
+24
pkg/tool/file/file.cue
··· 75 75 glob: !="" 76 76 files: [...string] 77 77 } 78 + 79 + // Mkdir creates a directory at the specified path. 80 + Mkdir: { 81 + $id: "tool/file.Mkdir" 82 + 83 + // The directory path to create. 84 + // If path is already a directory, Mkdir does nothing. 85 + // If path already exists and is not a directory, Mkdir will return an error. 86 + path: string 87 + 88 + // When true any necessary parents are created as well. 89 + createParents: bool | *false 90 + 91 + // Directory mode and permission bits (before umask). 92 + permissions: int | *0o755 93 + } 94 + 95 + // MkdirAll creates a directory at the specified path along with any necessary 96 + // parents. 97 + // If path is already a directory, MkdirAll does nothing. 98 + // If path already exists and is not a directory, MkdirAll will return an error. 99 + MkdirAll: Mkdir & { 100 + createParents: true 101 + }
+30
pkg/tool/file/file.go
··· 23 23 "path/filepath" 24 24 25 25 "cuelang.org/go/cue" 26 + "cuelang.org/go/cue/errors" 26 27 "cuelang.org/go/internal/task" 27 28 ) 28 29 ··· 31 32 task.Register("tool/file.Append", newAppendCmd) 32 33 task.Register("tool/file.Create", newCreateCmd) 33 34 task.Register("tool/file.Glob", newGlobCmd) 35 + task.Register("tool/file.Mkdir", newMkdirCmd) 34 36 } 35 37 36 38 func newReadCmd(v cue.Value) (task.Runner, error) { return &cmdRead{}, nil } 37 39 func newAppendCmd(v cue.Value) (task.Runner, error) { return &cmdAppend{}, nil } 38 40 func newCreateCmd(v cue.Value) (task.Runner, error) { return &cmdCreate{}, nil } 39 41 func newGlobCmd(v cue.Value) (task.Runner, error) { return &cmdGlob{}, nil } 42 + func newMkdirCmd(v cue.Value) (task.Runner, error) { return &cmdMkdir{}, nil } 40 43 41 44 type cmdRead struct{} 42 45 type cmdAppend struct{} 43 46 type cmdCreate struct{} 44 47 type cmdGlob struct{} 48 + type cmdMkdir struct{} 45 49 46 50 func (c *cmdRead) Run(ctx *task.Context) (res interface{}, err error) { 47 51 filename := ctx.String("filename") ··· 111 115 files := map[string]interface{}{"files": m} 112 116 return files, err 113 117 } 118 + 119 + func (c *cmdMkdir) Run(ctx *task.Context) (res interface{}, err error) { 120 + path := ctx.String("path") 121 + mode := ctx.Int64("permissions") 122 + createParents, _ := ctx.Lookup("createParents").Bool() 123 + 124 + if ctx.Err != nil { 125 + return nil, ctx.Err 126 + } 127 + 128 + if createParents { 129 + if err := os.MkdirAll(path, os.FileMode(mode)); err != nil { 130 + return nil, errors.Wrapf(err, ctx.Obj.Pos(), "failed to create directory") 131 + } 132 + } else { 133 + dir, err := os.Stat(path) 134 + if err == nil && dir.IsDir() { 135 + return nil, nil 136 + } 137 + if err := os.Mkdir(path, os.FileMode(mode)); err != nil { 138 + return nil, errors.Wrapf(err, ctx.Obj.Pos(), "failed to create directory") 139 + } 140 + } 141 + 142 + return nil, nil 143 + }
+57
pkg/tool/file/file_test.go
··· 137 137 t.Errorf("got %v; want %v", got, want) 138 138 } 139 139 } 140 + 141 + func TestMkdir(t *testing.T) { 142 + baseDir, err := os.MkdirTemp("", "") 143 + if err != nil { 144 + t.Fatal(err) 145 + } 146 + defer os.RemoveAll(baseDir) 147 + 148 + // simple dir creation 149 + d1 := filepath.Join(baseDir, "foo") 150 + v := parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, d1)) 151 + _, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v}) 152 + if err != nil { 153 + t.Fatal(err) 154 + } 155 + fi1, err := os.Stat(d1) 156 + if err != nil { 157 + t.Fatal(err) 158 + } 159 + if !fi1.IsDir() { 160 + t.Fatal("not a directory") 161 + } 162 + 163 + // dir already exists 164 + v = parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, d1)) 165 + _, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v}) 166 + if err != nil { 167 + t.Fatal(err) 168 + } 169 + 170 + // create parents 171 + // set permissions 172 + d2 := filepath.Join(baseDir, "bar/x") 173 + v = parse(t, "tool/file.MkdirAll", fmt.Sprintf(`{path: #"%s"#, permissions: 0o700}`, d2)) 174 + _, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v}) 175 + if err != nil { 176 + t.Fatal(err) 177 + } 178 + fi2, err := os.Stat(d2) 179 + if err != nil { 180 + t.Fatal(err) 181 + } 182 + if !fi2.IsDir() { 183 + t.Fatal("not a directory") 184 + } 185 + 186 + // file at same path 187 + f, err := ioutil.TempFile(baseDir, "") 188 + if err != nil { 189 + t.Fatal(err) 190 + } 191 + v = parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, f.Name())) 192 + _, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v}) 193 + if err == nil { 194 + t.Fatal("should not create directory at existing filepath") 195 + } 196 + }
+9
pkg/tool/file/pkg.go
··· 41 41 glob: !="" 42 42 files: [...string] 43 43 } 44 + Mkdir: { 45 + $id: "tool/file.Mkdir" 46 + path: string 47 + createParents: bool | *false 48 + permissions: int | *493 49 + } 50 + MkdirAll: Mkdir & { 51 + createParents: true 52 + } 44 53 }`, 45 54 }