Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 1 | // Copyright 2019 CUE Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 15 | package file |
| 16 | |
Marcel van Lohuizen | 42ebafe | 2019-04-27 01:24:54 +0200 | [diff] [blame] | 17 | //go:generate go run gen.go |
| 18 | |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 19 | import ( |
| 20 | "io/ioutil" |
| 21 | "os" |
| 22 | "path/filepath" |
| 23 | |
| 24 | "cuelang.org/go/cue" |
| 25 | "cuelang.org/go/internal/task" |
| 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | task.Register("tool/file.Read", newReadCmd) |
| 30 | task.Register("tool/file.Append", newAppendCmd) |
| 31 | task.Register("tool/file.Create", newCreateCmd) |
| 32 | task.Register("tool/file.Glob", newGlobCmd) |
| 33 | } |
| 34 | |
| 35 | func newReadCmd(v cue.Value) (task.Runner, error) { return &cmdRead{}, nil } |
| 36 | func newAppendCmd(v cue.Value) (task.Runner, error) { return &cmdAppend{}, nil } |
| 37 | func newCreateCmd(v cue.Value) (task.Runner, error) { return &cmdCreate{}, nil } |
| 38 | func newGlobCmd(v cue.Value) (task.Runner, error) { return &cmdGlob{}, nil } |
| 39 | |
| 40 | type cmdRead struct{} |
| 41 | type cmdAppend struct{} |
| 42 | type cmdCreate struct{} |
| 43 | type cmdGlob struct{} |
| 44 | |
| 45 | func lookupStr(v cue.Value, str string) string { |
| 46 | str, _ = v.Lookup(str).String() |
| 47 | return str |
| 48 | } |
| 49 | |
| 50 | func (c *cmdRead) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) { |
| 51 | b, err := ioutil.ReadFile(lookupStr(v, "filename")) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | update := map[string]interface{}{"contents": b} |
| 56 | |
Marcel van Lohuizen | d99e32e | 2019-08-26 14:42:14 +0200 | [diff] [blame] | 57 | switch v.Lookup("contents").IncompleteKind() { |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 58 | case cue.BytesKind: |
| 59 | case cue.StringKind: |
| 60 | update["contents"] = string(b) |
| 61 | } |
| 62 | return update, nil |
| 63 | } |
| 64 | |
| 65 | func (c *cmdAppend) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) { |
| 66 | filename := lookupStr(v, "filename") |
Marcel van Lohuizen | a1d3b2d | 2019-06-18 14:32:36 +0200 | [diff] [blame] | 67 | filename = filepath.FromSlash(filename) |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 68 | mode, err := v.Lookup("permissions").Int64() |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, os.FileMode(mode)) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | defer f.Close() |
| 78 | |
| 79 | b, _ := v.Lookup("contents").Bytes() |
| 80 | if _, err := f.Write(b); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | return nil, nil |
| 84 | } |
| 85 | |
| 86 | func (c *cmdCreate) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) { |
| 87 | filename := lookupStr(v, "filename") |
Marcel van Lohuizen | a1d3b2d | 2019-06-18 14:32:36 +0200 | [diff] [blame] | 88 | filename = filepath.FromSlash(filename) |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 89 | mode, err := v.Lookup("permissions").Int64() |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | |
| 94 | b, _ := v.Lookup("contents").Bytes() |
| 95 | return nil, ioutil.WriteFile(filename, b, os.FileMode(mode)) |
| 96 | } |
| 97 | |
| 98 | func (c *cmdGlob) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) { |
Marcel van Lohuizen | a1d3b2d | 2019-06-18 14:32:36 +0200 | [diff] [blame] | 99 | glob := filepath.FromSlash(lookupStr(v, "glob")) |
| 100 | m, err := filepath.Glob(glob) |
| 101 | for i, s := range m { |
| 102 | m[i] = filepath.ToSlash(s) |
| 103 | } |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 104 | return m, err |
| 105 | } |