MCPcopy Create free account
hub / github.com/cli/cli / processFiles

Function processFiles

pkg/cmd/gist/create/create.go:190–240  ·  view source on GitHub ↗
(stdin io.ReadCloser, filenameOverride string, filenames []string)

Source from the content-addressed store, hash-verified

188}
189
190func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []string) (map[string]*shared.GistFile, error) {
191 fs := map[string]*shared.GistFile{}
192
193 if len(filenames) == 0 {
194 return nil, errors.New("no files passed")
195 }
196
197 for i, f := range filenames {
198 var filename string
199 var content []byte
200 var err error
201
202 if f == "-" {
203 if filenameOverride != "" {
204 filename = filenameOverride
205 } else {
206 filename = fmt.Sprintf("gistfile%d.txt", i)
207 }
208 content, err = io.ReadAll(stdin)
209 if err != nil {
210 return fs, fmt.Errorf("failed to read from stdin: %w", err)
211 }
212 stdin.Close()
213
214 if shared.IsBinaryContents(content) {
215 return nil, fmt.Errorf("binary file contents not supported")
216 }
217 } else {
218 isBinary, err := shared.IsBinaryFile(f)
219 if err != nil {
220 return fs, fmt.Errorf("failed to read file %s: %w", f, err)
221 }
222 if isBinary {
223 return nil, fmt.Errorf("failed to upload %s: binary file not supported", f)
224 }
225
226 content, err = os.ReadFile(f)
227 if err != nil {
228 return fs, fmt.Errorf("failed to read file %s: %w", f, err)
229 }
230
231 filename = filepath.Base(f)
232 }
233
234 fs[filename] = &shared.GistFile{
235 Content: string(content),
236 }
237 }
238
239 return fs, nil
240}
241
242func guessGistName(files map[string]*shared.GistFile) string {
243 filenames := make([]string, 0, len(files))

Callers 2

Test_processFilesFunction · 0.85
createRunFunction · 0.85

Calls 5

IsBinaryContentsFunction · 0.92
IsBinaryFileFunction · 0.92
BaseMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65

Tested by 1

Test_processFilesFunction · 0.68