forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundledir.go
61 lines (52 loc) · 1.35 KB
/
bundledir.go
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
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
type BundleDir struct {
Path string
data *BundleData
readMe string
}
// Trick to ensure *BundleDir implements the Bundle interface.
var _ Bundle = (*BundleDir)(nil)
// ReadBundleDir returns a BundleDir representing an expanded
// bundle directory. It does not verify the bundle data.
func ReadBundleDir(path string) (dir *BundleDir, err error) {
dir = &BundleDir{Path: path}
file, err := os.Open(dir.join("bundle.yaml"))
if err != nil {
return nil, err
}
dir.data, err = ReadBundleData(file)
file.Close()
if err != nil {
return nil, err
}
readMe, err := ioutil.ReadFile(dir.join("README.md"))
if err != nil {
return nil, fmt.Errorf("cannot read README file: %v", err)
}
dir.readMe = string(readMe)
return dir, nil
}
func (dir *BundleDir) Data() *BundleData {
return dir.data
}
func (dir *BundleDir) ReadMe() string {
return dir.readMe
}
func (dir *BundleDir) ArchiveTo(w io.Writer) error {
return writeArchive(w, dir.Path, -1, nil)
}
// join builds a path rooted at the bundle's expanded directory
// path and the extra path components provided.
func (dir *BundleDir) join(parts ...string) string {
parts = append([]string{dir.Path}, parts...)
return filepath.Join(parts...)
}