1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-05 02:29:54 +00:00

Expand home directory in config paths

This commit is contained in:
handlerug 2021-04-28 17:37:04 +07:00
parent 5bbfc6e049
commit df9e059e69
No known key found for this signature in database
GPG Key ID: 38009F0605051491
3 changed files with 35 additions and 16 deletions

View File

@ -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
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=