diff --git a/files/files.go b/files/files.go index d54566b..5f03a10 100644 --- a/files/files.go +++ b/files/files.go @@ -2,12 +2,13 @@ package files import ( "errors" - "log" + "fmt" "path/filepath" "strings" "github.com/adrg/xdg" "github.com/bouncepaw/mycorrhiza/util" + "github.com/mitchellh/go-homedir" ) var paths struct { @@ -55,28 +56,43 @@ func tokenStoragePath() (string, error) { } func registrationCredentialsPath() (string, error) { - path, err := filepath.Abs(util.RegistrationCredentialsPath) - if err != nil { - return "", nil + var err error + path := util.RegistrationCredentialsPath + + if len(path) == 0 { + path, err = xdg.DataFile("mycorrhiza/registration.json") + if err != nil { + return "", fmt.Errorf("cannot get a file to registration credentials, so no registered users will be saved: %w", err) + } + } else { + path, err = homedir.Expand(path) + if err != nil { + return "", fmt.Errorf("cannot expand RegistrationCredentialsPath: %w", err) + } + + path, err = filepath.Abs(path) + if err != nil { + return "", fmt.Errorf("cannot expand RegistrationCredentialsPath: %w", err) + } } - if path == "" { - dir, err := xdg.DataFile("mycorrhiza/registration.json") - if err != nil { - log.Println("Error: cannot get a file to registration credentials, so no registered users will be saved:", err) - return "", err - } - path = dir - } return path, nil } func fixedCredentialsPath() (string, error) { var err error - var path = "" + path := util.FixedCredentialsPath - if len(util.FixedCredentialsPath) > 0 { - path, err = filepath.Abs(util.FixedCredentialsPath) + if len(path) > 0 { + path, err = homedir.Expand(path) + if err != nil { + return "", fmt.Errorf("cannot expand FixedAuthCredentialsPath: %w", err) + } + + path, err = filepath.Abs(path) + if err != nil { + return "", fmt.Errorf("cannot expand FixedAuthCredentialsPath: %w", err) + } } - return path, err + return path, nil } diff --git a/go.mod b/go.mod index 6f4d76d..7e4d01e 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-ini/ini v1.62.0 github.com/gorilla/feeds v1.1.1 github.com/kr/pretty v0.2.1 // indirect + github.com/mitchellh/go-homedir v1.1.0 github.com/smartystreets/goconvey v1.6.4 // indirect github.com/valyala/quicktemplate v1.6.3 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 diff --git a/go.sum b/go.sum index 5c4b13d..16305f9 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=