Skip to content

Commit

Permalink
fix: fallback to system default xdg values (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Dec 8, 2020
1 parent b48625f commit ed96942
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var cleanCmd = &cobra.Command{
userHomeDir, err := utils.GetHomeDir()
tui.FatalIfError("Unable to detect user home dir", err)

utils.ApplyDefaultXdgEnv()

dotfiles, err := dotfile.Detect(userHomeDir)
tui.FatalIfError("Failed to detect dotfiles in home dir", err)

Expand Down
1 change: 1 addition & 0 deletions internal/tui/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
Magenta ctc.Color = ctc.ForegroundBrightMagenta
Green ctc.Color = ctc.ForegroundBrightGreen
Gray ctc.Color = ctc.ForegroundBrightBlack
Yellow ctc.Color = ctc.ForegroundBrightYellow
)

func ApplyStyle(style ctc.Color, text string) string {
Expand Down
5 changes: 5 additions & 0 deletions internal/tui/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func Debug(format string, a ...interface{}) {
}
}

func Warn(format string, a ...interface{}) {
format = fmt.Sprintf("%s %s\n", ApplyStyle(Yellow, "WARNING:"), format)
fmt.Fprintf(os.Stderr, format, a...)
}

func Print(format string, a ...interface{}) {
fmt.Printf(format+"\n", a...)
}
Expand Down
26 changes: 26 additions & 0 deletions internal/utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os/user"

"github.com/adrg/xdg"

"github.com/doron-cohen/antidot/internal/tui"
)

func GetHomeDir() (string, error) {
Expand All @@ -21,6 +23,30 @@ func ExpandEnv(text string) string {
return os.ExpandEnv(text)
}

func ApplyDefaultXdgEnv() {
xdgSystemDefaults := map[string]string{
"XDG_CONFIG_HOME": xdg.ConfigHome,
"XDG_CACHE_HOME": xdg.CacheHome,
"XDG_DATA_HOME": xdg.DataHome,
}
printNewline := false
for name, defaultValue := range xdgSystemDefaults {
if value, exists := os.LookupEnv(name); !exists || value == "" {
tui.Warn(
"Environment variable %s not set. Using default path: %s",
tui.ApplyStyle(tui.Yellow, name),
tui.ApplyStyle(tui.Yellow, defaultValue),
)
os.Setenv(name, defaultValue)
printNewline = true
}
}

if printNewline {
fmt.Println("")
}
}

func XdgVarsExport() string {
return fmt.Sprintf(`export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"%s"}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-"%s"}"
Expand Down

0 comments on commit ed96942

Please sign in to comment.