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