1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.
package cmd
import (
"errors"
"io"
"io/ioutil"
"os"
)
// FileVar represents a path to a file.
type FileVar struct {
// Path is the path to the file.
Path string
// StdinMarkers are the Path values that should be interpreted as
// stdin. If it is empty then stdin is not supported.
StdinMarkers []string
}
var ErrNoPath = errors.New("path not set")
// Set stores the chosen path name in f.Path.
func (f *FileVar) Set(v string) error {
f.Path = v
return nil
}
// SetStdin sets StdinMarkers to the provided strings. If none are
// provided then the default of "-" is used.
func (f *FileVar) SetStdin(markers ...string) {
if len(markers) == 0 {
markers = append(markers, "-")
}
f.StdinMarkers = markers
}
// IsStdin determines whether or not the path represents stdin.
func (f FileVar) IsStdin() bool {
for _, marker := range f.StdinMarkers {
if f.Path == marker {
return true
}
}
return false
}
// Open opens the file.
func (f *FileVar) Open(ctx *Context) (io.ReadCloser, error) {
if f.Path == "" {
return nil, ErrNoPath
}
if f.IsStdin() {
return ioutil.NopCloser(ctx.Stdin), nil
}
path, err := normalizePath(f.Path)
if err != nil {
return nil, err
}
return os.Open(ctx.AbsPath(path))
}
// Read returns the contents of the file.
func (f *FileVar) Read(ctx *Context) ([]byte, error) {
if f.Path == "" {
return nil, ErrNoPath
}
if f.IsStdin() {
return ioutil.ReadAll(ctx.Stdin)
}
path, err := normalizePath(f.Path)
if err != nil {
return nil, err
}
return ioutil.ReadFile(ctx.AbsPath(path))
}
// String returns the path to the file.
func (f *FileVar) String() string {
return f.Path
}
|