(r fileRequest)
| 638 | } |
| 639 | |
| 640 | func (cmd *mainCmd) readFile(r fileRequest) ([]byte, error) { |
| 641 | if r.Filepath != "-" { |
| 642 | return errtrace.Wrap2(os.ReadFile(r.Filename)) |
| 643 | } |
| 644 | |
| 645 | if r.Write { |
| 646 | return nil, errtrace.Wrap(fmt.Errorf("can't use -w with stdin")) |
| 647 | } |
| 648 | |
| 649 | if r.ImplicitStdin { |
| 650 | // Running with no args reads from stdin, but this is not obvious |
| 651 | // so print a usage hint to stderr, if we think stdin is a TTY. |
| 652 | // Best-effort check for a TTY by looking for a character device. |
| 653 | type statter interface { |
| 654 | Stat() (os.FileInfo, error) |
| 655 | } |
| 656 | if st, ok := cmd.Stdin.(statter); ok { |
| 657 | if fi, err := st.Stat(); err == nil && |
| 658 | fi.Mode()&os.ModeCharDevice == os.ModeCharDevice { |
| 659 | cmd.log.Println("reading from stdin; use '-h' for help") |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return errtrace.Wrap2(io.ReadAll(cmd.Stdin)) |
| 665 | } |
| 666 | |
| 667 | type walker struct { |
| 668 | // Inputs |
no test coverage detected