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