2023-07-06 22:21:11 +00:00
|
|
|
package rcfile
|
2023-06-01 02:16:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-07-06 22:21:11 +00:00
|
|
|
func GetPath() string {
|
2023-06-01 02:16:58 +00:00
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(homeDir, ".comcordrc")
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:21:11 +00:00
|
|
|
func Load() map[string]string {
|
2023-06-01 02:16:58 +00:00
|
|
|
config := make(map[string]string)
|
2023-07-06 22:21:11 +00:00
|
|
|
file, err := os.ReadFile(GetPath())
|
2023-06-01 02:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(file), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
kvs := strings.Split(line, "=")
|
|
|
|
if len(kvs) == 2 {
|
|
|
|
config[kvs[0]] = kvs[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:21:11 +00:00
|
|
|
func Save(config map[string]string) {
|
2023-06-01 02:16:58 +00:00
|
|
|
out := ""
|
|
|
|
|
|
|
|
for key, value := range config {
|
|
|
|
out = out + key + "=" + value + "\n"
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:21:11 +00:00
|
|
|
err := os.WriteFile(GetPath(), []byte(out), 0644)
|
2023-06-01 02:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|