diff --git a/cmd/nixfig/main.go b/cmd/nixfig/main.go new file mode 100644 index 0000000000000000000000000000000000000000..f36d962fbc2f0401e42ae3fa6b7fe6fa1dda0f30 --- /dev/null +++ b/cmd/nixfig/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + + "go.jolheiser.com/nixfig" +) + +func maine() error { + if len(os.Args) != 2 { + return errors.New("nixfig requires 1 argument") + } + fn := os.Args[1] + + data, err := os.ReadFile(fn) + if err != nil { + return fmt.Errorf("could not read file %q: %w", fn, err) + } + + var out []byte + var a any + switch ext := filepath.Ext(fn); ext { + case ".json": + if err := json.Unmarshal(data, &a); err != nil { + return fmt.Errorf("invalid JSON file: %w", err) + } + out, err = nixfig.Marshal(a) + case ".nix": + if err := nixfig.Unmarshal(data, &a); err != nil { + return fmt.Errorf("invalid nix file: %w", err) + } + out, err = json.Marshal(a) + default: + return fmt.Errorf("unknown extension %q, must be json or nix", ext) + } + if err != nil { + return err + } + + fmt.Print(string(out)) + return nil +} + +func main() { + if err := maine(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/cmd/nixgen/main.go b/cmd/nixgen/main.go new file mode 100644 index 0000000000000000000000000000000000000000..c76bc456d59f37bb2d34dfc0733644899d85ec50 --- /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 [json, yaml, toml]: default 'json'", 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/nixfig.go b/nixfig.go index 91a746eb6b67841fae8929b12438850e2f93622d..e1597bcf2f5021c3365345d9329920f92503260c 100644 --- a/nixfig.go +++ b/nixfig.go @@ -46,7 +46,7 @@ return ErrNixNotFound } var stdout, stderr bytes.Buffer - cmd := exec.Command(Nix, "--extra-experimental-features", "nix-command", "eval", "--json", "--expr", string(data)) + cmd := exec.Command(Nix, "eval", "--json", "--expr", string(data)) cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil { @@ -74,7 +74,7 @@ return nil, fmt.Errorf("could not marshal JSON: %w", err) } var stdout, stderr bytes.Buffer - cmd := exec.Command(Nix, "--extra-experimental-features", "nix-command", "eval", "--expr", fmt.Sprintf(`(builtins.fromJSON %q)`, string(data))) + cmd := exec.Command(Nix, "eval", "--expr", fmt.Sprintf(`(builtins.fromJSON %q)`, string(data))) cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil {