Skip to content

Commit

Permalink
fixes #247 and reduces use of init()
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlehane committed Apr 18, 2024
1 parent aa72ecc commit 8af011e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
27 changes: 9 additions & 18 deletions pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,24 @@
package config

import (
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)

// the default Home location is a "siegfried" folder in the user's application data folder, which can be overridden by setting the SIEGFRIED_HOME environment variable
func init() {
func defaultHome() string {
if home, ok := os.LookupEnv("SIEGFRIED_HOME"); ok {
siegfried.home = home
return home
} else {
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

// if a home directory already exists in the legacy location continue using it, otherwise default to a XDG-aware OS-specific application data directory
siegfried.home = filepath.Join(home, "siegfried")
if _, err := os.Stat(siegfried.home); err != nil {
if errors.Is(err, fs.ErrNotExist) {
siegfried.home = filepath.Join(userDataDir(home), "siegfried")
} else {
log.Fatal(err)
}
base, _ := os.UserHomeDir()
// if a home directory already exists in the legacy location continue using it
home = filepath.Join(base, "siegfried")
if _, err := os.Stat(siegfried.home); err == nil {
return home
}
// otherwise default to a XDG-aware OS-specific application data directory
return filepath.Join(userDataDir(home), "siegfried")
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/config/siegfried.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func Version() [3]int {

// Home reports the siegfried HOME location (e.g. /usr/home/siegfried).
func Home() string {
if siegfried.home == "" {
siegfried.home = defaultHome()
}
return siegfried.home
}

Expand Down

0 comments on commit 8af011e

Please sign in to comment.