Home

nixfig @main - refs - log -
-
https://git.jolheiser.com/nixfig.git
Nix as a Config
tree log patch
feat: nixgen Signed-off-by: jolheiser <john.olheiser@gmail.com>
Signature
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEgqEQpE3xoo1QwJO/uFOtpdp7v3oFAmYpoDkACgkQuFOtpdp7 v3ri2A//W/74cQlIg80809S4NJrcRcWlkTwePqrDUL7riZvY2VBW6KK6dPZ9Jqt3 ISChW+8bLV4vqkxDCaPne4XYhHm9QseEFEfxBFsonFx/3od0vSXIkauIXIybi0DZ f2uHvexpA7OurJEggZD7C/dnqPP3exYuA6Uo6RgBmoyF9NpkMLQQy7Z/b+wyRdvh DYAIzF3eIqOp2DnEMfX6NC4CXU0dvUULPblaW0pjv0D2qSIOjUTGAxnAYUU/DF8x CfIdY7YdFomWecp996A3oe6rRRQHpJPNn5eUB8pbeA43fw4ExDljuF9kVPfr8yXK kIA0qxDEtosQYws83SkjrXfEyVQtzcFE5QRjbQHBXSyilEYJJEc1DDKERk+RxCSx 409k54vqrOg0QeRslkrzjtr1YKiYBSdnLOyyPnflyYEpBhBa00sk9RpSt7ChNukS fRCwLrUf/tNdxox/TIbX6eEFHdWxJNl8B+fhmVm3+tWnYsqv2RJt0Q5MzT2/LwLD 9rprCzew0L4rBMwv3TWmrfKN1OxXuXOjaXbTZYMRq0nB2+7ODlhNz2FcrppYpoyH V/dRGqbCDKnNyJmMYhXDFyDlK2kEfd8LZC/v49Q4LzqgIM9jWxNw+MBChF7gFJgR EtdJyPHjs68Jhef7pLE45up01D3j2RgKNuxeOY4Q2nBEaOGyq74= =nlNn -----END PGP SIGNATURE-----
jolheiser <john.olheiser@gmail.com>
10 months ago
5 changed files, 100 additions(+), 2 deletions(-)
Icmd/nixgen/main.go
diff --git a/cmd/nixgen/main.go b/cmd/nixgen/main.go
new file mode 100644
index 0000000000000000000000000000000000000000..907f1720ee6f38301053c0abdba89e73063705ab
--- /dev/null
+++ b/cmd/nixgen/main.go
@@ -0,0 +1,70 @@
+package main
+
+import (
+	"encoding/json"
+	"errors"
+	"flag"
+	"fmt"
+	"os"
+	"strings"
+
+	"github.com/pelletier/go-toml/v2"
+	"go.jolheiser.com/nixfig"
+	"gopkg.in/yaml.v3"
+)
+
+func maine() error {
+	fs := flag.NewFlagSet("nixgen", flag.ExitOnError)
+
+	format := func(v any) ([]byte, error) {
+		return json.MarshalIndent(v, "", "\t")
+	}
+	formatFunc := func(s string) error {
+		switch strings.ToLower(s) {
+		case "json":
+			// Default
+		case "yaml":
+			format = yaml.Marshal
+		case "toml":
+			format = toml.Marshal
+		default:
+			return fmt.Errorf("unknown format %q, should be one of [json yaml toml]", s)
+		}
+		return nil
+	}
+	fs.Func("format", "Output format", formatFunc)
+	fs.Func("f", "--format", formatFunc)
+	if err := fs.Parse(os.Args[1:]); err != nil {
+		return err
+	}
+
+	if len(fs.Args()) < 1 {
+		return errors.New("nixgen requires a file")
+	}
+
+	fp := fs.Arg(0)
+	data, err := os.ReadFile(fp)
+	if err != nil {
+		return err
+	}
+
+	var nix map[string]any
+	if err := nixfig.Unmarshal(data, &nix); err != nil {
+		return err
+	}
+
+	out, err := format(nix)
+	if err != nil {
+		return err
+	}
+	fmt.Println(string(out))
+
+	return nil
+}
+
+func main() {
+	if err := maine(); err != nil {
+		fmt.Println(err)
+		os.Exit(1)
+	}
+}
M go.modgo.mod
diff --git a/go.mod b/go.mod
index 320eb929bd3d8f921051c72e3d2a394f84ffe14b..556b398e1d418bd11616e77532e066b51b4c0018 100644
--- a/go.mod
+++ b/go.mod
@@ -2,7 +2,11 @@ module go.jolheiser.com/nixfig
 
 go 1.21.4
 
-require github.com/alecthomas/assert/v2 v2.4.0
+require (
+	github.com/alecthomas/assert/v2 v2.4.0
+	github.com/pelletier/go-toml/v2 v2.2.1
+	gopkg.in/yaml.v3 v3.0.1
+)
 
 require (
 	github.com/alecthomas/repr v0.3.0 // indirect
M go.sumgo.sum
diff --git a/go.sum b/go.sum
index bb218edf75f7e5851f885dad9c29af54e00b24a4..0c882c31c9dcfde941c3ed0ab83cee22123fb59e 100644
--- a/go.sum
+++ b/go.sum
@@ -2,5 +2,26 @@ github.com/alecthomas/assert/v2 v2.4.0 h1:/ZiZ0NnriAWPYYO+4eOjgzNELrFQLaHNr92mHSHFj9U=
 github.com/alecthomas/assert/v2 v2.4.0/go.mod h1:fw5suVxB+wfYJ3291t0hRTqtGzFYdSwstnRQdaQx2DM=
 github.com/alecthomas/repr v0.3.0 h1:NeYzUPfjjlqHY4KtzgKJiWd6sVq2eNUPTi34PiFGjY8=
 github.com/alecthomas/repr v0.3.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
 github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
+github.com/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg=
+github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
+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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
+github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
+github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
M nixfig_test.gonixfig_test.go
diff --git a/nixfig_test.go b/nixfig_test.go
index 517070e2cc31cc7dedb3a30385e90c859f8d830d..1d70aee17e7838ca41ce74183489c9ae304f7983 100644
--- a/nixfig_test.go
+++ b/nixfig_test.go
@@ -8,7 +8,8 @@ 	"github.com/alecthomas/assert/v2"
 )
 
 type Config struct {
-	Log struct {
+	User string
+	Log  struct {
 		Level string
 		File  string
 	}
@@ -20,6 +21,7 @@ 	}
 }
 
 var testCfg = Config{
+	User: "jolheiser",
 	Log: struct {
 		Level string
 		File  string
M testdata/config.nixtestdata/config.nix
diff --git a/testdata/config.nix b/testdata/config.nix
index 2d38d2b0ab1e6123cc206ee20ffb72b739676809..535f62e6b10bc8eb8e75e475da1501bedc8ef653 100644
--- a/testdata/config.nix
+++ b/testdata/config.nix
@@ -1,6 +1,7 @@
 let
   user = "jolheiser";
 in {
+  inherit user;
   log = {
     level = "warn";
     # Name the log file after the user....for reasons