2018-08-15 22:23:34 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
"gopkg.in/urfave/cli.v2/altsrc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-03 20:05:30 +00:00
|
|
|
// File names from which we attempt to read configuration.
|
|
|
|
DefaultConfigFiles = []string{"config.yml", "config.yaml"}
|
2018-08-15 22:23:34 +00:00
|
|
|
|
|
|
|
// Launchd doesn't set root env variables, so there is default
|
|
|
|
// Windows default config dir was ~/cloudflare-warp in documentation; let's keep it compatible
|
|
|
|
DefaultConfigDirs = []string{"~/.cloudflared", "~/.cloudflare-warp", "~/cloudflare-warp", "/usr/local/etc/cloudflared", "/etc/cloudflared"}
|
|
|
|
)
|
|
|
|
|
2018-10-03 20:05:30 +00:00
|
|
|
const DefaultCredentialFile = "cert.pem"
|
|
|
|
|
2018-08-15 22:23:34 +00:00
|
|
|
// FileExists checks to see if a file exist at the provided path.
|
|
|
|
func FileExists(path string) (bool, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// ignore missing files
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindInputSourceContext pulls the input source from the config flag.
|
|
|
|
func FindInputSourceContext(context *cli.Context) (altsrc.InputSourceContext, error) {
|
|
|
|
if context.String("config") != "" {
|
|
|
|
return altsrc.NewYamlSourceFromFile(context.String("config"))
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindDefaultConfigPath returns the first path that contains a config file.
|
2018-10-03 20:05:30 +00:00
|
|
|
// If none of the combination of DefaultConfigDirs and DefaultConfigFiles
|
|
|
|
// contains a config file, return empty string.
|
2018-08-15 22:23:34 +00:00
|
|
|
func FindDefaultConfigPath() string {
|
|
|
|
for _, configDir := range DefaultConfigDirs {
|
2018-10-03 20:05:30 +00:00
|
|
|
for _, configFile := range DefaultConfigFiles {
|
2018-08-15 22:23:34 +00:00
|
|
|
dirPath, err := homedir.Expand(configDir)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path := filepath.Join(dirPath, configFile)
|
|
|
|
if ok, _ := FileExists(path); ok {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|