-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.go
69 lines (59 loc) · 1.34 KB
/
config.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
package traefikkop
import (
"io"
"os"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/traefik/traefik/v2/pkg/provider/docker"
"gopkg.in/yaml.v3"
)
type Config struct {
DockerConfig string
DockerHost string
Hostname string
BindIP string
Addr string
Pass string
DB int
PollInterval int64
Namespace string
}
type ConfigFile struct {
Docker docker.Provider `yaml:"docker"`
}
func loadDockerConfig(input string) (*docker.Provider, error) {
if input == "" {
return nil, nil
}
var r io.Reader
if looksLikeFile(input) {
// see if given filename
_, err := os.Stat(input)
if err == nil {
logrus.Debugf("loading docker config from file %s", input)
r, err = os.Open(input)
if err != nil {
return nil, errors.Wrapf(err, "failed to open docker config %s", input)
}
}
} else {
logrus.Debugf("loading docker config from yaml input")
r = strings.NewReader(input) // treat as direct yaml input
}
// parse
conf := ConfigFile{Docker: docker.Provider{}}
err := yaml.NewDecoder(r).Decode(&conf)
if err != nil {
return nil, errors.Wrap(err, "failed to load config")
}
return &conf.Docker, nil
}
func looksLikeFile(input string) bool {
if strings.Contains(input, "\n") {
return false
}
ok, _ := regexp.MatchString(`\.ya?ml`, input)
return ok
}