Skip to content

Commit

Permalink
refactor: config unset reset to default config (#264)
Browse files Browse the repository at this point in the history
* refactor: `config unset` reset to default config

* unsupported types are not handled
  • Loading branch information
Chance-fyi authored May 13, 2024
1 parent 3614e26 commit 535ec44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
33 changes: 29 additions & 4 deletions cmd/commands/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"errors"
"fmt"
"github.com/version-fox/vfox/internal/config"
"reflect"
Expand Down Expand Up @@ -52,9 +53,10 @@ func configCmd(ctx *cli.Context) error {
return nil
}

value := args.Get(1)
var value any
value = args.Get(1)
if unset {
value = ""
value = defaultConfig(keys)
}
err := configSet(conf, keys, value)
if err != nil {
Expand Down Expand Up @@ -102,7 +104,7 @@ func configGet(v reflect.Value, keys []string) {
}
}

func configSet(v reflect.Value, keys []string, value string) error {
func configSet(v reflect.Value, keys []string, value any) error {
key := keys[0]
if v.Kind() == reflect.Ptr {
v = v.Elem()
Expand All @@ -117,9 +119,11 @@ func configSet(v reflect.Value, keys []string, value string) error {
} else {
switch v.Field(i).Kind() {
case reflect.Bool:
value := fmt.Sprintf("%v", value)
parseBool, _ := strconv.ParseBool(value)
v.Field(i).SetBool(parseBool)
case reflect.Int64:
value := fmt.Sprintf("%v", value)
if v.Field(i).Type() == reflect.TypeOf(config.CacheDuration(0)) {
if value == "-1" {
v.Field(i).SetInt(-1)
Expand All @@ -137,12 +141,33 @@ func configSet(v reflect.Value, keys []string, value string) error {
}
v.Field(i).SetInt(int64(atoi))
}
case reflect.Ptr:
if _, ok := value.(string); ok {
return fmt.Errorf("key does not contain a section: %v", key)
}
v.Field(i).Set(reflect.ValueOf(value))
default:
v.Field(i).SetString(value)
return errors.New("unsupported configuration type")
}
}
break
}
}
return nil
}

func defaultConfig(keys []string) any {
v := reflect.ValueOf(config.DefaultConfig)
for _, key := range keys {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for i := 0; i < v.NumField(); i++ {
if v.Type().Field(i).Tag.Get("yaml") == key {
v = v.Field(i)
break
}
}
}
return v.Interface()
}
6 changes: 3 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Config struct {
const filename = "config.yaml"

var (
defaultConfig = &Config{
DefaultConfig = &Config{
Proxy: EmptyProxy,
Storage: EmptyStorage,
Registry: EmptyRegistry,
Expand All @@ -45,10 +45,10 @@ var (

func NewConfigWithPath(p string) (*Config, error) {
if !util.FileExists(p) {
content, err := yaml.Marshal(defaultConfig)
content, err := yaml.Marshal(DefaultConfig)
if err == nil {
_ = os.WriteFile(p, content, 0666)
return defaultConfig, nil
return DefaultConfig, nil
}
}
_ = util.ChangeModeIfNot(p, 0666)
Expand Down

0 comments on commit 535ec44

Please sign in to comment.