readSource reads an artifact's content and works out its type and name. typeFlag is the type from --type, or empty without it.
(s source, stdin io.ReadCloser, typeFlag string)
| 357 | // readSource reads an artifact's content and works out its type and name. |
| 358 | // typeFlag is the type from --type, or empty without it. |
| 359 | func readSource(s source, stdin io.ReadCloser, typeFlag string) (artifact, error) { |
| 360 | a := artifact{source: s, artifactType: s.artifactType(typeFlag), name: s.name} |
| 361 | switch s.kind { |
| 362 | case fromBodyFlag: |
| 363 | a.body = s.body |
| 364 | case fromStdin: |
| 365 | body, _, err := shared.ReadContent(s.arg, stdin) |
| 366 | if err != nil { |
| 367 | return a, fmt.Errorf("failed to read artifact content from standard input: %w", err) |
| 368 | } |
| 369 | a.body = body |
| 370 | case fromURL: |
| 371 | u, ok := shared.HTTPURL(s.arg) |
| 372 | if !ok { |
| 373 | return a, fmt.Errorf("invalid link URL %q: must be an http(s) URL with a host, in printable ASCII with no spaces", s.arg) |
| 374 | } |
| 375 | a.body = u |
| 376 | if a.name == "" { |
| 377 | // The server rejects the ":" and "/" of a whole URL in a name. |
| 378 | parsed, err := url.Parse(u) |
| 379 | if err != nil { |
| 380 | return a, err |
| 381 | } |
| 382 | a.name = parsed.Hostname() |
| 383 | } |
| 384 | case fromFile: |
| 385 | content, dir, err := shared.ReadContent(s.arg, stdin) |
| 386 | if err != nil { |
| 387 | return a, fmt.Errorf("failed to read artifact content from %s: %w", s.arg, err) |
| 388 | } |
| 389 | if shared.IsShortcutFile(s.arg) { |
| 390 | u, err := shared.ShortcutURL(content) |
| 391 | if errors.Is(err, shared.ErrNoShortcutSection) { |
| 392 | return a, fmt.Errorf("%s: %w; pass the URL as an argument instead", s.arg, err) |
| 393 | } else if err != nil { |
| 394 | return a, fmt.Errorf("%s: %w", s.arg, err) |
| 395 | } |
| 396 | a.body = u |
| 397 | } else { |
| 398 | a.body, a.dir = content, dir |
| 399 | } |
| 400 | if a.name == "" { |
| 401 | a.name = nameFromFile(s.arg) |
| 402 | if a.name == "" { |
| 403 | hint := "name it with `#<name>` or `--name`" |
| 404 | if s.bodyFile { |
| 405 | hint = "name it with `--name`" |
| 406 | } |
| 407 | return a, fmt.Errorf("cannot derive an artifact name from %s: the file name has no letters or numbers; %s", s.arg, hint) |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | return a, nil |
| 412 | } |
| 413 | |
| 414 | // nameFromFile returns the name gh gives an artifact from a file: its base |
| 415 | // name, as typed rather than a symlink's target, cleaned up to fit the |
no test coverage detected