openFile opens a file and returns a reader and cleanup function
(filePath string)
| 50 | |
| 51 | // openFile opens a file and returns a reader and cleanup function |
| 52 | func (ir *InputResolver) openFile(filePath string) (io.Reader, func() error, error) { |
| 53 | // Expand relative path to absolute |
| 54 | absPath, err := filepath.Abs(filePath) |
| 55 | if err != nil { |
| 56 | return nil, nil, fmt.Errorf("failed to resolve path %s: %w", filePath, err) |
| 57 | } |
| 58 | |
| 59 | // Check if file exists |
| 60 | if _, err := os.Stat(absPath); os.IsNotExist(err) { |
| 61 | return nil, nil, fmt.Errorf("file not found: %s", absPath) |
| 62 | } |
| 63 | |
| 64 | // Open the file |
| 65 | file, err := os.Open(absPath) |
| 66 | if err != nil { |
| 67 | return nil, nil, fmt.Errorf("failed to open file %s: %w", absPath, err) |
| 68 | } |
| 69 | |
| 70 | if ir.verbose { |
| 71 | fmt.Fprintf(os.Stderr, "Reading from file: %s\n", absPath) |
| 72 | } |
| 73 | |
| 74 | cleanup := func() error { |
| 75 | return file.Close() |
| 76 | } |
| 77 | |
| 78 | return bufio.NewReader(file), cleanup, nil |
| 79 | } |
| 80 | |
| 81 | // ReadAll reads all content from the resolved input source |
| 82 | func (ir *InputResolver) ReadAll(args []string) ([]byte, error) { |