Skip to content

Commit

Permalink
Merge pull request #4 from darrikonn/unclutter-home-directory
Browse files Browse the repository at this point in the history
Unclutter home directory
  • Loading branch information
darrikonn authored Jan 8, 2020
2 parents cb50050 + 56795bc commit b35841b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
11 changes: 4 additions & 7 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (

rootCmd = &cobra.Command{
Use: "cheat",
Version: "1.0.0",
Version: "1.1.0",
SilenceUsage: true,
Short: "Cheat is a personal cheatsheet manager",
Long: strings.TrimSpace(`
Expand Down Expand Up @@ -112,18 +112,15 @@ func errorHandling() {

func initConfig() {
// Find home directory.
homeDirectory, err := os.UserHomeDir()
if err != nil {
panic(exceptions.CheatException("Could not find home directory", err))
}
homeDirectory := utils.HomeDir()

// Search config in home directory with name ".cheat" (without extension).
viper.AddConfigPath(homeDirectory)
viper.SetConfigName(".cheat")

// Fallback to "vi" for the editor
viper.SetDefault("editor", utils.GetEnv("EDITOR", "vi"))
viper.SetDefault("database", "~/.cheatsheet.db")
viper.SetDefault("editor", utils.GetEnvWithFallback("EDITOR", "vi"))
viper.SetDefault("database", homeDirectory+"/.cheatsheet.db")

// Load config
_ = viper.ReadInConfig()
Expand Down
57 changes: 56 additions & 1 deletion cli/utils/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,67 @@ func CreateFile(file string) {
}
}

// CreateDir : creates a directory in path
func CreateDir(dir string) {
err := os.Mkdir(ResolvePath(dir), 0755)
if err != nil {
panic(exceptions.CheatException("Could not create directory: \""+dir+"\"", err))
}
}

// FileExists : check if file exists
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if _, err := os.Stat(ResolvePath(name)); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}

// HomeDir : finds the cheat's home directory in order to
// unclutter your $HOME directory
func HomeDir() string {
var dir string

// Check if CHEAT_HOME environment is set
dir = os.Getenv("CHEAT_HOME")
if dir != "" {
if !FileExists(dir) {
panic(
exceptions.CheatException(
"CHEAT_HOME environment variable set to \""+dir+"\", but directory does not exist",
nil,
),
)
}
return dir
}

// Check if XDG_CONFIG_HOME environment is set
dir = os.Getenv("XDG_CONFIG_HOME")
if dir != "" {
if !FileExists(dir) {
panic(
exceptions.CheatException(
"XDG_CONFIG_HOME environment variable set to \""+dir+"\", but directory does not exist",
nil,
),
)
}
// Respect the file structure of XDG_CONFIG_HOME
// and create the "cheat" directory if it doesn't exist
dir = dir + "/cheat"
if !FileExists(dir) {
CreateDir(dir)
}
return dir
}

// Else default to home directory
dir, err := os.UserHomeDir()
if err != nil {
panic(exceptions.CheatException("Could not find home directory", err))
}
return dir
}
4 changes: 2 additions & 2 deletions cli/utils/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"cheat/cli/exceptions"
)

// GetEnv : gets environment variable with a fallback
func GetEnv(key string, fallback string) string {
// GetEnvWithFallback : gets environment variable with a fallback
func GetEnvWithFallback(key string, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
Expand Down

0 comments on commit b35841b

Please sign in to comment.