Skip to content

Commit b98cec7

Browse files
committed
Add input.json to inputs.json fallback when file is missing
1 parent 991e4ac commit b98cec7

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

internal/io/read.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67

78
"github.com/alexander-lindner/go-cff"
89
toolspec "github.com/hydrocode-de/tool-spec-go"
@@ -25,6 +26,18 @@ func ReadSpecFile(path string) (toolspec.SpecFile, error) {
2526
func ReadInputFile(path string) (toolspec.InputFile, error) {
2627
inputBuffer, err := os.ReadFile(path)
2728
if err != nil {
29+
// Backward-compatible fallback for tools that still mount /in/inputs.json.
30+
if os.IsNotExist(err) && filepath.Base(path) == "input.json" {
31+
fallbackPath := filepath.Join(filepath.Dir(path), "inputs.json")
32+
fallbackBuffer, fallbackErr := os.ReadFile(fallbackPath)
33+
if fallbackErr == nil {
34+
input, parseErr := toolspec.LoadInputs(fallbackBuffer)
35+
if parseErr != nil {
36+
return toolspec.InputFile{}, fmt.Errorf("failed to load input file: %w", parseErr)
37+
}
38+
return input, nil
39+
}
40+
}
2841
return toolspec.InputFile{}, fmt.Errorf("failed to read input file: %w", err)
2942
}
3043

internal/io/read_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
const testInputJSON = `{
10+
"demo": {
11+
"parameters": {},
12+
"data": {}
13+
}
14+
}`
15+
16+
func TestReadInputFileFallsBackToInputsJSON(t *testing.T) {
17+
dir := t.TempDir()
18+
fallbackPath := filepath.Join(dir, "inputs.json")
19+
20+
if err := os.WriteFile(fallbackPath, []byte(testInputJSON), 0o644); err != nil {
21+
t.Fatalf("failed to write fallback inputs.json: %v", err)
22+
}
23+
24+
_, err := ReadInputFile(filepath.Join(dir, "input.json"))
25+
if err != nil {
26+
t.Fatalf("expected fallback from input.json to inputs.json, got error: %v", err)
27+
}
28+
}
29+
30+
func TestReadInputFilePrefersInputJSONWhenPresent(t *testing.T) {
31+
dir := t.TempDir()
32+
inputPath := filepath.Join(dir, "input.json")
33+
fallbackPath := filepath.Join(dir, "inputs.json")
34+
35+
if err := os.WriteFile(inputPath, []byte(testInputJSON), 0o644); err != nil {
36+
t.Fatalf("failed to write input.json: %v", err)
37+
}
38+
if err := os.WriteFile(fallbackPath, []byte("not-json"), 0o644); err != nil {
39+
t.Fatalf("failed to write inputs.json: %v", err)
40+
}
41+
42+
_, err := ReadInputFile(inputPath)
43+
if err != nil {
44+
t.Fatalf("expected input.json to be used first, got error: %v", err)
45+
}
46+
}

0 commit comments

Comments
 (0)