From 26d3d0faeb723373a7a90f725b2cf5c7d2419794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Wed, 3 Dec 2025 09:43:41 +0100 Subject: [PATCH] dir2config: support json output --- cmd/dkl-dir2config/main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/dkl-dir2config/main.go b/cmd/dkl-dir2config/main.go index aa9dd86..bfe13bf 100644 --- a/cmd/dkl-dir2config/main.go +++ b/cmd/dkl-dir2config/main.go @@ -1,10 +1,12 @@ package main import ( + "encoding/json" "flag" "io/fs" "log" "os" + "path" "path/filepath" "github.com/go-git/go-git/v5" @@ -127,12 +129,22 @@ func main() { defer out.Close() - out.Write([]byte("# dkl-dir2config " + Version + "\n")) + switch ext := path.Ext(*outPath); ext { + case ".yaml": + out.Write([]byte("# dkl-dir2config " + Version + "\n")) - if err = yaml.NewEncoder(out).Encode(dst); err != nil { - log.Fatal("failed to render output: ", err) + if err = yaml.NewEncoder(out).Encode(dst); err != nil { + log.Fatal("failed to render output: ", err) + } + + case ".json": + if err = json.NewEncoder(out).Encode(dst); err != nil { + log.Fatal("failed to render output: ", err) + } + + default: + log.Fatal("unknown output file extension: ", ext) } - } func cfgPath(subPath string) string { return filepath.Join(*dir, subPath) }