cfg @main -
refs -
log -
-
https://git.jolheiser.com/cfg.git
Convert between various configuration formats
feat: kdl
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Signature
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEEgqEQpE3xoo1QwJO/uFOtpdp7v3oFAmZjoGcACgkQuFOtpdp7
v3pi4w/+K5WIBphd6a68mjHKrkMdggmyOrQHOzfR5wRYBmWoqj23pEcaFAh+/gQo
15wxtVI66SklzYx0JO6BZ6UrGWvD2XGkS5QPnSez4QoEVxfIOfW+qZhSyBRn2FR9
k8Z1/Ggd5lnPRqGqZJm3X7W139TpW+JRi0CKDjpTP4umZO52ZTQHMi31r9nu4Lwo
YRHMsbnK9/gShVjCGccoEu5cIfqeKZerTQ7x6jE45IWsdAduI0igxgJguMebIIV7
Ot/PE4vgXaD+FzgwuxJceBCNuSMn1Xahj9ti62tztAd9F3wTvjwZCy47XzjH2/Mb
ThSO51MSz+53zzVSiw4mAKPc7nYn2iCf149uaaSfKomgL4hJ+xHMW5MUVOB+B9iv
zu4RL1KjkR935WMw491LWtXa5HiDWLjzQiQ7u1wq51rENQe3rHFXBHQ3dP19K0Ng
4+upzekUhsxy6VRq3zitqNdKSNFlUZVnwdcFHvYSgRqG0RgbM4PMLxCyi2lhi7Uo
gtXXsqDHvD3uNMYV4RxIoI5Ex6dqw/syWiuLaOok1e/FOYG+5rwHNKfQOLGKf/t0
H86Vf59nqeUPh254QoHcMdrjHaFotg9F23/SacGEp4vjhH+/eb2fGvhAabnmGfV3
d+MWIv/WTVuAA14Q97HEdqjgv5QPejbk8hMywBqdeiruDA2igc8=
=5hx0
-----END PGP SIGNATURE-----
6 changed files, 18 additions(+), 3 deletions(-)
diff --git a/cfg.go b/cfg.go
index c3d9612364a3f0ce2be9fc02ea1967631c588bf8..f71f6734d6d99bd1eae4dccd949af7701dbe90fe 100644
--- a/cfg.go
+++ b/cfg.go
@@ -7,6 +7,7 @@ "strings"
"github.com/pelletier/go-toml/v2"
"github.com/philandstuff/dhall-golang/v6"
+ "github.com/sblinch/kdl-go"
"github.com/tailscale/hujson"
"go.jolheiser.com/nixfig"
"gopkg.in/yaml.v3"
@@ -22,6 +23,7 @@ YAML Encoding = "yaml"
TOML Encoding = "toml"
NIX Encoding = "nix"
DHALL Encoding = "dhall"
+ KDL Encoding = "kdl"
)
// Marshal takes data and encodes for an [Encoding]
@@ -49,6 +51,8 @@ case "nix":
return NIX, nil
case "dhall":
return DHALL, nil
+ case "kdl":
+ return KDL, nil
default:
return "", fmt.Errorf("unknown encoding %q", s)
}
@@ -65,6 +69,8 @@ case TOML:
return toml.Marshal(v)
case NIX:
return nixfig.Marshal(v)
+ case KDL:
+ return kdl.Marshal(v)
default:
return nil, fmt.Errorf("unknown marshal format %q", e)
}
@@ -87,6 +93,8 @@ case NIX:
return nixfig.Unmarshal(data, v)
case DHALL:
return dhall.Unmarshal(data, v)
+ case KDL:
+ return kdl.Unmarshal(data, v)
default:
return fmt.Errorf("unknown unmarshal format %q", e)
diff --git a/cfg_test.go b/cfg_test.go
index 79323fa243c124d51255ec5a9e2afcbcb8909460..c6b055fc1a137ccc1ffec6e0bfff5861bb0354a7 100644
--- a/cfg_test.go
+++ b/cfg_test.go
@@ -47,7 +47,7 @@ Gonk: false,
},
}
- encoders := []Encoding{JSON, JSONC, YAML, TOML}
+ encoders := []Encoding{JSON, JSONC, YAML, TOML, KDL}
// Only test nix if it's available
if nixfig.Nix != "" {
encoders = append(encoders, NIX)
diff --git a/cmd/cfg/main.go b/cmd/cfg/main.go
index 78047fc41c269ec9ef3407fa990bd3329fac3230..cf92c9bfb100c122f3e4c6c210a1874825293a05 100644
--- a/cmd/cfg/main.go
+++ b/cmd/cfg/main.go
@@ -24,7 +24,7 @@ }
unmarshal = e.Unmarshal
return nil
}
- fs.Func("from", "The format to convert from [json(c), yaml, toml, nix, dhall]", fromFunc)
+ fs.Func("from", "The format to convert from [json(c), yaml, toml, nix, dhall, kdl]", fromFunc)
fs.Func("f", "--from", fromFunc)
toFunc := func(s string) error {
e, err := cfg.ParseEncoding(s)
@@ -34,7 +34,7 @@ }
marshal = e.Marshal
return nil
}
- fs.Func("to", "The format to convert to [json, yaml, toml, nix]", toFunc)
+ fs.Func("to", "The format to convert to [json, yaml, toml, nix, kdl]", toFunc)
fs.Func("t", "--to", toFunc)
if err := fs.Parse(os.Args[1:]); err != nil {
return err
diff --git a/example/example.kdl b/example/example.kdl
new file mode 100644
index 0000000000000000000000000000000000000000..361e4b3ae542c1edb23a648be8c098dbc62ef1d3
--- /dev/null
+++ b/example/example.kdl
@@ -0,0 +1,4 @@
+admins "jolheiser" "admin"
+email "jolheiser@example.com"
+username "jolheiser"
+home "/home/jolheiser"
diff --git a/go.mod b/go.mod
index bedd44dffecde29e3d29bc7c296876164c05b762..a75721cc3e2402c3cb1f7882b73cc1ce2adaddc8 100644
--- a/go.mod
+++ b/go.mod
@@ -6,6 +6,7 @@ require (
github.com/matryer/is v1.4.1
github.com/pelletier/go-toml/v2 v2.2.2
github.com/philandstuff/dhall-golang/v6 v6.0.2
+ github.com/sblinch/kdl-go v0.0.0-20240410000746-21754ba9ac55
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
go.jolheiser.com/nixfig v0.0.0-20231129190849-8b2170b2ecda
gopkg.in/yaml.v3 v3.0.1
diff --git a/go.sum b/go.sum
index 2a702be2992b154f56f6667931a8690ef5c6c842..357afc93eec6b90d38fd402fa3a7003e372287b4 100644
--- a/go.sum
+++ b/go.sum
@@ -41,6 +41,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/sblinch/kdl-go v0.0.0-20240410000746-21754ba9ac55 h1:scyq0E9FvdGLX5lxAwjK0HebTM3Y7dG3tYrlXP+x+tk=
+github.com/sblinch/kdl-go v0.0.0-20240410000746-21754ba9ac55/go.mod h1:b3oNGuAKOQzhsCKmuLc/urEOPzgHj6fB8vl8bwTBh28=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=