refactor: minor changes and fixes

This commit is contained in:
David Allen 2025-09-20 15:27:20 -06:00
parent 47c9d48735
commit 505dbefb67
Signed by: towk
GPG key ID: 0430CDBE22619155
2 changed files with 19 additions and 5 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings"
"gopkg.in/yaml.v3" "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 // both the standard default format (JSON) and any command line
// change to that provided by options. // change to that provided by options.
func DataFormatFromFileExt(path string, defaultFmt DataFormat) DataFormat { func DataFormatFromFileExt(path string, defaultFmt DataFormat) DataFormat {
switch filepath.Ext(path) { switch strings.TrimLeft(strings.ToLower(filepath.Ext(path)), ".") {
case ".json", ".JSON": case JSON.String():
// The file is a JSON file // The file is a JSON file
return JSON return JSON
case ".yaml", ".yml", ".YAML", ".YML": case YAML.String(), "yml":
// The file is a YAML file // The file is a YAML file
return YAML return YAML
} }

View file

@ -5,15 +5,28 @@ import (
"fmt" "fmt"
"git.towk2.me/towk/makeshift/internal/format" "git.towk2.me/towk/makeshift/internal/format"
"github.com/rs/zerolog/log"
) )
const RESERVED_KEY = "kwargs" const RESERVED_KEY = "kwargs"
type KWArgs map[string]any type KWArgs map[string]any
func New() KWArgs {
return KWArgs{}
}
func (kw KWArgs) String() string { func (kw KWArgs) String() string {
b, _ := json.Marshal(kw) return string(kw.Bytes())
return string(b) }
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 { func (kw *KWArgs) Set(v string /* should be JSON object*/) error {