This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manifest.go
92 lines (81 loc) · 1.72 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
type RunDefinition struct {
Processes map[string]string
CronJobs []string `json:"cron_jobs"`
}
type Manifest struct {
ServerType string `json:omit`
Platform string `json:"platform"`
BuildCommand string `json:"build"`
Owner []string
Types map[string]RunDefinition
Repos []Repo
}
type Repo struct {
Url string
Folder string
ShaRaw string `json:"sha"`
}
func (r *Repo) Sha() string {
if r.ShaRaw == "" {
return "origin/master"
}
return r.ShaRaw
}
func firstNonEmpty(possibilities []string) string {
for _, v := range possibilities {
if v != "" {
return v
}
}
return ""
}
func (m *Manifest) Processes() map[string]string {
return m.Types[m.ServerType].Processes
}
func (m *Manifest) CronJobs() []string {
return m.Types[m.ServerType].CronJobs
}
func (m *Manifest) Get(key string) string {
switch {
case key == "owner":
return strings.Join(m.Owner, "\n")
case key == "processes":
processes := []string{}
for k, v := range m.Processes() {
processes = append(processes, fmt.Sprintf("%s %s", k, v))
}
return strings.Join(processes, "\n")
case key == "cron_jobs":
return strings.Join(m.CronJobs(), "\n")
case key == "repos":
repos := []string{}
for _, r := range m.Repos {
repos = append(repos, fmt.Sprintf("%s %s %s", r.Folder, r.Url, r.Sha()))
}
return strings.Join(repos, "\n")
}
return ""
}
func NewManifest(path string, serverType string) Manifest {
m := Manifest{}
m.ServerType = serverType
content, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = json.Unmarshal(content, &m)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return m
}