From b65cef96c3f0ba5fcc2fa525e84ae71ef6904f30 Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Thu, 15 Aug 2024 12:27:48 -0500 Subject: [PATCH] Replaced the relative path on Windows with an absolute path --- config.go | 16 ++-------------- main.go | 15 ++++++++------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/config.go b/config.go index 5c296a6..c6a1abf 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,6 @@ import ( "errors" "io/fs" "os" - "os/user" "path/filepath" "regexp" "runtime" @@ -24,21 +23,10 @@ func runningOnWindows() bool { // generateDefaultConfigs is used to generate a folder of default config files // for common languages. These default config files are embedded into the program, and will -// be outputted into a directory. +// be outputted into the given directory. // // If there is an error encountered, the error is returned. -func generateDefaultConfigs() error { - - var configOutputPath string // Location of config files, depends on OS - if runningOnWindows() { - configOutputPath = "%APPDATA%\\ccat" - } else { - currentUser, err := user.Current() - if err != nil { - panic(err) - } - configOutputPath = filepath.Join("/home/" + currentUser.Username + "/.config/ccat/") - } +func generateDefaultConfigs(configOutputPath string) error { err := os.MkdirAll(configOutputPath, 0755) if err != nil { if os.IsExist(err) { diff --git a/main.go b/main.go index 32942ff..5291ff2 100644 --- a/main.go +++ b/main.go @@ -78,18 +78,19 @@ func main() { flag.Parse() // Check if config exists. If it doesn't, generate the config files. - var configPath string // Location of config files, depends on OS + var configPath string // Location of config files, depends on OS + currentUser, err := user.Current() // Get current user, to determine config path + if err != nil { + panic(err) + } if runningOnWindows() { - configPath = "%APPDATA%\\ccat" + configPath = filepath.Join("C:\\Users" + currentUser.Username + "AppData\\Local\\ccat\\") } else { - currentUser, err := user.Current() - if err != nil { - panic(err) - } + configPath = filepath.Join("/home/" + currentUser.Username + "/.config/ccat/") } if _, err := os.Stat(configPath); os.IsNotExist(err) { - generateDefaultConfigs() + generateDefaultConfigs(configPath) } // Check if user has provided a file name