resolveInlineOrFileBytes supports agent-friendly inputs for flags that otherwise require shell-escaped JSON strings. Supported forms: - literal: '{"a":1}' - stdin: '-' - file: '@path/to/file.json' - stdin: '@-'
(spec string, input io.Reader)
| 18 | // - file: '@path/to/file.json' |
| 19 | // - stdin: '@-' |
| 20 | func resolveInlineOrFileBytes(spec string, input io.Reader) ([]byte, error) { |
| 21 | spec = strings.TrimSpace(spec) |
| 22 | if spec == "" { |
| 23 | return nil, nil |
| 24 | } |
| 25 | |
| 26 | readStdin := func() ([]byte, error) { |
| 27 | b, err := io.ReadAll(input) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | return b, nil |
| 32 | } |
| 33 | |
| 34 | switch { |
| 35 | case spec == "-": |
| 36 | return readStdin() |
| 37 | case strings.HasPrefix(spec, "@"): |
| 38 | path := strings.TrimSpace(strings.TrimPrefix(spec, "@")) |
| 39 | if path == "" { |
| 40 | return nil, fmt.Errorf("empty @file reference") |
| 41 | } |
| 42 | if path == "-" { |
| 43 | return readStdin() |
| 44 | } |
| 45 | path, err := config.ExpandPath(path) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | b, err := os.ReadFile(path) //nolint:gosec // user-provided path |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | return b, nil |
| 54 | default: |
| 55 | return []byte(spec), nil |
| 56 | } |
| 57 | } |