-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathout.go
113 lines (101 loc) · 2.47 KB
/
out.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
package concourse_tfe_resource
import (
"context"
"encoding/json"
"fmt"
tfe "github.com/hashicorp/go-tfe"
"os"
"path"
)
func out(input inputJSON) ([]byte, error) {
if err := pushVars(input); err != nil {
return nil, err
}
rco := tfe.RunCreateOptions{
Workspace: workspace,
Message: &input.Params.Message,
}
run, err := client.Runs.Create(context.Background(), rco)
if err != nil {
return nil, formatError(err, "creating run")
}
result := outOutputJSON{
Version: version{Ref: run.ID},
Metadata: runMetadata(input, run),
}
return json.Marshal(result)
}
func pushVars(input inputJSON) error {
list, err := getVariableList()
if err != nil {
return err
}
for k, v := range input.Params.Vars {
if err := pushVar(list, k, v); err != nil {
return err
}
}
return nil
}
func pushVar(list tfe.VariableList, name string, v variableJSON) error {
var variable *tfe.Variable
// see if the variable exists
for _, k := range list.Items {
if name == k.Key {
variable = k
break
}
}
value, err := getValue(v, name)
if err != nil {
return err
}
if variable != nil {
update := tfe.VariableUpdateOptions{
Key: &name,
Value: &value,
HCL: &v.Hcl,
Sensitive: &v.Sensitive,
Description: &v.Description,
}
_, err := client.Variables.Update(context.Background(), workspace.ID, variable.ID, update)
if err != nil {
return formatError(err, "updating variable \""+name+"\"")
}
} else {
create := tfe.VariableCreateOptions{
Key: &name,
Value: &value,
HCL: &v.Hcl,
Sensitive: &v.Sensitive,
Description: &v.Description,
Category: &v.Category,
}
_, err := client.Variables.Create(context.Background(), workspace.ID, create)
if err != nil {
return formatError(err, "creating variable \""+name+"\"")
}
}
return nil
}
func getValue(v variableJSON, name string) (string, error) {
var value string
if v.Value != "" {
value = v.Value
} else if v.File != "" {
fileName := path.Join(workingDirectory, v.File)
f, err := os.Open(fileName)
if err != nil {
return "", formatError(err, "getting value for variable \""+name+"\"")
}
s, _ := f.Stat()
byteVal := make([]byte, s.Size())
if _, err = f.Read(byteVal); err != nil {
return "", formatError(err, "reading value for variable \""+name+"\"")
}
value = string(byteVal)
} else {
return "", fmt.Errorf("error finding value for variable \"%s\": no value or filename provided", name)
}
return value, nil
}