-
Notifications
You must be signed in to change notification settings - Fork 52
/
project_config.go
89 lines (74 loc) · 1.97 KB
/
project_config.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
package projectconfig
import (
"io/ioutil"
"log"
"github.com/hashicorp/terraform/dag"
"github.com/k0kubun/pp"
yaml "gopkg.in/yaml.v2"
)
// GraphRootName represents the root of the graph of modules in a project
const GraphRootName = "graphRoot"
type ZeroProjectConfig struct {
Name string `yaml:"name"`
ShouldPushRepositories bool `yaml:"shouldPushRepositories"`
Parameters map[string]string
Modules Modules `yaml:"modules"`
}
type Modules map[string]Module
type Module struct {
DependsOn []string `yaml:"dependsOn,omitempty"`
Parameters Parameters `yaml:"parameters,omitempty"`
Files Files
}
type Parameters map[string]string
type Files struct {
Directory string `yaml:"dir,omitempty"`
Repository string `yaml:"repo,omitempty"`
Source string
}
func LoadConfig(filePath string) *ZeroProjectConfig {
config := &ZeroProjectConfig{}
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Panicf("failed to read config: %v", err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Panicf("failed to parse config: %v", err)
}
return config
}
func (c *ZeroProjectConfig) Print() {
pp.Println(c)
}
// GetDAG returns a graph of the module names used in this project config
func (c *ZeroProjectConfig) GetDAG() dag.AcyclicGraph {
var g dag.AcyclicGraph
// Add vertices to graph
g.Add(GraphRootName)
for name := range c.Modules {
g.Add(name)
}
// Connect modules in graph
for name, m := range c.Modules {
if len(m.DependsOn) == 0 {
g.Connect(dag.BasicEdge(GraphRootName, name))
} else {
for _, dependencyName := range m.DependsOn {
g.Connect(dag.BasicEdge(dependencyName, name))
}
}
}
return g
}
func NewModule(parameters Parameters, directory string, repository string, source string, dependsOn []string) Module {
return Module{
Parameters: parameters,
DependsOn: dependsOn,
Files: Files{
Directory: directory,
Repository: repository,
Source: source,
},
}
}