...
1
16
17 package util
18
19 import (
20 "fmt"
21 "io"
22 "os"
23
24 "edge-infra.dev/third_party/gopherage/pkg/cov"
25
26 "golang.org/x/tools/cover"
27 )
28
29
30
31 func DumpProfile(destination string, profile []*cover.Profile) error {
32 var output io.Writer
33 if destination == "-" {
34 output = os.Stdout
35 } else {
36 f, err := os.Create(destination)
37 if err != nil {
38 return fmt.Errorf("failed to open %s: %v", destination, err)
39 }
40 defer f.Close()
41 output = f
42 }
43 err := cov.DumpProfile(profile, output)
44 if err != nil {
45 return fmt.Errorf("failed to dump profile: %v", err)
46 }
47 return nil
48 }
49
50
51
52 func LoadProfile(origin string) ([]*cover.Profile, error) {
53 filename := origin
54 if origin == "-" {
55
56
57
58 tf, err := os.CreateTemp("", "")
59 if err != nil {
60 return nil, fmt.Errorf("failed to create temp file: %v", err)
61 }
62 defer tf.Close()
63 defer os.Remove(tf.Name())
64 if _, err := io.Copy(tf, os.Stdin); err != nil {
65 return nil, fmt.Errorf("failed to copy stdin to temp file: %v", err)
66 }
67 filename = tf.Name()
68 }
69 return cover.ParseProfiles(filename)
70 }
71
View as plain text