mirror of
https://salsa.debian.org/mdosch/go-sendxmpp
synced 2024-11-17 03:25:33 +00:00
Rework getting data path.
This commit is contained in:
parent
080100486e
commit
c7376832ce
29
helpers.go
29
helpers.go
@ -56,14 +56,14 @@ func getClientID() (string, error) {
|
||||
var clientID string
|
||||
clientIDLoc, err := getClientIDLoc()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return strError, err
|
||||
}
|
||||
buf, err := readFile(clientIDLoc)
|
||||
if err != nil {
|
||||
clientID = uuid.NewString()
|
||||
file, err := os.Create(clientIDLoc)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("getClientID: failed to create clientid file: %w", err)
|
||||
return strError, fmt.Errorf("getClientID: failed to create clientid file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
if runtime.GOOS != "windows" {
|
||||
@ -73,7 +73,7 @@ func getClientID() (string, error) {
|
||||
}
|
||||
_, err = file.Write([]byte(clientID))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("getClientID: failed to write client id file: %w", err)
|
||||
return strError, fmt.Errorf("getClientID: failed to write client id file: %w", err)
|
||||
}
|
||||
} else {
|
||||
clientID = buf.String()
|
||||
@ -81,9 +81,7 @@ func getClientID() (string, error) {
|
||||
return clientID, nil
|
||||
}
|
||||
|
||||
// TODO: Dedup code by putting detection of dataDir in a separate function and
|
||||
// remove it from func oxGetPubKeyLoc oxGetPrivKeyLoc and getClientIDLoc
|
||||
func getClientIDLoc() (string, error) {
|
||||
func getDataPath(folder string) (string, error) {
|
||||
var err error
|
||||
var homeDir, dataDir string
|
||||
switch {
|
||||
@ -98,20 +96,31 @@ func getClientIDLoc() (string, error) {
|
||||
default:
|
||||
homeDir, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("getClientIDLoc: failed to determine user dir: %w", err)
|
||||
return strError, fmt.Errorf("getDataPath: failed to determine user dir: %w", err)
|
||||
}
|
||||
if homeDir == "" {
|
||||
return strError, errors.New("getClientIDLoc: received empty string for home directory")
|
||||
return strError, errors.New("getDataPath: received empty string for home directory")
|
||||
}
|
||||
dataDir = homeDir + "/.local/share"
|
||||
}
|
||||
dataDir += "/go-sendxmpp/"
|
||||
if folder != "" && !strings.HasSuffix(folder, "/") {
|
||||
folder = fmt.Sprintf("%s/", folder)
|
||||
}
|
||||
dataDir = fmt.Sprintf("%s/go-sendxmpp/%s", dataDir, folder)
|
||||
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(dataDir, defaultDirRights)
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("getClientIDLoc: could not create folder: %w", err)
|
||||
return strError, fmt.Errorf("getDataPath: could not create folder: %w", err)
|
||||
}
|
||||
}
|
||||
return dataDir, nil
|
||||
}
|
||||
|
||||
func getClientIDLoc() (string, error) {
|
||||
dataDir, err := getDataPath("")
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("getClientIDLoc: %w", err)
|
||||
}
|
||||
dataFile := dataDir + "clientid"
|
||||
return dataFile, nil
|
||||
}
|
||||
|
60
ox.go
60
ox.go
@ -316,33 +316,9 @@ func oxPublishPubKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, pubKey *
|
||||
}
|
||||
|
||||
func oxGetPrivKeyLoc(jid string) (string, error) {
|
||||
var err error
|
||||
var homeDir, dataDir string
|
||||
switch {
|
||||
case os.Getenv("$XDG_DATA_HOME") != "":
|
||||
dataDir = os.Getenv("$XDG_DATA_HOME")
|
||||
case os.Getenv("$XDG_HOME") != "":
|
||||
homeDir = os.Getenv("$XDG_HOME")
|
||||
dataDir = homeDir + "/.local/share"
|
||||
case os.Getenv("$HOME") != "":
|
||||
homeDir = os.Getenv("$HOME")
|
||||
dataDir = homeDir + "/.local/share"
|
||||
default:
|
||||
homeDir, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("oxGetPrivKeyLoc: failed to determine user dir: %w", err)
|
||||
}
|
||||
if homeDir == "" {
|
||||
return strError, errors.New("oxGetPrivKeyLoc: received empty string for home directory")
|
||||
}
|
||||
dataDir = homeDir + "/.local/share"
|
||||
}
|
||||
dataDir += "/go-sendxmpp/oxprivkeys/"
|
||||
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(dataDir, defaultDirRights)
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("oxGetPrivKeyLoc: could not create folder for private keys: %w", err)
|
||||
}
|
||||
dataDir, err := getDataPath("oxprivkeys/")
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("oxGetPubKeyLoc: %w", err)
|
||||
}
|
||||
// TODO: Remove handling of oldDataFile in a later version when it's very likely that there are no
|
||||
// more versions in use using the oldDataFile (<0.8.3).
|
||||
@ -365,33 +341,9 @@ func oxGetPrivKeyLoc(jid string) (string, error) {
|
||||
}
|
||||
|
||||
func oxGetPubKeyLoc(fingerprint string) (string, error) {
|
||||
var err error
|
||||
var homeDir, dataDir string
|
||||
switch {
|
||||
case os.Getenv("$XDG_DATA_HOME") != "":
|
||||
dataDir = os.Getenv("$XDG_DATA_HOME")
|
||||
case os.Getenv("$XDG_HOME") != "":
|
||||
homeDir = os.Getenv("$XDG_HOME")
|
||||
dataDir = homeDir + "/.local/share"
|
||||
case os.Getenv("$HOME") != "":
|
||||
homeDir = os.Getenv("$HOME")
|
||||
dataDir = homeDir + "/.local/share"
|
||||
default:
|
||||
homeDir, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("oxGetPubKeyLoc: failed to determine user dir: %w", err)
|
||||
}
|
||||
if homeDir == "" {
|
||||
return strError, errors.New("oxGetPubKeyLoc: received empty string for home directory")
|
||||
}
|
||||
dataDir = homeDir + "/.local/share"
|
||||
}
|
||||
dataDir += "/go-sendxmpp/oxpubkeys/"
|
||||
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(dataDir, defaultDirRights)
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("oxGetPubKeyLoc: could not create folder for public keys: %w", err)
|
||||
}
|
||||
dataDir, err := getDataPath("oxpubkeys/")
|
||||
if err != nil {
|
||||
return strError, fmt.Errorf("oxGetPubKeyLoc: %w", err)
|
||||
}
|
||||
dataFile := dataDir + fingerprint
|
||||
return dataFile, nil
|
||||
|
Loading…
Reference in New Issue
Block a user