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

Merge pull request #511 from wtfutil/WTF-510-no-config-dir

WTF-510 no config dir
This commit is contained in:
Chris Cummer 2019-07-20 12:28:49 -07:00 committed by GitHub
commit b6dc9e8ca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 35 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) {
@ -53,8 +56,8 @@ func MigrateOldConfig() {
/* -------------------- Config Migration -------------------- */
// ConfigDir returns the absolute path to the configuration directory
func ConfigDir() (string, error) {
configDir, err := expandHomeDir(ConfigDirV2)
func WtfConfigDir() (string, error) {
configDir, err := expandHomeDir(WtfConfigDirV2)
if err != nil {
return "", err
}
@ -62,21 +65,37 @@ 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)
displayXdgConfigDirCreateError(err)
os.Exit(1)
}
}
}
// 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, _ := WtfConfigDir()
if _, err := os.Stat(wtfConfigDir); os.IsNotExist(err) {
err := os.Mkdir(wtfConfigDir, os.ModePerm)
if err != nil {
displayWtfConfigDirCreateError(err)
os.Exit(1)
}
}
}
// 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)
@ -97,7 +116,7 @@ func CreateConfigFile() {
// If successful, eturns the absolute path to the file
// If unsuccessful, returns an error
func CreateFile(fileName string) (string, error) {
configDir, err := ConfigDir()
configDir, err := WtfConfigDir()
if err != nil {
return "", err
}
@ -120,21 +139,13 @@ func CreateFile(fileName string) (string, error) {
return filePath, nil
}
// LoadConfigFile loads the config.yml file to configure the app
func LoadConfigFile(filePath string) *config.Config {
// LoadWtfConfigFile loads the config.yml file to configure the app
func LoadWtfConfigFile(filePath string) *config.Config {
absPath, _ := expandHomeDir(filePath)
cfg, err := config.ParseYamlFile(absPath)
if err != nil {
fmt.Println("\n\033[1mERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.")
fmt.Println()
fmt.Println("This could mean one of two things:")
fmt.Println()
fmt.Println(" 1. Your \033[0;33mconfig.yml\033[0m file is missing. Check in \033[0;33m~/.config/wtf\033[0m to see if \033[0;33mconfig.yml\033[0m is there.")
fmt.Println(" 2. Your \033[0;33mconfig.yml\033[0m file has a syntax error. Try running it through http://www.yamllint.com to check for errors.")
fmt.Println()
fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error())
displayWtfConfigFileLoadError(err)
os.Exit(1)
}
@ -208,6 +219,29 @@ const simpleConfig = `wtf:
/* -------------------- Unexported Functions -------------------- */
func displayXdgConfigDirCreateError(err error) {
fmt.Printf("\n\033[1mERROR:\033[0m Could not create the '\033[0;33m%s\033[0m' directory.\n", XdgConfigDir)
fmt.Println()
fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error())
}
func displayWtfConfigDirCreateError(err error) {
fmt.Printf("\n\033[1mERROR:\033[0m Could not create the '\033[0;33m%s\033[0m' directory.\n", WtfConfigDirV2)
fmt.Println()
fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error())
}
func displayWtfConfigFileLoadError(err error) {
fmt.Println("\n\033[1mERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.")
fmt.Println()
fmt.Println("This could mean one of two things:")
fmt.Println()
fmt.Println(" 1. Your \033[0;33mconfig.yml\033[0m file is missing. Check in \033[0;33m~/.config/wtf\033[0m to see if \033[0;33mconfig.yml\033[0m is there.")
fmt.Println(" 2. Your \033[0;33mconfig.yml\033[0m file has a syntax error. Try running it through http://www.yamllint.com to check for errors.")
fmt.Println()
fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error())
}
// Expand expands the path to include the home directory if the path
// is prefixed with `~`. If it isn't prefixed with `~`, the path is
// returned as-is.

View File

@ -93,7 +93,7 @@ func watchForConfigChanges(app *tview.Application, configFilePath string, grid *
// Disable all widgets to stop scheduler goroutines and remove widgets from memory
disableAllWidgets(runningWidgets)
config := cfg.LoadConfigFile(absPath)
config := cfg.LoadWtfConfigFile(absPath)
widgets := maker.MakeWidgets(app, pages, config)
runningWidgets = widgets
@ -129,13 +129,14 @@ 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()
flags.Parse()
config := cfg.LoadConfigFile(flags.ConfigFilePath())
config := cfg.LoadWtfConfigFile(flags.ConfigFilePath())
flags.RenderIf(version, config)
if flags.Profile {

View File

@ -49,7 +49,7 @@ func (widget *Widget) displayPrev() {
}
func (widget *Widget) openFile() {
confDir, _ := cfg.ConfigDir()
confDir, _ := cfg.WtfConfigDir()
wtf.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath))
}

View File

@ -102,7 +102,7 @@ func (widget *Widget) init() {
// Loads the todo list from Yaml file
func (widget *Widget) load() {
confDir, _ := cfg.ConfigDir()
confDir, _ := cfg.WtfConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath)
fileData, _ := wtf.ReadFileBytes(filePath)
@ -135,7 +135,7 @@ func (widget *Widget) newItem() {
// persist writes the todo list to Yaml file
func (widget *Widget) persist() {
confDir, _ := cfg.ConfigDir()
confDir, _ := cfg.WtfConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath)
fileData, _ := yaml.Marshal(&widget.list)