FromFile loads and returns the contents of a given file. If - is passed through, much like git, it will read from stdin. This can be piped data, unless there is a tty in which case the user will be prompted to enter a message.
(fileName string)
| 86 | // unless there is a tty in which case the user will be prompted to enter a |
| 87 | // message. |
| 88 | func FromFile(fileName string) (string, error) { |
| 89 | if fileName == "-" { |
| 90 | stat, err := os.Stdin.Stat() |
| 91 | if err != nil { |
| 92 | return "", fmt.Errorf("Error reading from stdin: %v\n", err) |
| 93 | } |
| 94 | if (stat.Mode() & os.ModeCharDevice) == 0 { |
| 95 | // There is no tty. This will allow us to read piped data instead. |
| 96 | output, err := io.ReadAll(os.Stdin) |
| 97 | if err != nil { |
| 98 | return "", fmt.Errorf("Error reading from stdin: %v\n", err) |
| 99 | } |
| 100 | return string(output), err |
| 101 | } |
| 102 | |
| 103 | fmt.Printf("(reading comment from standard input)\n") |
| 104 | var output bytes.Buffer |
| 105 | s := bufio.NewScanner(os.Stdin) |
| 106 | for s.Scan() { |
| 107 | output.Write(s.Bytes()) |
| 108 | output.WriteRune('\n') |
| 109 | } |
| 110 | return output.String(), nil |
| 111 | } |
| 112 | |
| 113 | output, err := os.ReadFile(fileName) |
| 114 | if err != nil { |
| 115 | return "", fmt.Errorf("Error reading file: %v\n", err) |
| 116 | } |
| 117 | return string(output), err |
| 118 | } |
| 119 | |
| 120 | func startInlineCommand(command string, args ...string) (*exec.Cmd, error) { |
| 121 | cmd := exec.Command(command, args...) |