From aea8c2f77a5f9dcce716957b06c6b922aca3ec20 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sat, 13 Dec 2025 00:14:18 +0100 Subject: [PATCH] add config system Signed-off-by: GnomeZworc --- cmd/db/main.go | 25 +++++++++++++++---------- go.mod | 12 ++++++++++++ internal/config/struct.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 internal/config/struct.go diff --git a/cmd/db/main.go b/cmd/db/main.go index 6dbbca4..e17dd6f 100644 --- a/cmd/db/main.go +++ b/cmd/db/main.go @@ -5,6 +5,7 @@ import ( "os" "strings" + "git.g3e.fr/syonad/two/internal/config" "git.g3e.fr/syonad/two/pkg/db/kv" "github.com/dgraph-io/badger/v4" ) @@ -12,8 +13,8 @@ import ( var DB *badger.DB func CheckInDB(dbName, id string) int { - prefix := []byte(dbName + "/bash/") - key := []byte(dbName + "/bash/" + id) + prefix := []byte(dbName + "/") + key := []byte(dbName + "/" + id) // vérifier si DB contient au moins une entrée avec ce préfixe hasPrefix := false @@ -47,8 +48,8 @@ func CheckInDB(dbName, id string) int { func AddInDB(dbName string, line string) error { // ID = partie avant le premier ';' - id := strings.Split(line, ";")[0] - key := []byte(dbName + "/bash/" + id) + id := strings.Split(line, ";")[0] + "/bash" + key := []byte(dbName + "/" + id) return DB.Update(func(txn *badger.Txn) error { return txn.Set(key, []byte(line)) @@ -56,7 +57,7 @@ func AddInDB(dbName string, line string) error { } func DeleteInDB(dbName, id string) error { - key := []byte(dbName + "/bash/" + id) + key := []byte(dbName + "/" + id + "/bash") return DB.Update(func(txn *badger.Txn) error { return txn.Delete(key) @@ -64,7 +65,7 @@ func DeleteInDB(dbName, id string) error { } func CountInDB(dbName, id string) int { - prefix := []byte(dbName + "/bash/" + id) + prefix := []byte(dbName + "/" + id + "/bash") count := 0 DB.View(func(txn *badger.Txn) error { @@ -81,7 +82,7 @@ func CountInDB(dbName, id string) int { } func GetFromDB(dbName, id string) (string, error) { - key := []byte(dbName + "/bash/" + id) + key := []byte(dbName + "/" + id + "/bash") var result string @@ -124,11 +125,15 @@ func printDB() { } func main() { - var conf kv.Config = kv.Config{ - Path: "./data/", + configuration, err := config.LoadConfig("./two.yaml") + if err != nil { + fmt.Println(err) + return } - DB = kv.InitDB(conf) + DB = kv.InitDB(kv.Config{ + Path: configuration.Database.Path, + }) defer DB.Close() printDB() diff --git a/go.mod b/go.mod index e348de1..cbc34b5 100644 --- a/go.mod +++ b/go.mod @@ -7,15 +7,27 @@ require ( github.com/dgraph-io/badger/v4 v4.8.0 // indirect github.com/dgraph-io/ristretto/v2 v2.2.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect + github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-viper/mapstructure/v2 v2.4.0 // indirect github.com/google/flatbuffers v25.2.10+incompatible // indirect github.com/klauspost/compress v1.18.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/sagikazarmark/locafero v0.11.0 // indirect + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect + github.com/spf13/afero v1.15.0 // indirect + github.com/spf13/cast v1.10.0 // indirect + github.com/spf13/pflag v1.0.10 // indirect + github.com/spf13/viper v1.21.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect go.opentelemetry.io/otel v1.37.0 // indirect go.opentelemetry.io/otel/metric v1.37.0 // indirect go.opentelemetry.io/otel/trace v1.37.0 // indirect + go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/net v0.41.0 // indirect golang.org/x/sys v0.34.0 // indirect + golang.org/x/text v0.28.0 // indirect google.golang.org/protobuf v1.36.6 // indirect ) diff --git a/internal/config/struct.go b/internal/config/struct.go new file mode 100644 index 0000000..a3708fb --- /dev/null +++ b/internal/config/struct.go @@ -0,0 +1,30 @@ +package config + +import ( + "github.com/spf13/viper" +) + +type Config struct { + Database struct { + Path string `mapstructure:"path"` + } `mapstructure:"database"` +} + +func LoadConfig(path string) (*Config, error) { + v := viper.New() + v.SetConfigFile(path) + v.SetConfigType("yaml") + + v.SetDefault("database.path", "./data/") + + if err := v.ReadInConfig(); err != nil { + return nil, err + } + + var cfg Config + if err := v.Unmarshal(&cfg); err != nil { + return nil, err + } + + return &cfg, nil +}