-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment.go
95 lines (76 loc) · 1.95 KB
/
environment.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
package main
import (
"fmt"
"io/ioutil"
"log"
"path"
"github.com/yannh/r10k-go/git"
"github.com/yannh/r10k-go/puppetsource"
)
type environment struct {
source puppetsource.Source
branch string
modulesFolder string
}
func newEnvironment(s puppetsource.Source, branch string) environment {
return environment{
s, branch, "modules",
}
}
func getEnvironments(envNames []string, sources []puppetsource.Source) []environment {
envs := make([]environment, 0)
for _, envName := range envNames {
// Find in which source the environment is
// TODO: make deterministic
found := false
for _, source := range sources {
if git.RepoHasRemoteBranch(source.Remote(), envName) {
envs = append(envs, newEnvironment(source, envName))
found = true
break
}
}
if found == false {
log.Printf("failed to find source for environment %s", envName)
}
}
fmt.Printf("%+v\n", envs)
return envs
}
func (e *environment) installedModules() []string {
folder := path.Join(e.source.Basedir(), e.branch, e.modulesFolder)
files, err := ioutil.ReadDir(folder)
if err != nil {
log.Fatal(err)
}
modules := make([]string, 5)
for _, f := range files {
modules = append(modules, f.Name())
}
return modules
}
func (env *environment) fetch(cache *cache) error {
s := env.source.(*puppetsource.GitSource) // FIXME should use interface instead of checkout/clone
if err := s.Fetch(cache.folder); err != nil {
return err
}
if err := git.Checkout(s.Location(), git.NewRef(git.TypeBranch, env.branch)); err != nil {
return err
}
if err := git.Clone(s.Location(), path.Join(s.Basedir(), env.branch)); err != nil {
return err
}
return nil
}
func DeployedEnvironments(s puppetsource.Source) []environment {
folder := path.Join(s.Basedir())
files, err := ioutil.ReadDir(folder)
if err != nil {
log.Fatal(err)
}
envs := make([]environment, 0)
for _, f := range files {
envs = append(envs, newEnvironment(s, f.Name()))
}
return envs
}