forked from mhanygin/go-gocd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
151 lines (133 loc) · 3.9 KB
/
env.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gocd
import (
"fmt"
"strings"
)
type Link struct {
HRef string `json:"href"`
}
type Links struct {
Self Link `json:"self"`
Doc Link `json:"doc"`
Find Link `json:"find"`
}
type ShortAgent struct {
Links Links `json:"_links"`
Uuid string `json:"uuid"`
}
type ShortPipeline struct {
Links Links `json:"_links"`
Name string `json:"name"`
}
type EnvironmentVariable struct {
Secure bool
Crypt bool
Name string
Value string
}
type Environment struct {
Links Links `json:"_links"`
Name string `json:"name"`
Agents []ShortAgent `json:"agents"`
EnvironmentVariables []map[string]interface{} `json:"environment_variables"`
Pipelines []ShortPipeline `json:"pipelines"`
}
func NewEnvironment() *Environment {
return &Environment{Agents: make([]ShortAgent, 0),
Pipelines: make([]ShortPipeline, 0),
EnvironmentVariables: make([]map[string]interface{}, 0)}
}
func (p *Environment) AddPipeline(pipeline string) error {
for _, p := range p.Pipelines {
if strings.Compare(p.Name, pipeline) == 0 {
return fmt.Errorf("Pipeline %s exist", pipeline)
}
}
p.Pipelines = append(p.Pipelines, ShortPipeline{Name: pipeline})
return nil
}
func (p *Environment) ExistPipeline(pipeline string) bool {
for _, p := range p.Pipelines {
if strings.Compare(p.Name, pipeline) == 0 {
return true
}
}
return false
}
func (p *Environment) DeletePipeline(pipeline string) error {
for i := 0; i < len(p.Pipelines); i++ {
if strings.Compare(p.Pipelines[i].Name, pipeline) == 0 {
p.Pipelines[i] = p.Pipelines[len(p.Pipelines)-1]
p.Pipelines = p.Pipelines[:len(p.Pipelines)-1]
return nil
}
}
return fmt.Errorf("Pipeline %s not exist", pipeline)
}
func (p *Environment) AddEnvironmentVariables(env *EnvironmentVariable) error {
valueField := "value"
if env.Secure && !env.Crypt {
valueField = "encrypted_value"
}
for _, v := range p.EnvironmentVariables {
if name, _ := v["name"]; strings.Compare(name.(string), env.Name) == 0 {
return fmt.Errorf("Env %s exist", env.Name)
}
}
p.EnvironmentVariables = append(p.EnvironmentVariables, map[string]interface{}{
"name": env.Name,
"secure": env.Secure,
valueField: env.Value})
return nil
}
func (p *Environment) GetEnvironmentVariables(name string) (*EnvironmentVariable, error) {
for _, v := range p.EnvironmentVariables {
if n, _ := v["name"]; strings.Compare(n.(string), name) == 0 {
env := EnvironmentVariable{Name: name, Secure: v["secure"].(bool)}
if val, ok := v["encrypted_value"]; ok {
env.Value = val.(string)
env.Crypt = false
} else {
env.Value = v["value"].(string)
env.Crypt = env.Secure
}
return &env, nil
}
}
return nil, fmt.Errorf("Env %s not exist", name)
}
func (p *Environment) DeleteEnvironmentVariables(name string) error {
for i, v := range p.EnvironmentVariables {
if n, _ := v["name"]; strings.Compare(n.(string), name) == 0 {
if i != (len(p.EnvironmentVariables) - 1) {
p.EnvironmentVariables[i] = p.EnvironmentVariables[len(p.EnvironmentVariables)-1]
}
p.EnvironmentVariables = p.EnvironmentVariables[:(len(p.EnvironmentVariables) - 1)]
return nil
}
}
return fmt.Errorf("Env %s not exist", name)
}
type Environments struct {
Links Links `json:"_links"`
Embeded struct {
Environments []Environment `json:"environments"`
} `json:"_embedded"`
}
func NewEnvironments() *Environments {
return &Environments{Embeded: struct {
Environments []Environment `json:"environments"`
}{Environments: make([]Environment, 0)}}
}
type Version struct {
ClientVersion string
Links struct {
Self Link `json:"self"`
Doc Link `json:"doc"`
} `json:"_links"`
Version string `json:"version"`
BuildNumber string `json:"build_number"`
GitSha string `json:"git_sha"`
FullVersion string `json:"full_version"`
CommitUrl string `json:"commit_url"`
}