-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvention.go
51 lines (42 loc) · 1.27 KB
/
convention.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
package kanvas
import (
"fmt"
"os"
)
const (
defaultConfigFileBase = "kanvas"
DefaultConfigFileYAML = defaultConfigFileBase + ".yaml"
DefaultConfigFileJsonnet = defaultConfigFileBase + ".jsonnet"
DefaultConfigFileTemplateJsonnet = defaultConfigFileBase + ".template.jsonnet"
)
// DiscoverConfigFile returns the path to the config file to use.
//
// If the config file is not specified, it will try to find the follwoing files in the current directory,
// and return the first one found:
//
// - kanvas.yaml
// - kanvas.jsonnet
// - kanvas.template.jsonnet
//
// If the config file is not specified and no config file is found, it will return an error.
func DiscoverConfigFile(opts Options) (string, error) {
if opts.ConfigFile != "" {
return opts.ConfigFile, nil
}
var (
found []string
)
if _, err := os.Stat(DefaultConfigFileYAML); err == nil {
found = append(found, DefaultConfigFileYAML)
}
if _, err := os.Stat(DefaultConfigFileJsonnet); err == nil {
found = append(found, DefaultConfigFileJsonnet)
}
if _, err := os.Stat(DefaultConfigFileTemplateJsonnet); err == nil {
found = append(found, DefaultConfigFileTemplateJsonnet)
}
if len(found) == 0 {
return "", fmt.Errorf("unable to find config file")
}
return found[0], nil
}