Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

refactor: default config directory #109

Merged
merged 2 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions cmd/autoscan/config.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}
}
6 changes: 3 additions & 3 deletions cmd/autoscan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
)

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down