export config paths

This commit is contained in:
ycc 2023-01-28 19:26:58 +01:00
parent bd93a5dee4
commit 8bb4703577
3 changed files with 8 additions and 8 deletions

View File

@ -6,7 +6,7 @@ package localconfig
import "os" import "os"
// Configfile is the OS dependent config file location // Configfile is the OS dependent config file location
func getConfig(filename string) string { func GetConfigPath(filename string) string {
home, _ := os.UserHomeDir() home, _ := os.UserHomeDir()
return home + "/." + filename return home + "/." + filename
} }

View File

@ -4,6 +4,6 @@
package localconfig package localconfig
// Configfile is the OS dependent config file location // Configfile is the OS dependent config file location
func getConfig(filename string) string { func GetConfigPath(filename string) string {
return ".\\" + filename return ".\\" + filename
} }

View File

@ -31,7 +31,7 @@ func randomString(n int) string {
} }
func loadRootKey() []byte { func loadRootKey() []byte {
rkpath := getConfig("rk") rkpath := GetConfigPath("rk")
rk, err := os.ReadFile(rkpath) rk, err := os.ReadFile(rkpath)
if err != nil { if err != nil {
rk = []byte(randomString(64)) rk = []byte(randomString(64))
@ -41,7 +41,7 @@ func loadRootKey() []byte {
} }
func (creds *Creds) Exists(filename string) bool { 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 return true
} else if errors.Is(err, os.ErrNotExist) { } else if errors.Is(err, os.ErrNotExist) {
return false return false
@ -64,9 +64,9 @@ func (creds *Creds) Init(filename string) {
if silentPassword != "" { if silentPassword != "" {
creds.Password = silentPassword creds.Password = silentPassword
b, _ := json.Marshal(creds) b, _ := json.Marshal(creds)
fmt.Println("Writing credentials to : " + getConfig(filename)) fmt.Println("Writing credentials to : " + GetConfigPath(filename))
armor, _ := helper.EncryptMessageWithPassword(loadRootKey(), string(b)) 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 creds.Password = silentPassword
b, _ := json.Marshal(creds) b, _ := json.Marshal(creds)
armor, _ := helper.EncryptMessageWithPassword(loadRootKey(), string(b)) 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 // Load credentials if available or creates them
func (creds *Creds) Load(filename string) error { func (creds *Creds) Load(filename string) error {
indata, err := os.ReadFile(getConfig(filename)) indata, err := os.ReadFile(GetConfigPath(filename))
if err != nil { if err != nil {
creds.Init(filename) creds.Init(filename)
return nil return nil