/
manifest.go
57 lines (48 loc) · 1.34 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
package manifest
import (
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/go-git/go-git/v5"
)
type Manifest struct {
Name string `toml:"name,omitempty"`
BootstrapPeers []string `toml:"bootstrap_peers,omitempty"`
ClusterKey string `toml:"cluster_key,omitempty"`
Replications int64 `toml:"replications,omitempty"`
StatsNode string `toml:"stats_node,omitempty"`
url string `toml:"url"`
path string `toml:"path"`
r *git.Repository
}
// Init Clones/Pulls a Manifest Repository and Parses the Config
func Init(path, url string) (*Manifest, error) {
var err error
// Initialize Manifest Struct
result := Manifest{
path: path,
url: url,
}
// Check if Git Repository Exists
result.r, err = git.PlainOpen(path)
if err != nil && err.Error() == "repository does not exist" {
result.r, err = git.PlainClone(path, false, &git.CloneOptions{
URL: url,
})
}
if err != nil {
return nil, err
}
// Pull in new changes from the manifest
err = result.Pull()
if err != nil {
return nil, err
}
// Decode local manifest config from repository
err = result.Decode()
return &result, err
}
// Decode Manifest Configuration from Manifest Repository
func (m *Manifest) Decode() error {
_, err := toml.DecodeFile(filepath.Join(m.path, "config.toml"), m)
return err
}