Skip to content

Commit 383afce

Browse files
committed
helpers/runJSON: unmarshal JSON
1 parent e365220 commit 383afce

2 files changed

Lines changed: 18 additions & 21 deletions

File tree

src/helpers.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ func loadDeployments(cfg string) (map[string]JobSpec, error) {
2929
flakeReference := fmt.Sprintf("%s#nynxDeployments", cfg)
3030

3131
// Nix -> JSON
32-
data, err := runJSON("nix", "eval", "--json", flakeReference)
32+
data, err := runJSON[map[string]JobSpec]("nix", "eval", "--json", flakeReference)
3333
if err != nil {
3434
return nil, fmt.Errorf("failed to run nix eval on %s: %w", cfg, err)
3535
}
36-
jobs := make(map[string]JobSpec)
37-
if err := json.Unmarshal(data, &jobs); err != nil {
38-
return nil, fmt.Errorf("invalid JSON in %s: %w", cfg, err)
39-
}
36+
jobs := data
4037

4138
// Perform validation and system inference
4239
for name, spec := range jobs {
@@ -84,16 +81,22 @@ func run(cmd string, args ...string) ([]byte, error) {
8481
return out, nil
8582
}
8683

87-
func runJSON(cmd string, args ...string) ([]byte, error) {
84+
func runJSON[T any](cmd string, args ...string) (T, error) {
8885
c := exec.Command(cmd, args...)
8986
out, err := c.Output() // only capture stdout
9087
if err != nil {
88+
var zero T
9189
if ee, ok := err.(*exec.ExitError); ok {
92-
return nil, fmt.Errorf("`%s %v` failed: %s", cmd, args, string(ee.Stderr))
90+
return zero, fmt.Errorf("`%s %v` failed: %s", cmd, args, string(ee.Stderr))
9391
}
94-
return nil, fmt.Errorf("`%s %v` failed: %v", cmd, args, err)
92+
return zero, fmt.Errorf("`%s %v` failed: %v", cmd, args, err)
9593
}
96-
return out, nil
94+
var result T
95+
if err := json.Unmarshal(out, &result); err != nil {
96+
var zero T
97+
return zero, fmt.Errorf("failed to unmarshal JSON: %w\nRaw: %s", err, string(out))
98+
}
99+
return result, nil
97100
}
98101

99102
func validateOperations(jobs map[string]JobSpec, op string) ([]string, error) {

src/nixbuild.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
65
)
76

@@ -11,24 +10,20 @@ type BuildResult struct {
1110
}
1211

1312
func buildClosure(spec JobSpec, drvPath string, builder string) (string, error) {
14-
var buildOut []byte
1513
var err error
14+
var results []BuildResult
1615

1716
// Build the closure locally or on the remote builder
1817
if builder != "localhost" {
19-
buildOut, err = runJSON("nix", "build", "--no-link", "--json", "--store", "ssh-ng://"+builder, drvPath+"^*")
18+
results, err = runJSON[[]BuildResult]("nix", "build", "--no-link", "--json", "--store", "ssh-ng://"+builder, drvPath+"^*")
2019
} else {
21-
buildOut, err = runJSON("nix", "build", "--no-link", "--json", drvPath+"^*")
20+
results, err = runJSON[[]BuildResult]("nix", "build", "--no-link", "--json", drvPath+"^*")
2221
}
2322
if err != nil {
2423
return "", fmt.Errorf("failed to build %s on %s: %w", spec.Output, builder, err)
2524
}
2625

2726
// Parse the output path
28-
var results []BuildResult
29-
if err := json.Unmarshal(buildOut, &results); err != nil {
30-
return "", fmt.Errorf("invalid build JSON for %s: %w\nRaw: %s", spec.Output, err, string(buildOut))
31-
}
3227
if len(results) == 0 {
3328
return "", fmt.Errorf("build result for %s was empty", spec.Output)
3429
}
@@ -83,14 +78,13 @@ func instantiateDrvPath(flake string, name string, builder string) (string, erro
8378
drvExpr := expr + ".drvPath"
8479

8580
// Evaluate the .drv path locally
86-
data, err := runJSON("nix", "eval", "--json", drvExpr)
81+
drvPath, err := runJSON[string]("nix", "eval", "--json", drvExpr)
8782
if err != nil {
8883
return "", fmt.Errorf("failed to evaluate drvPath for job '%s': %w", name, err)
8984
}
9085

91-
var drvPath string
92-
if err := json.Unmarshal(data, &drvPath); err != nil {
93-
return "", fmt.Errorf("failed to parse JSON for job '%s': %w", name, err)
86+
if err != nil {
87+
return "", fmt.Errorf("failed to evaluate drvPath for job '%s': %w", name, err)
9488
}
9589

9690
// Copy the .drv to the remote builder (if needed)

0 commit comments

Comments
 (0)