Add -config option to specify a config file

This commit is contained in:
Nick Craig-Wood 2014-03-15 17:01:13 +00:00
parent b3f1a45bbf
commit 7d786204b4

View File

@ -25,18 +25,19 @@ const (
var ( var (
// Config file // Config file
ConfigFile *goconfig.ConfigFile ConfigFile *goconfig.ConfigFile
// Home directory
HomeDir = configHome()
// Config file path // Config file path
ConfigPath string ConfigPath = path.Join(HomeDir, configFileName)
// Global config // Global config
Config = &ConfigInfo{} Config = &ConfigInfo{}
// Home directory
HomeDir string
// Flags // Flags
verbose = flag.Bool("verbose", false, "Print lots more stuff") verbose = flag.Bool("verbose", false, "Print lots more stuff")
quiet = flag.Bool("quiet", false, "Print as little stuff as possible") quiet = flag.Bool("quiet", false, "Print as little stuff as possible")
modifyWindow = flag.Duration("modify-window", time.Nanosecond, "Max time diff to be considered the same") modifyWindow = flag.Duration("modify-window", time.Nanosecond, "Max time diff to be considered the same")
checkers = flag.Int("checkers", 8, "Number of checkers to run in parallel.") checkers = flag.Int("checkers", 8, "Number of checkers to run in parallel.")
transfers = flag.Int("transfers", 4, "Number of file transfers to run in parallel.") transfers = flag.Int("transfers", 4, "Number of file transfers to run in parallel.")
configFile = flag.String("config", ConfigPath, "Config file.")
) )
// Filesystem config options // Filesystem config options
@ -48,6 +49,17 @@ type ConfigInfo struct {
Transfers int Transfers int
} }
// Find the config directory
func configHome() string {
// Find users home directory
usr, err := user.Current()
if err != nil {
log.Printf("Couldn't find home directory: %v", err)
return ""
}
return usr.HomeDir
}
// Loads the config file // Loads the config file
func LoadConfig() { func LoadConfig() {
// Read some flags if set // Read some flags if set
@ -59,16 +71,10 @@ func LoadConfig() {
Config.Checkers = *checkers Config.Checkers = *checkers
Config.Transfers = *transfers Config.Transfers = *transfers
// Find users home directory ConfigPath = *configFile
usr, err := user.Current()
if err != nil {
log.Printf("Couldn't find home directory: %v", err)
return
}
HomeDir = usr.HomeDir
ConfigPath = path.Join(HomeDir, configFileName)
// Load configuration file. // Load configuration file.
var err error
ConfigFile, err = goconfig.LoadConfigFile(ConfigPath) ConfigFile, err = goconfig.LoadConfigFile(ConfigPath)
if err != nil { if err != nil {
log.Printf("Failed to load config file %v - using defaults", ConfigPath) log.Printf("Failed to load config file %v - using defaults", ConfigPath)