From 505dbefb67f9a5f174356c77afd01ef767563181 Mon Sep 17 00:00:00 2001 From: David Allen Date: Sat, 20 Sep 2025 15:27:20 -0600 Subject: [PATCH] refactor: minor changes and fixes --- internal/format/format.go | 7 ++++--- internal/kwargs/kwargs.go | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/format/format.go b/internal/format/format.go index bdbfea0..11cfa56 100644 --- a/internal/format/format.go +++ b/internal/format/format.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "path/filepath" + "strings" "gopkg.in/yaml.v3" ) @@ -92,11 +93,11 @@ func Unmarshal(data []byte, v any, inFormat DataFormat) error { // both the standard default format (JSON) and any command line // change to that provided by options. func DataFormatFromFileExt(path string, defaultFmt DataFormat) DataFormat { - switch filepath.Ext(path) { - case ".json", ".JSON": + switch strings.TrimLeft(strings.ToLower(filepath.Ext(path)), ".") { + case JSON.String(): // The file is a JSON file return JSON - case ".yaml", ".yml", ".YAML", ".YML": + case YAML.String(), "yml": // The file is a YAML file return YAML } diff --git a/internal/kwargs/kwargs.go b/internal/kwargs/kwargs.go index 1da80bd..0c8911f 100644 --- a/internal/kwargs/kwargs.go +++ b/internal/kwargs/kwargs.go @@ -5,15 +5,28 @@ import ( "fmt" "git.towk2.me/towk/makeshift/internal/format" + "github.com/rs/zerolog/log" ) const RESERVED_KEY = "kwargs" type KWArgs map[string]any +func New() KWArgs { + return KWArgs{} +} + func (kw KWArgs) String() string { - b, _ := json.Marshal(kw) - return string(b) + return string(kw.Bytes()) +} + +func (kw KWArgs) Bytes() []byte { + b, err := json.Marshal(kw) + if err != nil { + log.Error().Err(err).Msg("failed to marshal kwargs") + return []byte("{}") + } + return b } func (kw *KWArgs) Set(v string /* should be JSON object*/) error {