start: add config system

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2025-12-13 00:14:18 +01:00
commit 115456258c
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
3 changed files with 57 additions and 10 deletions

View file

@ -5,6 +5,7 @@ import (
"os" "os"
"strings" "strings"
"git.g3e.fr/syonad/two/internal/config"
"git.g3e.fr/syonad/two/pkg/db/kv" "git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4" "github.com/dgraph-io/badger/v4"
) )
@ -12,8 +13,8 @@ import (
var DB *badger.DB var DB *badger.DB
func CheckInDB(dbName, id string) int { func CheckInDB(dbName, id string) int {
prefix := []byte(dbName + "/bash/") prefix := []byte(dbName + "/")
key := []byte(dbName + "/bash/" + id) key := []byte(dbName + "/" + id)
// vérifier si DB contient au moins une entrée avec ce préfixe // vérifier si DB contient au moins une entrée avec ce préfixe
hasPrefix := false hasPrefix := false
@ -47,8 +48,8 @@ func CheckInDB(dbName, id string) int {
func AddInDB(dbName string, line string) error { func AddInDB(dbName string, line string) error {
// ID = partie avant le premier ';' // ID = partie avant le premier ';'
id := strings.Split(line, ";")[0] id := strings.Split(line, ";")[0] + "/bash"
key := []byte(dbName + "/bash/" + id) key := []byte(dbName + "/" + id)
return DB.Update(func(txn *badger.Txn) error { return DB.Update(func(txn *badger.Txn) error {
return txn.Set(key, []byte(line)) return txn.Set(key, []byte(line))
@ -56,7 +57,7 @@ func AddInDB(dbName string, line string) error {
} }
func DeleteInDB(dbName, id 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 DB.Update(func(txn *badger.Txn) error {
return txn.Delete(key) return txn.Delete(key)
@ -64,7 +65,7 @@ func DeleteInDB(dbName, id string) error {
} }
func CountInDB(dbName, id string) int { func CountInDB(dbName, id string) int {
prefix := []byte(dbName + "/bash/" + id) prefix := []byte(dbName + "/" + id + "/bash")
count := 0 count := 0
DB.View(func(txn *badger.Txn) error { DB.View(func(txn *badger.Txn) error {
@ -81,7 +82,7 @@ func CountInDB(dbName, id string) int {
} }
func GetFromDB(dbName, id string) (string, error) { func GetFromDB(dbName, id string) (string, error) {
key := []byte(dbName + "/bash/" + id) key := []byte(dbName + "/" + id + "/bash")
var result string var result string
@ -124,11 +125,15 @@ func printDB() {
} }
func main() { func main() {
var conf kv.Config = kv.Config{ configuration, err := config.LoadConfig("./two.yaml")
Path: "./data/", if err != nil {
fmt.Println(err)
return
} }
DB = kv.InitDB(conf) DB = kv.InitDB(kv.Config{
Path: configuration.Database.Path,
})
defer DB.Close() defer DB.Close()
printDB() printDB()

12
go.mod
View file

@ -7,15 +7,27 @@ require (
github.com/dgraph-io/badger/v4 v4.8.0 // indirect github.com/dgraph-io/badger/v4 v4.8.0 // indirect
github.com/dgraph-io/ristretto/v2 v2.2.0 // indirect github.com/dgraph-io/ristretto/v2 v2.2.0 // indirect
github.com/dustin/go-humanize v1.0.1 // 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/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // 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/google/flatbuffers v25.2.10+incompatible // indirect
github.com/klauspost/compress v1.18.0 // 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/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel v1.37.0 // indirect go.opentelemetry.io/otel v1.37.0 // indirect
go.opentelemetry.io/otel/metric v1.37.0 // indirect go.opentelemetry.io/otel/metric v1.37.0 // indirect
go.opentelemetry.io/otel/trace 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/net v0.41.0 // indirect
golang.org/x/sys v0.34.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 google.golang.org/protobuf v1.36.6 // indirect
) )

30
internal/config/struct.go Normal file
View file

@ -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
}