-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.go
50 lines (42 loc) · 1.05 KB
/
resources.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
package puck
import (
"bytes"
"encoding/json"
"fmt"
)
type Resources struct {
Resources []Resource
}
type Resource struct {
Type string
NameAttribute string `json:"name_attribute"`
Attributes map[string]interface{}
}
func ParseResourcesJSON(j []byte) *Resources {
var resources Resources
json.Unmarshal(j, &resources)
return &resources
}
func CompileChefResources(resources []Resource) string {
var buffer bytes.Buffer
for _, v := range resources {
buffer.WriteString(fmt.Sprint(v.ToChefResource()))
}
return buffer.String()
}
func (r *Resource) ToChefResource() string {
var buffer bytes.Buffer
quotedNameAttribute := fmt.Sprintf("\"%v\"", r.NameAttribute)
buffer.WriteString(fmt.Sprintln(r.Type, quotedNameAttribute, "do"))
for k, v := range r.Attributes {
switch v := v.(type) {
case string:
quotedString := fmt.Sprintf("\"%v\"", v)
buffer.WriteString(fmt.Sprintln(" ", k, quotedString))
default:
buffer.WriteString(fmt.Sprintln(" ", k, v))
}
}
buffer.WriteString(fmt.Sprintln("end"))
return buffer.String()
}