This commit is contained in:
2025-10-25 20:03:36 +02:00
parent 381a4e4988
commit 6224e63fa9
5 changed files with 154 additions and 1 deletions

52
cmd/cli/config/config.go Normal file
View File

@@ -0,0 +1,52 @@
package config
import (
"encoding/json"
"errors"
"os"
"path/filepath"
)
type (
ClientConfiguration struct {
Deamon ClientDaemonConfiguration `json:"daemon"`
}
ClientDaemonConfiguration struct {
URL string `json:"url"`
}
)
func Load() ClientConfiguration {
userConfigDir, err := os.UserConfigDir()
if err != nil {
panic("failed to get user config path: " + err.Error())
}
path := filepath.Join(userConfigDir, "mirror-sync", "config.json")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return Default()
}
panic("failed to load configuration file (" + path + "): " + err.Error())
}
defer f.Close()
var c ClientConfiguration
d := json.NewDecoder(f)
if err := d.Decode(&c); err != nil {
panic("failed to read the configuration file: " + err.Error())
}
return c
}
func Default() ClientConfiguration {
return ClientConfiguration{
Deamon: ClientDaemonConfiguration{
URL: "http://localhost:8080",
},
}
}