github.com/kat-co/cmd@v0.0.0-20140616103059-5da365f9d57e/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 "github.com/juju/utils" 11 ) 12 13 // FileVar represents a path to a file. 14 type FileVar struct { 15 Path string 16 } 17 18 var ErrNoPath = errors.New("path not set") 19 20 // Set stores the chosen path name in f.Path. 21 func (f *FileVar) Set(v string) error { 22 f.Path = v 23 return nil 24 } 25 26 // Read returns the contents of the file. 27 func (f *FileVar) Read(ctx *Context) ([]byte, error) { 28 if f.Path == "" { 29 return nil, ErrNoPath 30 } 31 path, err := utils.NormalizePath(f.Path) 32 if err != nil { 33 return nil, err 34 } 35 return ioutil.ReadFile(ctx.AbsPath(path)) 36 } 37 38 // String returns the path to the file. 39 func (f *FileVar) String() string { 40 return f.Path 41 }