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

View File

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