1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

WTF-510 Create the ~/.config directory if it is missing

This commit is contained in:
Chris Cummer 2019-07-20 12:12:04 -07:00
parent 83018d252c
commit 00ccf8a95b
2 changed files with 34 additions and 16 deletions

View File

@ -11,19 +11,22 @@ import (
"github.com/olebedev/config" "github.com/olebedev/config"
) )
// ConfigDirV1 defines the path to the first version of configuration. Do not use this // XdgConfigDir defines the path to the minimal XDG-compatible configuration directory
const ConfigDirV1 = "~/.wtf/" const XdgConfigDir = "~/.config/"
// ConfigDirV2 defines the path to the second version of the configuration. Use this. // WtfConfigDirV1 defines the path to the first version of configuration. Do not use this
const ConfigDirV2 = "~/.config/wtf/" const WtfConfigDirV1 = "~/.wtf/"
// WtfConfigDirV2 defines the path to the second version of the configuration. Use this.
const WtfConfigDirV2 = "~/.config/wtf/"
/* -------------------- Config Migration -------------------- */ /* -------------------- Config Migration -------------------- */
// MigrateOldConfig copies any existing configuration from the old location // MigrateOldConfig copies any existing configuration from the old location
// to the new, XDG-compatible location // to the new, XDG-compatible location
func MigrateOldConfig() { func MigrateOldConfig() {
srcDir, _ := expandHomeDir(ConfigDirV1) srcDir, _ := expandHomeDir(WtfConfigDirV1)
destDir, _ := expandHomeDir(ConfigDirV2) destDir, _ := expandHomeDir(WtfConfigDirV2)
// If the old config directory doesn't exist, do not move // If the old config directory doesn't exist, do not move
if _, err := os.Stat(srcDir); os.IsNotExist(err) { if _, err := os.Stat(srcDir); os.IsNotExist(err) {
@ -54,7 +57,7 @@ func MigrateOldConfig() {
// ConfigDir returns the absolute path to the configuration directory // ConfigDir returns the absolute path to the configuration directory
func ConfigDir() (string, error) { func ConfigDir() (string, error) {
configDir, err := expandHomeDir(ConfigDirV2) configDir, err := expandHomeDir(WtfConfigDirV2)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -62,21 +65,35 @@ func ConfigDir() (string, error) {
return configDir, nil return configDir, nil
} }
// CreateConfigDir creates the wtf/ directory in the user's home dir // CreateXdgConfigDir creates the necessary base directory for storing the config file
func CreateConfigDir() { // If ~/.config is missing, it will try to create it
configDir, _ := ConfigDir() func CreateXdgConfigDir() {
xdgConfigDir, _ := expandHomeDir(XdgConfigDir)
if _, err := os.Stat(configDir); os.IsNotExist(err) { if _, err := os.Stat(xdgConfigDir); os.IsNotExist(err) {
err := os.Mkdir(configDir, os.ModePerm) err := os.Mkdir(xdgConfigDir, os.ModePerm)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
} }
// CreateConfigFile creates a simple config file in the config directory if // CreateWtfConfigDir creates the necessary directories for storing the default config file
// If ~/.config/wtf is missing, it will try to create it
func CreateWtfConfigDir() {
wtfConfigDir, _ := ConfigDir()
if _, err := os.Stat(wtfConfigDir); os.IsNotExist(err) {
err := os.Mkdir(wtfConfigDir, os.ModePerm)
if err != nil {
panic(err)
}
}
}
// CreateWtfConfigFile creates a simple config file in the config directory if
// one does not already exist // one does not already exist
func CreateConfigFile() { func CreateWtfConfigFile() {
filePath, err := CreateFile("config.yml") filePath, err := CreateFile("config.yml")
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -129,8 +129,9 @@ func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
cfg.MigrateOldConfig() cfg.MigrateOldConfig()
cfg.CreateConfigDir() cfg.CreateXdgConfigDir()
cfg.CreateConfigFile() cfg.CreateWtfConfigDir()
cfg.CreateWtfConfigFile()
// Parse and handle flags // Parse and handle flags
flags := flags.NewFlags() flags := flags.NewFlags()