blob: 694f8329bbe6eba966b6d694b6efc1bb749fe4b9 [file] [log] [blame]
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +02001// 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 Lohuizen83b33c02019-04-26 22:42:16 +020015package file
16
Marcel van Lohuizen42ebafe2019-04-27 01:24:54 +020017//go:generate go run gen.go
18
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020019import (
20 "io/ioutil"
21 "os"
22 "path/filepath"
23
24 "cuelang.org/go/cue"
25 "cuelang.org/go/internal/task"
26)
27
28func 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
35func newReadCmd(v cue.Value) (task.Runner, error) { return &cmdRead{}, nil }
36func newAppendCmd(v cue.Value) (task.Runner, error) { return &cmdAppend{}, nil }
37func newCreateCmd(v cue.Value) (task.Runner, error) { return &cmdCreate{}, nil }
38func newGlobCmd(v cue.Value) (task.Runner, error) { return &cmdGlob{}, nil }
39
40type cmdRead struct{}
41type cmdAppend struct{}
42type cmdCreate struct{}
43type cmdGlob struct{}
44
45func lookupStr(v cue.Value, str string) string {
46 str, _ = v.Lookup(str).String()
47 return str
48}
49
50func (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 Lohuizend99e32e2019-08-26 14:42:14 +020057 switch v.Lookup("contents").IncompleteKind() {
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020058 case cue.BytesKind:
59 case cue.StringKind:
60 update["contents"] = string(b)
61 }
62 return update, nil
63}
64
65func (c *cmdAppend) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) {
66 filename := lookupStr(v, "filename")
Marcel van Lohuizena1d3b2d2019-06-18 14:32:36 +020067 filename = filepath.FromSlash(filename)
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020068 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
86func (c *cmdCreate) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) {
87 filename := lookupStr(v, "filename")
Marcel van Lohuizena1d3b2d2019-06-18 14:32:36 +020088 filename = filepath.FromSlash(filename)
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020089 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
98func (c *cmdGlob) Run(ctx *task.Context, v cue.Value) (res interface{}, err error) {
Marcel van Lohuizena1d3b2d2019-06-18 14:32:36 +020099 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 Lohuizen83b33c02019-04-26 22:42:16 +0200104 return m, err
105}