-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
52 lines (44 loc) · 1.04 KB
/
utils.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
package main
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
func getBytesForFileOrURL(path string) ([]byte, error) {
u, err := url.ParseRequestURI(path)
if err == nil && u.Scheme != "" {
return getBytesFromURL(path)
}
ext := filepath.Ext(path)
if strings.TrimSpace(ext) == "" || ext != "yaml" {
return nil, errors.New("unsupported file type, supports only yaml as of now")
}
if _, err := os.Stat(path); err != nil {
return nil, err
}
return ioutil.ReadFile(path)
}
func getBytesFromURL(uri string) ([]byte, error) {
resp, err := http.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func decodeConfig(in []byte) (*AngaGoConf, error) {
var config AngaGoConf
if err := yaml.Unmarshal(in, &config); err != nil {
return nil, err
}
// TODO: validate other basic configs
if _, ok := validResurcesMap[strings.ToLower(config.Kind)]; !ok {
return nil, errors.New("invalid resource type")
}
return &config, nil
}