Skip to content

Commit

Permalink
Only use a dotfile for home dir
Browse files Browse the repository at this point in the history
  • Loading branch information
darrikonn committed Jan 8, 2020
1 parent c78b2db commit 756aaa6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
8 changes: 4 additions & 4 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 8 additions & 7 deletions cli/utils/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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
Expand All @@ -67,7 +68,7 @@ func HomeDir() string {
),
)
}
return dir
return ResolvePath(dir), ""
}

// Check if XDG_CONFIG_HOME environment is set
Expand All @@ -87,13 +88,13 @@ func HomeDir() string {
if !FileExists(dir) {
CreateDir(dir)
}
return dir
return ResolvePath(dir), ""
}

// Else default to home directory
dir, err := os.UserHomeDir()
if err != nil {
panic(exceptions.CheatException("Could not find home directory", err))
}
return dir
return dir, "."
}

0 comments on commit 756aaa6

Please sign in to comment.