This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
86 lines (79 loc) · 1.77 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"io/ioutil"
"net/url"
"os"
"os/user"
"path/filepath"
yaml "gopkg.in/yaml.v2"
)
// configuration for faces app
type config struct {
CameraID int `yaml:"cameraId"`
SubscriptionKey string `yaml:"subscriptionKey"`
URIBase string `yaml:"uriBase"`
URIPath string `yaml:"uriPath"`
URIParams string `yaml:"uriParams"`
uri string
CapturesPerMinute int `yaml:"capturesPerMinute"`
FrameStrenght int `yaml:"frameStrenght"`
SaveImagePath string `yaml:"saveImagePath"`
SaveImageMax int `yaml:"saveImageMax"`
Debug bool `yaml:"debug"`
}
func newConfig(file string) (*config, error) {
// read configuration
c := config{}
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(data, &c); err != nil {
return nil, err
}
u, err := url.Parse(c.URIBase)
if err != nil {
return nil, err
}
u.Path = c.URIPath
u.RawQuery = c.URIParams
c.uri = u.String()
return &c, nil
}
func (c *config) URI() string {
return c.uri
}
// findConfig in different locations, first match will be returned
func findConfig(args []string) string {
var configFileNames = []string{
"faces.yaml",
"faces.yml",
".faces.yaml",
".faces.yml",
}
dirs := []string{}
// check args
if len(args) > 1 && args[1] != "" {
return args[1]
}
// get user home
u, _ := user.Current()
if u != nil {
dirs = append(dirs, u.HomeDir)
}
// get executable path
e, _ := os.Executable()
if e != "" {
ep := filepath.Dir(e)
dirs = append(dirs, ep, filepath.Join(ep, "..", "Resources"))
}
for _, d := range dirs {
for _, c := range configFileNames {
n := filepath.Join(d, c)
if _, err := os.Stat(n); err == nil {
return n
}
}
}
return ""
}