-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookbooks.go
192 lines (159 loc) · 4.07 KB
/
cookbooks.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package chef
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"sort"
"strings"
"github.com/go-chef/chef"
"golang.org/x/mod/semver"
)
var (
ErrCookbookNotFound = errors.New("cookbook not found")
ErrCookbookVersionNotFound = errors.New("cookbook version not found")
ErrCookbookFileNotFound = errors.New("cookbook file not found")
ErrInternalServerError = errors.New("internal server error")
)
type CookbookVersion struct {
Version string
}
type CookbookListItem struct {
Name string `json:"name"`
Versions []string `json:"versions"`
Dependencies []string `json:"dependencies,omitempty"`
}
type CookbookListResult struct {
Cookbooks []CookbookListItem `json:"cookbooks"`
}
type Cookbook struct {
chef.Cookbook
}
type CookbookMeta struct {
chef.CookbookMeta
}
func (s Service) GetCookbooks(ctx context.Context) (*CookbookListResult, error) {
universe, err := s.client.Universe.Get()
if err != nil {
return nil, err
}
var cookbookList []CookbookListItem
for j, v := range universe.Books {
var versions []string
for q := range v.Versions {
versions = append(versions, q)
}
semver.Sort(versions)
ReverseSlice(versions)
cookbook := CookbookListItem{
Name: j,
Versions: versions,
}
cookbookList = append(cookbookList, cookbook)
}
sort.SliceStable(cookbookList, func(i, j int) bool {
return cookbookList[i].Name < cookbookList[j].Name
})
return &CookbookListResult{Cookbooks: cookbookList}, nil
}
func ReverseSlice[T comparable](s []T) {
sort.SliceStable(s, func(i, j int) bool {
return i > j
})
}
func (s Service) GetLatestCookbooks(ctx context.Context) (*CookbookListResult, error) {
cookbooks, err := s.client.Cookbooks.List()
if err != nil {
return nil, err
}
var cookbookList []CookbookListItem
for j, v := range cookbooks {
var versions []string
for _, k := range v.Versions {
versions = append(versions, k.Version)
}
cookbook := CookbookListItem{
Name: j,
Versions: versions,
}
cookbookList = append(cookbookList, cookbook)
}
return &CookbookListResult{Cookbooks: cookbookList}, nil
}
// GetCookbook should get the latest version of the cookbook
func (s Service) GetCookbook(ctx context.Context, name string) (*Cookbook, error) {
cookbook, err := s.GetCookbookVersion(ctx, name, "_latest")
if err != nil {
return nil, err
}
return cookbook, nil
}
func (s Service) GetCookbookVersion(ctx context.Context, name string, version string) (*Cookbook, error) {
cookbook, err := s.client.Cookbooks.GetVersion(name, version)
if err != nil {
if cerr, ok := err.(*chef.ErrorResponse); ok {
if cerr.StatusCode() == 404 {
return nil, ErrCookbookVersionNotFound
}
}
s.log.Error(err.Error())
return nil, ErrInternalServerError
}
return &Cookbook{cookbook}, nil
}
func (s Cookbook) GetFile(ctx context.Context, client *http.Client, path string) (string, error) {
t := strings.SplitN(path, "/", 2)[0]
var loc []chef.CookbookItem
switch t {
case "attributes":
loc = s.Attributes
case "recipes":
loc = s.Recipes
case "resources":
loc = s.Resources
case "libraries":
loc = s.Libraries
case "providers":
loc = s.Providers
case "templates":
loc = s.Templates
case "files":
loc = s.Files
default:
loc = s.RootFiles
}
for _, f := range loc {
if f.Path == path {
content, err := downloadFile(client, f.Url)
if err != nil {
return "", err
}
return string(content), err
}
}
return "", ErrCookbookFileNotFound
}
func downloadFile(client *http.Client, url string) ([]byte, error) {
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return body, nil
}
func (s Cookbook) GetReadme(ctx context.Context, client *http.Client) (string, error) {
for _, f := range s.RootFiles {
if f.Name == "README.md" {
resp, err := client.Get(f.Url)
if err != nil {
return "", fmt.Errorf("failed to download cookbook readme")
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return string(body), nil
}
}
return "", fmt.Errorf("failed to identify cookbook readme")
}