Skip to content

Commit

Permalink
fix: Use golang's native directory functions
Browse files Browse the repository at this point in the history
fix: Exit if dbPath not found
  • Loading branch information
ll931217 committed Aug 28, 2024
1 parent 568eff4 commit 6384d00
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
25 changes: 18 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,36 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)

type Config struct {
DbPath string
}

var (
configHome = os.Getenv("XDG_CONFIG_HOME")
configDir = configHome + "/kancli"
configFile = configDir + "/config.json"
)

func readConfig() Config {
mkdirErr := os.MkdirAll(configDir, 0755)
// look for $XDG_CONFIG_HOME/kancli/config.json or $HOME/.config/kancli/config.json
configDir, err := os.UserConfigDir()
if err != nil {
homeDir, err := os.UserHomeDir()
if err != nil {
// cant find home or config just give up
return Config{}
}
configDir = filepath.Join(homeDir, ".config")
}

// Create ~/.config/kancli/ if it does not exist
configPath := filepath.Join(configDir, "kancli")

mkdirErr := os.MkdirAll(configPath, 0755)
if mkdirErr != nil {
log.Fatal(mkdirErr)
}

configFile := filepath.Join(configPath, "config.json")

var config Config
if _, err := os.Stat(configFile); os.IsNotExist(err) {
file, err := os.Create(configFile)
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func main() {
defer f.Close()

config := readConfig()
if config.DbPath == "" {
fmt.Println("No dbPath found in config")
os.Exit(1)
}
csvFile = config.DbPath

board = NewBoard()
Expand Down

0 comments on commit 6384d00

Please sign in to comment.