-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleek.go
315 lines (272 loc) · 8.53 KB
/
fleek.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package fleek
import (
"context"
"net/http"
"time"
"github.com/hasura/go-graphql-client"
"golang.org/x/oauth2"
)
type FleekClient struct {
client *graphql.Client
httpClient *http.Client
staticToken oauth2.TokenSource
token string
}
type Team struct {
Id string `json:"id"`
Name string `json:"name"`
}
type EnvironmentVariable struct {
Name string `json:"name"`
Value string `json:"value"`
}
type BuildSettings struct {
BuildCommand string `json:"buildCommand"`
BaseDirectoryPath string `json:"baseDirectoryPath"`
PublishDirectoryPath string `json:"publishDirectoryPath"`
DockerImage string `json:"dockerImage"`
EnvironmentVariables []EnvironmentVariable `json:"environmentVariables"`
}
type Source struct {
// IPFS Source
CID string `json:"cid,omitempty"`
// Repository
Type string `json:"type,omitempty"`
URL string `json:"url,omitempty"`
Branch string `json:"branch,omitempty"`
}
type DeploySettings struct {
AutoPublishing bool `json:"autoPublishing"`
PRDeployPreviews bool `json:"prDeployPreviews"`
DfinityUseProxy bool `json:"dfinityUseProxy"`
Source Source `json:"source"`
}
type Repository struct {
Commit string `json:"commit"`
Branch string `json:"branch"`
Owner string `json:"owner"`
Name string `json:"name"`
Message string `json:"message"`
}
type PublishedDeploy struct {
Id interface{} `json:"id"`
Status string `json:"status"`
IPFSHash string `json:"ipfsHash"`
Log string `json:"log"`
PreviewImage string `json:"previewImage"`
AutoPublish bool `json:"autoPublish"`
Published bool `json:"published"`
Repository Repository `json:"repository"`
TotalTime int `json:"totalTime"`
StartedAt time.Time `json:"startedAt"`
CompletedAt time.Time `json:"completedAt"`
}
type Site struct {
Id interface{} `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
Platform string `json:"platform"`
Team Team `json:"team"`
BuildSettings BuildSettings `json:"buildSettings"`
DeploySettings DeploySettings `json:"deploySettings"`
PublishedDeploy PublishedDeploy `json:"publishedDeploy"`
CreatedBy interface{} `json:"createdBy"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type GraphqlSite struct {
Id graphql.ID
Name graphql.String
Slug graphql.String
Description graphql.String
Platform graphql.String
Team struct {
Id graphql.ID
Name graphql.String
}
BuildSettings struct {
BuildCommand graphql.String
BaseDirectoryPath graphql.String
PublishDirectoryPath graphql.String
DockerImage graphql.String
EnvironmentVariables []struct {
Name graphql.String
Value graphql.String
}
}
DeploySettings struct {
AutoPublishing graphql.Boolean
PRDeployPreviews graphql.Boolean
DfinityUseProxy graphql.Boolean
Source struct {
IPFSSource struct {
CID graphql.String
} `graphql:"... on IpfsSource"`
Repository struct {
Type graphql.String
URL graphql.String
Branch graphql.String
} `graphql:"... on Repository"`
}
}
PublishedDeploy struct {
Id graphql.ID
Status graphql.String
IpfsHash graphql.String
PreviewImage graphql.String
AutoPublish graphql.Boolean
Published graphql.Boolean
Log graphql.String
Repository struct {
Commit graphql.String
Branch graphql.String
Owner graphql.String
Name graphql.String
Message graphql.String
}
TotalTime graphql.Int
StartedAt graphql.String
CompletedAt graphql.String
}
CreatedBy graphql.ID
CreatedAt graphql.String
UpdatedAt graphql.String
}
func (f *FleekClient) convertGraphqlSiteToSite(gqlSite GraphqlSite) (Site, error) {
// BuildSettings
buildSettings := BuildSettings{
BuildCommand: string(gqlSite.BuildSettings.BuildCommand),
BaseDirectoryPath: string(gqlSite.BuildSettings.BaseDirectoryPath),
PublishDirectoryPath: string(gqlSite.BuildSettings.BaseDirectoryPath),
DockerImage: string(gqlSite.BuildSettings.DockerImage),
}
for _, gqlEnvVar := range gqlSite.BuildSettings.EnvironmentVariables {
buildSettings.EnvironmentVariables = append(
buildSettings.EnvironmentVariables,
EnvironmentVariable{
Name: string(gqlEnvVar.Name),
Value: string(gqlEnvVar.Value),
},
)
}
// DeploySettings
deploySettings := DeploySettings{
AutoPublishing: bool(gqlSite.DeploySettings.AutoPublishing),
PRDeployPreviews: bool(gqlSite.DeploySettings.PRDeployPreviews),
DfinityUseProxy: bool(gqlSite.DeploySettings.DfinityUseProxy),
Source: Source{},
}
if gqlSite.DeploySettings.Source.IPFSSource.CID != "" {
deploySettings.Source.CID =
string(gqlSite.DeploySettings.Source.IPFSSource.CID)
} else if gqlSite.DeploySettings.Source.Repository.URL != "" {
deploySettings.Source.Type =
string(gqlSite.DeploySettings.Source.Repository.Type)
deploySettings.Source.URL =
string(gqlSite.DeploySettings.Source.Repository.URL)
deploySettings.Source.Branch =
string(gqlSite.DeploySettings.Source.Repository.Branch)
}
// Site
startedAt, _ := time.Parse(time.RFC3339, string(gqlSite.PublishedDeploy.StartedAt))
completedAt, _ := time.Parse(time.RFC3339, string(gqlSite.PublishedDeploy.CompletedAt))
createdAt, _ := time.Parse(time.RFC3339, string(gqlSite.CreatedAt))
updatedAt, _ := time.Parse(time.RFC3339, string(gqlSite.UpdatedAt))
site := Site{
Id: gqlSite.Id,
Name: string(gqlSite.Name),
Slug: string(gqlSite.Slug),
Description: string(gqlSite.Description),
Platform: string(gqlSite.Platform),
Team: Team{
Id: string(gqlSite.Team.Id),
Name: string(gqlSite.Name),
},
BuildSettings: buildSettings,
DeploySettings: deploySettings,
PublishedDeploy: PublishedDeploy{
Id: gqlSite.PublishedDeploy.Id,
Status: string(gqlSite.PublishedDeploy.Status),
IPFSHash: string(gqlSite.PublishedDeploy.IpfsHash),
PreviewImage: string(gqlSite.PublishedDeploy.PreviewImage),
AutoPublish: bool(gqlSite.PublishedDeploy.AutoPublish),
Published: bool(gqlSite.PublishedDeploy.Published),
Log: string(gqlSite.PublishedDeploy.Log),
Repository: Repository{
Commit: string(gqlSite.PublishedDeploy.Repository.Commit),
Branch: string(gqlSite.PublishedDeploy.Repository.Branch),
Owner: string(gqlSite.PublishedDeploy.Repository.Owner),
Name: string(gqlSite.PublishedDeploy.Repository.Name),
Message: string(gqlSite.PublishedDeploy.Repository.Message),
},
TotalTime: int(gqlSite.PublishedDeploy.TotalTime),
StartedAt: startedAt,
CompletedAt: completedAt,
},
CreatedBy: gqlSite.CreatedBy,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
return site, nil
}
// Initializes new FleekClient
func New(token string) (*FleekClient, error) {
fleekClient := new(FleekClient)
fleekClient.token = token
fleekClient.staticToken = oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: fleekClient.token,
TokenType: " ",
})
fleekClient.httpClient = oauth2.NewClient(
context.Background(),
fleekClient.staticToken,
)
fleekClient.client = graphql.NewClient(
"https://api.fleek.co/graphql",
fleekClient.httpClient,
)
return fleekClient, nil
}
// Gets all sites of a team, with the team ID being the *slug* (e.g. `my-team`)
func (f *FleekClient) GetSitesByTeamId(teamId string) ([]Site, error) {
var query struct {
GetSitesByTeam struct {
Sites []GraphqlSite
NextToken graphql.String
} `graphql:"getSitesByTeam(teamId: $teamId, limit: 100)"`
}
vars := map[string]interface{}{
"teamId": graphql.ID(teamId),
}
err := f.client.Query(context.Background(), &query, vars)
if err != nil {
return []Site{}, err
}
var sites []Site
for _, querySite := range query.GetSitesByTeam.Sites {
site, err := f.convertGraphqlSiteToSite(querySite)
if err != nil {
return sites, err
}
sites = append(sites, site)
}
return sites, nil
}
// Gets a single site by its slug
func (f *FleekClient) GetSiteBySlug(slug string) (Site, error) {
var query struct {
GetSiteBySlug struct {
GraphqlSite
} `graphql:"getSiteBySlug(slug: $slug)"`
}
vars := map[string]interface{}{
"slug": graphql.String(slug),
}
err := f.client.Query(context.Background(), &query, vars)
if err != nil {
return Site{}, err
}
site, err := f.convertGraphqlSiteToSite(query.GetSiteBySlug.GraphqlSite)
return site, err
}