This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
manifest.go
106 lines (92 loc) · 2.52 KB
/
manifest.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package manifest
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/KablamoOSS/go-cli-printer"
yaml "github.com/KablamoOSS/yaml"
)
// Once loaded, we keep the manifest in memory
var (
loadedManifest *Manifest
once sync.Once
)
// FindAndLoadManifest - Search the current directory for a manifest file, and load it
func FindAndLoadManifest() *Manifest {
if loadedManifest == nil {
once.Do(func() {
path, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
printer.Fatal(
err,
"If you want to re-initialise your kombustion.yaml file, first remove it.",
"https://www.kombustion.io/api/manifest/",
)
}
manifest, err := findAndLoadManifest(path)
if err != nil {
printer.Fatal(
err,
"If you want to re-initialise your kombustion.yaml file, first remove it.",
"https://www.kombustion.io/api/manifest/",
)
}
loadedManifest = &manifest
})
}
return loadedManifest
}
// CheckManifestExists to determine if there is a manifest in the current dir
func CheckManifestExists() bool {
path, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return false
}
_, err = findAndLoadManifest(path)
if err != nil {
return false
}
return true
}
// findAndLoadManifest - Search the given directory for a manifest file, and load it
// This is seperated to allow for easy testing
func findAndLoadManifest(path string) (Manifest, error) {
var manifestPath string
foundManifest := false
// Support for .yml
if _, err := os.Stat(path + "/kombustion.yml"); err == nil {
manifestPath = path + "/kombustion.yml"
foundManifest = true
}
if _, err := os.Stat(path + "/kombustion.yaml"); err == nil {
if manifestPath == path+"/kombustion.yml" {
return Manifest{}, fmt.Errorf("there are both kombustion.yaml && kombustion.yml files, please remove one")
}
manifestPath = path + "/kombustion.yaml"
foundManifest = true
}
if foundManifest {
// Read the manifest file
data, err := ioutil.ReadFile(manifestPath)
if err != nil {
return Manifest{}, err
}
manifest, err := loadManifestFromString(data)
if err != nil {
return Manifest{}, err
}
return manifest, nil
}
return Manifest{}, fmt.Errorf("kombustion.yaml was not found")
}
// loadManifestFromString - Load a manifest from a string into a Manifest struct
func loadManifestFromString(manifestYaml []byte) (Manifest, error) {
manifest := Manifest{}
err := yaml.Unmarshal([]byte(manifestYaml), &manifest)
if err != nil {
return Manifest{}, err
}
return manifest, nil
}