MCPcopy
hub / github.com/cli/cli / readFileRun

Function readFileRun

pkg/cmd/repo/read-file/read_file.go:129–206  ·  view source on GitHub ↗
(opts *ReadFileOptions)

Source from the content-addressed store, hash-verified

127}
128
129func readFileRun(opts *ReadFileOptions) error {
130 httpClient, err := opts.HttpClient()
131 if err != nil {
132 return err
133 }
134
135 repo, err := opts.BaseRepo()
136 if err != nil {
137 return fmt.Errorf("%w. Run this command from within a git repository, or use the `--repo` flag to specify one", err)
138 }
139
140 file, err := fetchFile(httpClient, repo, opts.Path, opts.Ref)
141 if err != nil {
142 return err
143 }
144
145 // If the API didn't return the file content inline, it'll set the encoding to "none" (e.g. for large files).
146 contentAvailable := file.Encoding != "none"
147
148 if opts.Exporter != nil {
149 // Only pay for the file content when the caller actually selected the content field.
150 if !contentAvailable && slices.Contains(opts.Exporter.Fields(), "content") {
151 if err := loadContent(httpClient, repo, file, opts.Ref); err != nil {
152 return err
153 }
154 }
155 return opts.Exporter.Write(opts.IO, file)
156 }
157
158 if !contentAvailable {
159 if err := loadContent(httpClient, repo, file, opts.Ref); err != nil {
160 return err
161 }
162 }
163
164 if opts.Output != "" {
165 dest, err := writeToOutput(file, opts.Output, opts.Clobber)
166 if err != nil {
167 return err
168 }
169 if opts.IO.IsStdoutTTY() {
170 cs := opts.IO.ColorScheme()
171 fmt.Fprintf(opts.IO.ErrOut, "%s Wrote %s to %s\n", cs.SuccessIcon(), file.Path, dest)
172 }
173 return nil
174 }
175
176 if len(file.Content) == 0 {
177 cs := opts.IO.ColorScheme()
178 fmt.Fprintf(opts.IO.ErrOut, "%s file is empty\n", cs.WarningIcon())
179 return nil
180 }
181
182 if mime, ok := binaryContentType(file.Content); ok {
183 if opts.IO.IsStdoutTTY() {
184 return fmt.Errorf("binary file (%s, %s); use --output to save to a file or pipe stdout",
185 mime, text.FormatSize(int64(file.Size)))
186 }

Callers 2

NewCmdReadFileFunction · 0.85
Test_readFileRunFunction · 0.85

Calls 15

FormatSizeFunction · 0.92
fetchFileFunction · 0.85
loadContentFunction · 0.85
writeToOutputFunction · 0.85
binaryContentTypeFunction · 0.85
containsEscapeSequenceFunction · 0.85
ContainsMethod · 0.80
IsStdoutTTYMethod · 0.80
ColorSchemeMethod · 0.80
SuccessIconMethod · 0.80
WarningIconMethod · 0.80
StartPagerMethod · 0.80

Tested by 1

Test_readFileRunFunction · 0.68