diff --git a/cmd/autoscan/config.go b/cmd/autoscan/config.go index bc4c1f4b..c727f99d 100644 --- a/cmd/autoscan/config.go +++ b/cmd/autoscan/config.go @@ -1,28 +1,34 @@ package main import ( - "github.com/kirsle/configdir" - "golang.org/x/sys/unix" + "fmt" "os" "path/filepath" ) -func defaultConfigPath() string { - // get binary path - bp := getBinaryPath() - if dirIsWriteable(bp) == nil { - return bp +func defaultConfigDirectory(app string, filename string) string { + // binary path + bcd := getBinaryPath() + if _, err := os.Stat(filepath.Join(bcd, filename)); err == nil { + // there is a config file in the binary path + // so use this directory as the default + return bcd } - // binary path is not write-able, use alternative path - cp := configdir.LocalConfig("autoscan") - if _, err := os.Stat(cp); os.IsNotExist(err) { - if e := os.MkdirAll(cp, os.ModePerm); e != nil { - panic("failed to create autoscan config directory") + // config dir + ucd, err := os.UserConfigDir() + if err != nil { + panic(fmt.Sprintf("userconfigdir: %v", err)) + } + + acd := filepath.Join(ucd, app) + if _, err := os.Stat(acd); os.IsNotExist(err) { + if err := os.MkdirAll(acd, os.ModePerm); err != nil { + panic(fmt.Sprintf("mkdirall: %v", err)) } } - return cp + return acd } func getBinaryPath() string { @@ -31,14 +37,9 @@ func getBinaryPath() string { if err != nil { // get current working dir if dir, err = os.Getwd(); err != nil { - panic("failed to determine current binary location") + panic(fmt.Sprintf("getwd: %v", err)) } } return dir -} - -func dirIsWriteable(dir string) error { - // credits: https://stackoverflow.com/questions/20026320/how-to-tell-if-folder-exists-and-is-writable - return unix.Access(dir, unix.W_OK) -} +} \ No newline at end of file diff --git a/cmd/autoscan/main.go b/cmd/autoscan/main.go index ab8986f1..416f1549 100644 --- a/cmd/autoscan/main.go +++ b/cmd/autoscan/main.go @@ -112,9 +112,9 @@ func main() { }), kong.Vars{ "version": fmt.Sprintf("%s (%s@%s)", Version, GitCommit, Timestamp), - "config_file": filepath.Join(defaultConfigPath(), "config.yml"), - "log_file": filepath.Join(defaultConfigPath(), "activity.log"), - "database_file": filepath.Join(defaultConfigPath(), "autoscan.db"), + "config_file": filepath.Join(defaultConfigDirectory("autoscan", "config.yml"), "config.yml"), + "log_file": filepath.Join(defaultConfigDirectory("autoscan", "config.yml"), "activity.log"), + "database_file": filepath.Join(defaultConfigDirectory("autoscan", "config.yml"), "autoscan.db"), }, ) diff --git a/go.mod b/go.mod index d2bba713..a728eb93 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/alecthomas/kong v0.2.16 github.com/fsnotify/fsnotify v1.4.9 github.com/justinas/alice v1.2.0 - github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f github.com/l3uddz/bernard v0.5.1 github.com/m-rots/stubbs v1.1.0 github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect @@ -18,7 +17,7 @@ require ( github.com/rs/zerolog v1.20.0 golang.org/x/mod v0.4.2 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c - golang.org/x/sys v0.0.0-20210313110737-8e9fff1a3a18 + golang.org/x/sys v0.0.0-20210313110737-8e9fff1a3a18 // indirect golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba golang.org/x/tools v0.1.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index 8e3cf73b..668c4a60 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,6 @@ github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo= github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= -github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU= -github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0= github.com/l3uddz/bernard v0.5.1 h1:PdkmJn44q4dmix1riBkrvnpb2LVvhyRLwIRhWoc05bo= github.com/l3uddz/bernard v0.5.1/go.mod h1:J2ad7LeQl+6Nxc8sGJ8HtQIsv9c9bk2jHxsBt9OIHpo= github.com/m-rots/stubbs v1.0.0/go.mod h1:iDS6z2oonw2UMo2l0S1WTPJ9git7FWU4YEo6fq7F2WU=