From 885aa7e0f632204a140a721bd026869a4eade8c5 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Wed, 24 Apr 2024 19:13:45 -0500 Subject: [PATCH] feat: nixgen Signed-off-by: jolheiser --- cmd/nixgen/main.go | 70 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 6 +++- go.sum | 21 ++++++++++++++ nixfig_test.go | 4 ++- testdata/config.nix | 1 + 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 cmd/nixgen/main.go diff --git a/cmd/nixgen/main.go b/cmd/nixgen/main.go new file mode 100644 index 0000000..907f172 --- /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) + } +} diff --git a/go.mod b/go.mod index 320eb92..556b398 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 diff --git a/go.sum b/go.sum index bb218ed..0c882c3 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,26 @@ github.com/alecthomas/assert/v2 v2.4.0 h1:/ZiZ0NnriAWPYYO+4eOjgzNELrFQLaHNr92mHS 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= diff --git a/nixfig_test.go b/nixfig_test.go index 517070e..1d70aee 100644 --- a/nixfig_test.go +++ b/nixfig_test.go @@ -8,7 +8,8 @@ import ( ) type Config struct { - Log struct { + User string + Log struct { Level string File string } @@ -20,6 +21,7 @@ type Config struct { } var testCfg = Config{ + User: "jolheiser", Log: struct { Level string File string diff --git a/testdata/config.nix b/testdata/config.nix index 2d38d2b..535f62e 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