fix: always enable nix-command

Signed-off-by: jolheiser <git@jolheiser.com>
main
jolheiser 2024-10-17 15:03:55 -05:00
parent c1d5ec9d07
commit e902c130a5
No known key found for this signature in database
3 changed files with 2 additions and 125 deletions

View File

@ -1,53 +0,0 @@
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)
}
}

View File

@ -1,70 +0,0 @@
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)
}
}

View File

@ -46,7 +46,7 @@ func Unmarshal(data []byte, v any) error {
}
var stdout, stderr bytes.Buffer
cmd := exec.Command(Nix, "eval", "--json", "--expr", string(data))
cmd := exec.Command(Nix, "--extra-experimental-features", "nix-command", "eval", "--json", "--expr", string(data))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
@ -74,7 +74,7 @@ func Marshal(v any) ([]byte, error) {
}
var stdout, stderr bytes.Buffer
cmd := exec.Command(Nix, "eval", "--expr", fmt.Sprintf(`(builtins.fromJSON %q)`, string(data)))
cmd := exec.Command(Nix, "--extra-experimental-features", "nix-command", "eval", "--expr", fmt.Sprintf(`(builtins.fromJSON %q)`, string(data)))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {