github.com/juju/charm/v11@v11.2.0/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 // BundleDir defines a bundle from a given directory. 15 type BundleDir struct { 16 Path string 17 data *BundleData 18 readMe string 19 20 containsOverlays bool 21 } 22 23 // Trick to ensure *BundleDir implements the Bundle interface. 24 var _ Bundle = (*BundleDir)(nil) 25 26 // ReadBundleDir returns a BundleDir representing an expanded 27 // bundle directory. It does not verify the bundle data. 28 func ReadBundleDir(path string) (dir *BundleDir, err error) { 29 dir = &BundleDir{Path: path} 30 file, err := os.Open(dir.join("bundle.yaml")) 31 if err != nil { 32 return nil, err 33 } 34 dir.data, dir.containsOverlays, err = readBaseFromMultidocBundle(file) 35 file.Close() 36 if err != nil { 37 return nil, err 38 } 39 readMe, err := ioutil.ReadFile(dir.join("README.md")) 40 if err != nil { 41 return nil, fmt.Errorf("cannot read README file: %v", err) 42 } 43 dir.readMe = string(readMe) 44 return dir, nil 45 } 46 47 // Data returns the contents of the bundle's bundle.yaml file. 48 func (dir *BundleDir) Data() *BundleData { 49 return dir.data 50 } 51 52 // ReadMe returns the contents of the bundle's README.md file. 53 func (dir *BundleDir) ReadMe() string { 54 return dir.readMe 55 } 56 57 // ContainsOverlays returns true if the bundle contains any overlays. 58 func (dir *BundleDir) ContainsOverlays() bool { 59 return dir.containsOverlays 60 } 61 62 func (dir *BundleDir) ArchiveTo(w io.Writer) error { 63 return writeArchive(w, dir.Path, -1, "", nil, nil) 64 } 65 66 // join builds a path rooted at the bundle's expanded directory 67 // path and the extra path components provided. 68 func (dir *BundleDir) join(parts ...string) string { 69 parts = append([]string{dir.Path}, parts...) 70 return filepath.Join(parts...) 71 }