fs.go

 1package config
 2
 3import (
 4	"fmt"
 5	"os"
 6	"path/filepath"
 7	"runtime"
 8)
 9
10var testConfigDir string
11
12func baseConfigPath() string {
13	if testConfigDir != "" {
14		return testConfigDir
15	}
16
17	xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
18	if xdgConfigHome != "" {
19		return filepath.Join(xdgConfigHome, "crush")
20	}
21
22	// return the path to the main config directory
23	// for windows, it should be in `%LOCALAPPDATA%/crush/`
24	// for linux and macOS, it should be in `$HOME/.config/crush/`
25	if runtime.GOOS == "windows" {
26		localAppData := os.Getenv("LOCALAPPDATA")
27		if localAppData == "" {
28			localAppData = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Local")
29		}
30		return filepath.Join(localAppData, appName)
31	}
32
33	return filepath.Join(os.Getenv("HOME"), ".config", appName)
34}
35
36func baseDataPath() string {
37	if testConfigDir != "" {
38		return testConfigDir
39	}
40
41	xdgDataHome := os.Getenv("XDG_DATA_HOME")
42	if xdgDataHome != "" {
43		return filepath.Join(xdgDataHome, appName)
44	}
45
46	// return the path to the main data directory
47	// for windows, it should be in `%LOCALAPPDATA%/crush/`
48	// for linux and macOS, it should be in `$HOME/.local/share/crush/`
49	if runtime.GOOS == "windows" {
50		localAppData := os.Getenv("LOCALAPPDATA")
51		if localAppData == "" {
52			localAppData = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Local")
53		}
54		return filepath.Join(localAppData, appName)
55	}
56
57	return filepath.Join(os.Getenv("HOME"), ".local", "share", appName)
58}
59
60func ConfigPath() string {
61	return filepath.Join(baseConfigPath(), fmt.Sprintf("%s.json", appName))
62}
63
64func CrushInitialized() bool {
65	cfgPath := ConfigPath()
66	if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
67		// config file does not exist, so Crush is not initialized
68		return false
69	}
70	return true
71}