aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2024-05-12 21:15:29 +0000
committerterminaldweller <devi@terminaldweller.com>2024-05-12 21:15:29 +0000
commite7c76ab9a36dd0bf501305fe9cfa4bd6884e4eb2 (patch)
tree58c35271ac221af64f2ea1dbdb48636434bbfe82
parentfixes #1, fixes #2, this should take care of it (diff)
downloadmilla-e7c76ab9a36dd0bf501305fe9cfa4bd6884e4eb2.tar.gz
milla-e7c76ab9a36dd0bf501305fe9cfa4bd6884e4eb2.zip
added the set command which lets users change config params on the fly. probably a bad idea, might remove later on but for now its there.
-rw-r--r--main.go59
1 files changed, 54 insertions, 5 deletions
diff --git a/main.go b/main.go
index a3fd8e9..6d67de3 100644
--- a/main.go
+++ b/main.go
@@ -27,9 +27,12 @@ import (
)
var (
- errNotEnoughArgs = errors.New("not enough arguments")
- errUnknCmd = errors.New("unknown command")
- errUnknConfig = errors.New("unknown config name")
+ errNotEnoughArgs = errors.New("not enough arguments")
+ errUnknCmd = errors.New("unknown command")
+ errUnknConfig = errors.New("unknown config name")
+ errCantSet = errors.New("can't set field")
+ errWrongDataForField = errors.New("wrong data type for field")
+ errUnsupportedType = errors.New("unsupported type")
)
type TomlConfig struct {
@@ -126,12 +129,12 @@ func extractLast256ColorEscapeCode(str string) (string, error) {
r, err := regexp.Compile(pattern)
if err != nil {
- return "", fmt.Errorf("failed to compile regular expression: %v", err)
+ return "", fmt.Errorf("failed to compile regular expression: %w", err)
}
matches := r.FindAllStringSubmatch(str, -1)
if len(matches) == 0 {
- return "", nil // No 256-color escape codes found
+ return "", nil
}
lastMatch := matches[len(matches)-1]
@@ -174,6 +177,47 @@ func getHelpString() string {
return helpString
}
+func setFieldByName(v reflect.Value, field string, value string) error {
+ fieldValue := v.FieldByName(field)
+ if !fieldValue.IsValid() {
+ return errUnknConfig
+ }
+
+ if !fieldValue.CanSet() {
+ return errCantSet
+ }
+
+ switch fieldValue.Kind() {
+ case reflect.String:
+ fieldValue.SetString(value)
+ case reflect.Int:
+ intValue, err := strconv.Atoi(value)
+ if err != nil {
+ return errWrongDataForField
+ }
+
+ fieldValue.SetInt(int64(intValue))
+ case reflect.Float64:
+ floatValue, err := strconv.ParseFloat(value, 64)
+ if err != nil {
+ return errWrongDataForField
+ }
+
+ fieldValue.SetFloat(floatValue)
+ case reflect.Bool:
+ boolValue, err := strconv.ParseBool(value)
+ if err != nil {
+ return errWrongDataForField
+ }
+
+ fieldValue.SetBool(boolValue)
+ default:
+ return errUnsupportedType
+ }
+
+ return nil
+}
+
func runCommand(
client *girc.Client,
event girc.Event,
@@ -207,6 +251,11 @@ func runCommand(
break
}
+
+ err := setFieldByName(reflect.ValueOf(appConfig).Elem(), args[1], args[2])
+ if err != nil {
+ client.Cmd.Reply(event, err.Error())
+ }
case "get":
if len(args) < 2 { //nolint:gomnd
client.Cmd.Reply(event, errNotEnoughArgs.Error())