ResolveInput resolves special input conventions for a raw flag value: - "-" → read all bytes from stdin - "@ " → read all bytes from the file at via fileIO - "@@..." → strip leading @ (escape for a literal @-prefixed value) - "'...'" → strip surrounding single quotes (Windows c
(raw string, stdin io.Reader, fileIO fileio.FileIO)
| 25 | // Allows callers to bypass shell quoting issues (especially Windows PowerShell 5) |
| 26 | // by reading JSON from a file (@path) or piping via stdin (-). |
| 27 | func ResolveInput(raw string, stdin io.Reader, fileIO fileio.FileIO) (string, error) { |
| 28 | if raw == "" { |
| 29 | return "", nil |
| 30 | } |
| 31 | |
| 32 | // stdin |
| 33 | if raw == "-" { |
| 34 | if stdin == nil { |
| 35 | return "", fmt.Errorf("stdin is not available") |
| 36 | } |
| 37 | data, err := io.ReadAll(stdin) |
| 38 | if err != nil { |
| 39 | return "", fmt.Errorf("failed to read stdin: %w", err) |
| 40 | } |
| 41 | s := strings.TrimSpace(string(data)) |
| 42 | if s == "" { |
| 43 | return "", fmt.Errorf("stdin is empty (did you forget to pipe input?)") |
| 44 | } |
| 45 | return s, nil |
| 46 | } |
| 47 | |
| 48 | // escape: @@... → literal @... (no file read) |
| 49 | if strings.HasPrefix(raw, "@@") { |
| 50 | return raw[1:], nil |
| 51 | } |
| 52 | |
| 53 | // file: @path |
| 54 | if strings.HasPrefix(raw, "@") { |
| 55 | path := strings.TrimSpace(raw[1:]) |
| 56 | if path == "" { |
| 57 | return "", fmt.Errorf("file path cannot be empty after @") |
| 58 | } |
| 59 | data, err := ReadInputFile(fileIO, path) |
| 60 | if err != nil { |
| 61 | return "", err |
| 62 | } |
| 63 | s := strings.TrimSpace(string(data)) |
| 64 | if s == "" { |
| 65 | return "", fmt.Errorf("file %q is empty", path) |
| 66 | } |
| 67 | return s, nil |
| 68 | } |
| 69 | |
| 70 | // strip surrounding single quotes (Windows cmd.exe passes them literally) |
| 71 | if len(raw) >= 2 && raw[0] == '\'' && raw[len(raw)-1] == '\'' { |
| 72 | raw = raw[1 : len(raw)-1] |
| 73 | } |
| 74 | |
| 75 | return raw, nil |
| 76 | } |
| 77 | |
| 78 | // ReadInputFile reads path through fileIO. Open/read failures are wrapped with |
| 79 | // path context; fileio.ErrPathValidation remains matchable with errors.Is. |