launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/filevar.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cmd
     5  
     6  import (
     7  	"errors"
     8  	"io/ioutil"
     9  )
    10  
    11  // FileVar represents a path to a file.
    12  type FileVar struct {
    13  	Path string
    14  }
    15  
    16  var ErrNoPath = errors.New("path not set")
    17  
    18  // Set stores the chosen path name in f.Path.
    19  func (f *FileVar) Set(v string) error {
    20  	f.Path = v
    21  	return nil
    22  }
    23  
    24  // Read returns the contents of the file.
    25  func (f *FileVar) Read(ctx *Context) ([]byte, error) {
    26  	if f.Path == "" {
    27  		return nil, ErrNoPath
    28  	}
    29  	return ioutil.ReadFile(ctx.AbsPath(f.Path))
    30  }
    31  
    32  // String returns the path to the file.
    33  func (f *FileVar) String() string {
    34  	return f.Path
    35  }