From 756aaa65728210cfb3db388dcc425c8ca8e98e46 Mon Sep 17 00:00:00 2001 From: Darri Steinn Konradsson Date: Wed, 8 Jan 2020 21:14:19 +0000 Subject: [PATCH] Only use a dotfile for home dir --- README.md | 14 +++----------- cli/cmd/root.go | 8 ++++---- cli/utils/filesystem.go | 15 ++++++++------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 7795c1c..8fe7d05 100644 --- a/README.md +++ b/README.md @@ -58,20 +58,12 @@ Check out the [`api`](https://github.com/darrikonn/cheat/blob/master/API.md). ## Configuring The location of your cheat data and your configuration will depend on these environment variables (in this order): -1. **CHEAT_HOME**: determines where your `.cheatsheet.db` and `.cheat.yaml` file will live +1. **CHEAT_HOME**: determines where your `cheatsheet.db` and `cheat.yaml` file will live 2. **XDG_CONFIG_HOME**: a fallback if `$CHEAT_HOME` is not set -3. **HOME**: a fallback if `$XDG_CONFIG_HOME` is not set - -### Database -By default the database will be located at `$CHEAT_HOME/.cheatsheet.db`. -You can change your database path, and name, by specifying `database` in your `$CHEAT_HOME/.cheat.yaml` file: -```yaml -database: ~/.custom-database-name.db -``` -This results in a database instance at `~/.custom-database-name.db` +3. **HOME**: a fallback if `$XDG_CONFIG_HOME` is not set. If `$HOME` is used; all cheat files will be transformed to a dotfile, i.e.`~/.cheatsheet.db` and `~/.cheat.yaml`. ### Editor -When adding/editing a cheat, you'll be prompted to edit the cheat's `description` in your preferred editor. You can set your desired editor in the `$CHEAT_HOME/.cheat.yaml` config file: +When adding/editing a cheat, you'll be prompted to edit the cheat's `description` in your preferred editor. You can set your desired editor in the `$CHEAT_HOME/cheat.yaml` config file: ```yaml editor: nvim ``` diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 3acabc7..d80c892 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -112,15 +112,15 @@ func errorHandling() { func initConfig() { // Find home directory. - homeDirectory := utils.HomeDir() + homeDirectory, prefix := utils.CheatHomeDir() - // Search config in home directory with name ".cheat" (without extension). + // Search config in home directory with name "cheat" (without extension). viper.AddConfigPath(homeDirectory) - viper.SetConfigName(".cheat") + viper.SetConfigName(prefix + "cheat") // Fallback to "vi" for the editor viper.SetDefault("editor", utils.GetEnvWithFallback("EDITOR", "vi")) - viper.SetDefault("database", homeDirectory+"/.cheatsheet.db") + viper.SetDefault("database", homeDirectory+"/"+prefix+"cheatsheet.db") // Load config _ = viper.ReadInConfig() diff --git a/cli/utils/filesystem.go b/cli/utils/filesystem.go index 890eba2..4594c65 100644 --- a/cli/utils/filesystem.go +++ b/cli/utils/filesystem.go @@ -27,7 +27,7 @@ func ResolvePath(path string) string { // CreateFile : creates a file in path func CreateFile(file string) { - _, err := os.Create(file) + _, err := os.Create(ResolvePath(file)) if err != nil { panic(exceptions.CheatException("Could not create file: \""+file+"\"", err)) } @@ -51,9 +51,10 @@ func FileExists(name string) bool { return true } -// HomeDir : finds the cheat's home directory in order to -// unclutter your $HOME directory -func HomeDir() string { +// CheatHomeDir : finds the cheat's home directory in order to +// unclutter your $HOME directory. It also returns a prefix for +// cheat files +func CheatHomeDir() (string, string) { var dir string // Check if CHEAT_HOME environment is set @@ -67,7 +68,7 @@ func HomeDir() string { ), ) } - return dir + return ResolvePath(dir), "" } // Check if XDG_CONFIG_HOME environment is set @@ -87,7 +88,7 @@ func HomeDir() string { if !FileExists(dir) { CreateDir(dir) } - return dir + return ResolvePath(dir), "" } // Else default to home directory @@ -95,5 +96,5 @@ func HomeDir() string { if err != nil { panic(exceptions.CheatException("Could not find home directory", err)) } - return dir + return dir, "." }