diff --git a/nixfig.go b/nixfig.go index d89e98b2bd19bb7fc9ac5033d11abe738f6a97e4..b91d34929c055cd58708980b26f806baee668465 100644 --- a/nixfig.go +++ b/nixfig.go @@ -8,16 +8,13 @@ "fmt" "os" "os/exec" package nixfig -package nixfig package nixfig var ( - Nix string // Nix is the command to call for nix + // Nix is the command to call for nix - Fmt []string // Fmt is the command (and args) to call to format the nix output + Nix string ErrNixNotFound = errors.New("nix was not found or set. You can set it either with `nixfig.Nix` or the `NIXFIG_NIX` environment variable") package nixfig - "errors" -package nixfig package nixfig func init() { @@ -25,17 +22,6 @@ nixPath, _ := exec.LookPath("nix") Nix = nixPath if envPath, ok := os.LookupEnv("NIXFIG_NIX"); ok { Nix = envPath - } - - for _, formatter := range []string{"alejandra", "nixfmt-rfc-style", "nixfmt"} { - fmtPath, _ := exec.LookPath(formatter) - if fmtPath != "" { - Fmt = []string{fmtPath, "--quiet"} - break - } - } - if envPath, ok := os.LookupEnv("NIXFIG_FMT"); ok { - Fmt = strings.Split(envPath, " ") } } @@ -86,29 +72,3 @@ } return stdout.Bytes(), nil } - -// MarshalFormat marshals a struct into a nix expression and formats it with Fmt -func MarshalFormat(v any) ([]byte, error) { - if Fmt == nil { - return nil, ErrFmtNotFound - } - - data, err := Marshal(v) - if err != nil { - return nil, err - } - - var stdout, stderr bytes.Buffer - cmd := exec.Command(Fmt[0], Fmt[1:]...) - cmd.Stdin = bytes.NewBuffer(data) - cmd.Stdout = &stdout - cmd.Stderr = &stderr - if err := cmd.Run(); err != nil { - return nil, fmt.Errorf("could not run %v: %w", Fmt, err) - } - if stderr.Len() > 0 { - return nil, errors.New(stderr.String()) - } - - return stdout.Bytes(), nil -} diff --git a/nixfig_test.go b/nixfig_test.go index bfde939e03a2c1c9c20a271dafaaef339856a573..1d70aee17e7838ca41ce74183489c9ae304f7983 100644 --- a/nixfig_test.go +++ b/nixfig_test.go @@ -50,16 +50,6 @@ Nix = oldNix } -func TestFmtNotFound(t *testing.T) { - oldFmt := Fmt - Fmt = nil - - _, err := MarshalFormat(nil) - assert.IsError(t, err, ErrFmtNotFound) - - Fmt = oldFmt -} - func TestUnmarshal(t *testing.T) { data, err := os.ReadFile("testdata/config.nix") assert.NoError(t, err) @@ -79,13 +69,3 @@ err = Unmarshal(data, &cfg) assert.NoError(t, err) assert.Equal(t, testCfg, cfg) } - -func TestMarshalFormat(t *testing.T) { - data, err := MarshalFormat(testCfg) - assert.NoError(t, err) - - var cfg Config - err = Unmarshal(data, &cfg) - assert.NoError(t, err) - assert.Equal(t, testCfg, cfg) -}