github.com/solo-io/cue@v0.4.7/pkg/tool/file/file.go (about)

     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  
    15  package file
    16  
    17  //go:generate go run gen.go
    18  //go:generate gofmt -s -w .
    19  
    20  import (
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/solo-io/cue/cue"
    26  	"github.com/solo-io/cue/internal/task"
    27  )
    28  
    29  func init() {
    30  	task.Register("tool/file.Read", newReadCmd)
    31  	task.Register("tool/file.Append", newAppendCmd)
    32  	task.Register("tool/file.Create", newCreateCmd)
    33  	task.Register("tool/file.Glob", newGlobCmd)
    34  }
    35  
    36  func newReadCmd(v cue.Value) (task.Runner, error)   { return &cmdRead{}, nil }
    37  func newAppendCmd(v cue.Value) (task.Runner, error) { return &cmdAppend{}, nil }
    38  func newCreateCmd(v cue.Value) (task.Runner, error) { return &cmdCreate{}, nil }
    39  func newGlobCmd(v cue.Value) (task.Runner, error)   { return &cmdGlob{}, nil }
    40  
    41  type cmdRead struct{}
    42  type cmdAppend struct{}
    43  type cmdCreate struct{}
    44  type cmdGlob struct{}
    45  
    46  func (c *cmdRead) Run(ctx *task.Context) (res interface{}, err error) {
    47  	filename := ctx.String("filename")
    48  	if ctx.Err != nil {
    49  		return nil, ctx.Err
    50  	}
    51  
    52  	b, err := ioutil.ReadFile(filename)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	update := map[string]interface{}{"contents": b}
    57  
    58  	switch v := ctx.Lookup("contents"); v.IncompleteKind() {
    59  	case cue.BytesKind:
    60  		// already set above
    61  	case cue.StringKind:
    62  		update["contents"] = string(b)
    63  	}
    64  	return update, nil
    65  }
    66  
    67  func (c *cmdAppend) Run(ctx *task.Context) (res interface{}, err error) {
    68  	var (
    69  		filename = filepath.FromSlash(ctx.String("filename"))
    70  		mode     = ctx.Int64("permissions")
    71  		b        = ctx.Bytes("contents")
    72  	)
    73  	if ctx.Err != nil {
    74  		return nil, ctx.Err
    75  	}
    76  
    77  	f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.FileMode(mode))
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	defer f.Close()
    82  
    83  	if _, err := f.Write(b); err != nil {
    84  		return nil, err
    85  	}
    86  	return nil, nil
    87  }
    88  
    89  func (c *cmdCreate) Run(ctx *task.Context) (res interface{}, err error) {
    90  	var (
    91  		filename = filepath.FromSlash(ctx.String("filename"))
    92  		mode     = ctx.Int64("permissions")
    93  		b        = ctx.Bytes("contents")
    94  	)
    95  	if ctx.Err != nil {
    96  		return nil, ctx.Err
    97  	}
    98  
    99  	return nil, ioutil.WriteFile(filename, b, os.FileMode(mode))
   100  }
   101  
   102  func (c *cmdGlob) Run(ctx *task.Context) (res interface{}, err error) {
   103  	glob := ctx.String("glob")
   104  	if ctx.Err != nil {
   105  		return nil, ctx.Err
   106  	}
   107  	m, err := filepath.Glob(glob)
   108  	for i, s := range m {
   109  		m[i] = filepath.ToSlash(s)
   110  	}
   111  	files := map[string]interface{}{"files": m}
   112  	return files, err
   113  }