Skip to content

Commit

Permalink
Update internal/nix/nix.go
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Brewster <[email protected]>
  • Loading branch information
ryantm and cbrewster committed Oct 14, 2023
1 parent 00b83fd commit 07f7ad3
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions internal/nix/nix.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nix

import (
"bytes"
_ "embed"
"encoding/json"
"errors"
Expand Down Expand Up @@ -100,43 +99,34 @@ func RunNixEditorOps(ops []NixEditorOp) {
util.Die("nix-editor error: %s", err)
}

in := &bytes.Buffer{}
encoder := json.NewEncoder(in)
encoder := json.NewEncoder(stdin)
for _, op := range ops {
err := encoder.Encode(op)
if err != nil {
util.Die("unable to turn op into json: %v error: %s", op, err)
}
}
_, err = stdin.Write(in.Bytes())
err = stdin.Close()
if err != nil {
util.Die("unable to write to nix-editor")
}
stdin.Close()

go func() {
decoder := json.NewDecoder(stdout)
for {
var nixEditorStatus struct {
Status string
Data string
}
err := decoder.Decode(&nixEditorStatus)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
util.Die("unexpected nix-editor output: %s", err)
}
if nixEditorStatus.Status != "success" {
util.Die("nix-editor error: %s", nixEditorStatus.Data)

decoder := json.NewDecoder(stdout)
for {
var nixEditorStatus struct {
Status string
Data string
}
err := decoder.Decode(&nixEditorStatus)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
util.Die("unexpected nix-editor output: %s", err)
}
if nixEditorStatus.Status != "success" {
util.Die("nix-editor error: %s", nixEditorStatus.Data)
}
stdout.Close()
}()

err = cmd.Wait()
if err != nil {
util.Die("nix-editor error: %s", err)
}
stdout.Close()
}

0 comments on commit 07f7ad3

Please sign in to comment.