2021-04-26 17:25:47 +00:00
package files
import (
"errors"
2021-04-28 10:37:04 +00:00
"fmt"
2021-05-09 09:36:39 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
2021-04-26 17:25:47 +00:00
"path/filepath"
"strings"
2021-04-28 10:12:36 +00:00
"github.com/adrg/xdg"
2021-04-28 10:37:04 +00:00
"github.com/mitchellh/go-homedir"
2021-04-26 17:25:47 +00:00
)
var paths struct {
tokensJSON string
registrationCredentialsJSON string
fixedCredentialsJSON string
}
func TokensJSON ( ) string { return paths . tokensJSON }
func RegistrationCredentialsJSON ( ) string { return paths . registrationCredentialsJSON }
func FixedCredentialsJSON ( ) string { return paths . fixedCredentialsJSON }
// CalculatePaths looks for all external paths and stores them. Tries its best to find any errors. It is safe it to call it multiple times in order to save new paths.
func CalculatePaths ( ) error {
if dir , err := registrationCredentialsPath ( ) ; err != nil {
return err
} else {
paths . registrationCredentialsJSON = dir
}
if dir , err := tokenStoragePath ( ) ; err != nil {
return err
} else {
paths . tokensJSON = dir
}
if dir , err := fixedCredentialsPath ( ) ; err != nil {
return err
} else {
2021-04-28 10:12:36 +00:00
paths . fixedCredentialsJSON = dir
2021-04-26 17:25:47 +00:00
}
return nil
}
func tokenStoragePath ( ) ( string , error ) {
dir , err := xdg . DataFile ( "mycorrhiza/tokens.json" )
if err != nil {
return "" , err
}
2021-05-09 09:36:39 +00:00
if strings . HasPrefix ( dir , cfg . WikiDir ) {
2021-04-26 17:25:47 +00:00
return "" , errors . New ( "wiki storage directory includes private config files" )
}
return dir , nil
}
func registrationCredentialsPath ( ) ( string , error ) {
2021-04-28 10:37:04 +00:00
var err error
2021-05-09 09:36:39 +00:00
path := cfg . RegistrationCredentialsPath
2021-04-28 10:37:04 +00:00
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 )
}
2021-04-26 17:25:47 +00:00
2021-04-28 10:37:04 +00:00
path , err = filepath . Abs ( path )
2021-04-26 17:25:47 +00:00
if err != nil {
2021-04-28 10:37:04 +00:00
return "" , fmt . Errorf ( "cannot expand RegistrationCredentialsPath: %w" , err )
2021-04-26 17:25:47 +00:00
}
}
2021-04-28 10:37:04 +00:00
2021-04-26 17:25:47 +00:00
return path , nil
}
func fixedCredentialsPath ( ) ( string , error ) {
2021-04-28 10:12:36 +00:00
var err error
2021-05-09 09:36:39 +00:00
path := cfg . FixedAuthCredentialsPath
2021-04-28 10:37:04 +00:00
if len ( path ) > 0 {
path , err = homedir . Expand ( path )
if err != nil {
return "" , fmt . Errorf ( "cannot expand FixedAuthCredentialsPath: %w" , err )
}
2021-04-26 17:25:47 +00:00
2021-04-28 10:37:04 +00:00
path , err = filepath . Abs ( path )
if err != nil {
return "" , fmt . Errorf ( "cannot expand FixedAuthCredentialsPath: %w" , err )
}
2021-04-28 10:12:36 +00:00
}
2021-04-28 10:37:04 +00:00
return path , nil
2021-04-26 17:25:47 +00:00
}