gopkg.in/juju/charm.v6-unstable@v6.0.0-20171026192109-50d0c219b496/bundledir.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the LGPLv3, see LICENCE file for details. 3 4 package charm 5 6 import ( 7 "fmt" 8 "io" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 ) 13 14 type BundleDir struct { 15 Path string 16 data *BundleData 17 readMe string 18 } 19 20 // Trick to ensure *BundleDir implements the Bundle interface. 21 var _ Bundle = (*BundleDir)(nil) 22 23 // ReadBundleDir returns a BundleDir representing an expanded 24 // bundle directory. It does not verify the bundle data. 25 func ReadBundleDir(path string) (dir *BundleDir, err error) { 26 dir = &BundleDir{Path: path} 27 file, err := os.Open(dir.join("bundle.yaml")) 28 if err != nil { 29 return nil, err 30 } 31 dir.data, err = ReadBundleData(file) 32 file.Close() 33 if err != nil { 34 return nil, err 35 } 36 readMe, err := ioutil.ReadFile(dir.join("README.md")) 37 if err != nil { 38 return nil, fmt.Errorf("cannot read README file: %v", err) 39 } 40 dir.readMe = string(readMe) 41 return dir, nil 42 } 43 44 func (dir *BundleDir) Data() *BundleData { 45 return dir.data 46 } 47 48 func (dir *BundleDir) ReadMe() string { 49 return dir.readMe 50 } 51 52 func (dir *BundleDir) ArchiveTo(w io.Writer) error { 53 return writeArchive(w, dir.Path, -1, nil) 54 } 55 56 // join builds a path rooted at the bundle's expanded directory 57 // path and the extra path components provided. 58 func (dir *BundleDir) join(parts ...string) string { 59 parts = append([]string{dir.Path}, parts...) 60 return filepath.Join(parts...) 61 }