LoadProfile loads a profile from the given filename. If the filename is "-", it instead reads from stdin.
(origin string)
| 49 | // LoadProfile loads a profile from the given filename. |
| 50 | // If the filename is "-", it instead reads from stdin. |
| 51 | func LoadProfile(origin string) ([]*cover.Profile, error) { |
| 52 | filename := origin |
| 53 | if origin == "-" { |
| 54 | // Annoyingly, ParseProfiles only accepts a filename, so we have to write the bytes to disk |
| 55 | // so it can read them back. |
| 56 | // We could probably also just give it /dev/stdin, but that'll break on Windows. |
| 57 | tf, err := os.CreateTemp("", "") |
| 58 | if err != nil { |
| 59 | return nil, fmt.Errorf("failed to create temp file: %w", err) |
| 60 | } |
| 61 | defer tf.Close() |
| 62 | defer os.Remove(tf.Name()) |
| 63 | if _, err := io.Copy(tf, os.Stdin); err != nil { |
| 64 | return nil, fmt.Errorf("failed to copy stdin to temp file: %w", err) |
| 65 | } |
| 66 | filename = tf.Name() |
| 67 | } |
| 68 | return cover.ParseProfiles(filename) |
| 69 | } |