MCPcopy Index your code
hub / github.com/git-bug/git-bug / FromFile

Function FromFile

commands/input/input.go:88–118  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

86// unless there is a tty in which case the user will be prompted to enter a
87// message.
88func 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
120func startInlineCommand(command string, args ...string) (*exec.Cmd, error) {
121 cmd := exec.Command(command, args...)

Callers 2

BugCreateFileInputFunction · 0.92
BugCommentFileInputFunction · 0.92

Calls 8

ErrorfMethod · 0.80
ReadAllMethod · 0.65
PrintfMethod · 0.65
BytesMethod · 0.65
StringMethod · 0.65
StatMethod · 0.45
ModeMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected