/
provider.go
60 lines (51 loc) · 1.42 KB
/
provider.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
package canaryConfig
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/ghodss/yaml"
"github.com/armory-io/kayentactl/pkg/kayenta"
)
// GetCanaryConfig fetches a canary config from a remote or local source
// and converts it to a StandaloneCanaryAnalysisInput. It supports both YAML
// and JSON data formats
func GetCanaryConfig(location string) (*kayenta.CanaryConfig, error) {
var configProvider func(string) ([]byte, error)
if startsWithProtocol(location, []string{"file://", "http://", "https://"}) {
configProvider = httpConfigProvider
} else {
configProvider = ioutil.ReadFile
}
b, err := configProvider(location)
if err != nil {
return nil, err
}
var input kayenta.CanaryConfig
if err := parseYamlOrJson(b, &input); err != nil {
return nil, fmt.Errorf("failed to deserialize canary config: %w", err)
}
return &input, nil
}
func httpConfigProvider(location string) ([]byte, error) {
resp, err := http.Get(location)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("attempt to fetch config results in code %d", resp.StatusCode)
}
return ioutil.ReadAll(resp.Body)
}
func startsWithProtocol(location string, protocols []string) bool {
for _, p := range protocols {
if strings.HasPrefix(location, p) {
return true
}
}
return false
}
func parseYamlOrJson(b []byte, dest interface{}) error {
return yaml.Unmarshal(b, dest)
}