From 8bb4703577e7beb8efc27bbe83fd2c1f2101629a Mon Sep 17 00:00:00 2001 From: ycc Date: Sat, 28 Jan 2023 19:26:58 +0100 Subject: [PATCH] export config paths --- configfile_unix.go | 2 +- configfile_windows.go | 2 +- creds.go | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/configfile_unix.go b/configfile_unix.go index 04dc01e..f522e0f 100644 --- a/configfile_unix.go +++ b/configfile_unix.go @@ -6,7 +6,7 @@ package localconfig import "os" // Configfile is the OS dependent config file location -func getConfig(filename string) string { +func GetConfigPath(filename string) string { home, _ := os.UserHomeDir() return home + "/." + filename } diff --git a/configfile_windows.go b/configfile_windows.go index befabe6..b0da760 100644 --- a/configfile_windows.go +++ b/configfile_windows.go @@ -4,6 +4,6 @@ package localconfig // Configfile is the OS dependent config file location -func getConfig(filename string) string { +func GetConfigPath(filename string) string { return ".\\" + filename } diff --git a/creds.go b/creds.go index f568afd..ee98d1f 100644 --- a/creds.go +++ b/creds.go @@ -31,7 +31,7 @@ func randomString(n int) string { } func loadRootKey() []byte { - rkpath := getConfig("rk") + rkpath := GetConfigPath("rk") rk, err := os.ReadFile(rkpath) if err != nil { rk = []byte(randomString(64)) @@ -41,7 +41,7 @@ func loadRootKey() []byte { } func (creds *Creds) Exists(filename string) bool { - if _, err := os.Stat(getConfig(filename)); err == nil { + if _, err := os.Stat(GetConfigPath(filename)); err == nil { return true } else if errors.Is(err, os.ErrNotExist) { return false @@ -64,9 +64,9 @@ func (creds *Creds) Init(filename string) { if silentPassword != "" { creds.Password = silentPassword b, _ := json.Marshal(creds) - fmt.Println("Writing credentials to : " + getConfig(filename)) + fmt.Println("Writing credentials to : " + GetConfigPath(filename)) armor, _ := helper.EncryptMessageWithPassword(loadRootKey(), string(b)) - os.WriteFile(getConfig(filename), []byte(armor), 0600) + os.WriteFile(GetConfigPath(filename), []byte(armor), 0600) } } } @@ -80,13 +80,13 @@ func (creds *Creds) NewPw(filename string) { creds.Password = silentPassword b, _ := json.Marshal(creds) armor, _ := helper.EncryptMessageWithPassword(loadRootKey(), string(b)) - os.WriteFile(getConfig(filename), []byte(armor), 0600) + os.WriteFile(GetConfigPath(filename), []byte(armor), 0600) } } // Load credentials if available or creates them func (creds *Creds) Load(filename string) error { - indata, err := os.ReadFile(getConfig(filename)) + indata, err := os.ReadFile(GetConfigPath(filename)) if err != nil { creds.Init(filename) return nil