Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: config unset reset to default config #264

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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