/
fetcher.go
49 lines (40 loc) · 1017 Bytes
/
fetcher.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
package app
import (
"context"
"github.com/jlewi/hydros/api/v1alpha1"
"github.com/google/go-github/v52/github"
"github.com/palantir/go-githubapp/appconfig"
"gopkg.in/yaml.v2"
)
// FetchedConfig represents the hydros configuration fetched from GitHub and used to configure hydros
type FetchedConfig struct {
Config *v1alpha1.HydrosConfig
LoadError error
ParseError error
Source string
Path string
}
type ConfigFetcher struct {
Loader *appconfig.Loader
}
func (cf *ConfigFetcher) ConfigForRepositoryBranch(ctx context.Context, client *github.Client, owner, repository, branch string) FetchedConfig {
c, err := cf.Loader.LoadConfig(ctx, client, owner, repository, branch)
fc := FetchedConfig{
Source: c.Source,
Path: c.Path,
}
switch {
case err != nil:
fc.LoadError = err
return fc
case c.IsUndefined():
return fc
}
var pc v1alpha1.HydrosConfig
if err := yaml.UnmarshalStrict(c.Content, &pc); err != nil {
fc.ParseError = err
} else {
fc.Config = &pc
}
return fc
}