Skip to content

Commit

Permalink
Merge pull request #1410 from DiceDB/arpit-config-v2
Browse files Browse the repository at this point in the history
Configuration v2 - Simple, Readable, and Consistent
  • Loading branch information
arpitbbhayani authored Jan 22, 2025
2 parents 0618912 + ec0b852 commit bb3ad91
Show file tree
Hide file tree
Showing 48 changed files with 641 additions and 1,988 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ pnpm-debug.log*

# macOS-specific files
.DS_Store
dicedb.yaml
dicedb.conf
26 changes: 26 additions & 0 deletions cmd/init_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022-present, DiceDB contributors
// All rights reserved. Licensed under the BSD 3-Clause License. See LICENSE file in the project root for full license information.

package cmd

import (
"fmt"

"github.com/dicedb/dice/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var initConfigCmd = &cobra.Command{
Use: "init-config",
Short: "creates a config file at dicedb.yaml with default values",
Run: func(cmd *cobra.Command, args []string) {
config.Init(cmd.Flags())
_ = viper.WriteConfigAs("dicedb.yaml")
fmt.Println("config created at dicedb.yaml")
},
}

func init() {
rootCmd.AddCommand(initConfigCmd)
}
54 changes: 54 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2022-present, DiceDB contributors
// All rights reserved. Licensed under the BSD 3-Clause License. See LICENSE file in the project root for full license information.

package cmd

import (
"fmt"
"os"
"reflect"
"strconv"

"github.com/dicedb/dice/config"
"github.com/dicedb/dice/server"
"github.com/spf13/cobra"
)

func init() {
c := config.DiceDBConfig{}
_type := reflect.TypeOf(c)
for i := 0; i < _type.NumField(); i++ {
field := _type.Field(i)
yamlTag := field.Tag.Get("mapstructure")
descriptionTag := field.Tag.Get("description")
defaultTag := field.Tag.Get("default")

switch field.Type.Kind() {
case reflect.String:
rootCmd.PersistentFlags().String(yamlTag, defaultTag, descriptionTag)
case reflect.Int:
val, _ := strconv.Atoi(defaultTag)
rootCmd.PersistentFlags().Int(yamlTag, val, descriptionTag)
case reflect.Bool:
val, _ := strconv.ParseBool(defaultTag)
rootCmd.PersistentFlags().Bool(yamlTag, val, descriptionTag)
}
}
}

var rootCmd = &cobra.Command{
Use: "dicedb",
Short: "an in-memory database;",
Run: func(cmd *cobra.Command, args []string) {
config.Init(cmd.Flags())
server.Init()
server.Start()
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Loading

0 comments on commit bb3ad91

Please sign in to comment.