-
Notifications
You must be signed in to change notification settings - Fork 74
/
collect.go
101 lines (84 loc) · 2.04 KB
/
collect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package cmd
import (
"context"
"errors"
"fmt"
"io"
"os"
"github.com/testground/testground/pkg/api"
"github.com/testground/testground/pkg/client"
"github.com/testground/testground/pkg/logging"
"github.com/urfave/cli/v2"
)
// CollectCommand is the specification of the `collect` command.
var CollectCommand = cli.Command{
Name: "collect",
Usage: "collect the output assets of the supplied run into a .tgz archive",
Action: collectCommand,
ArgsUsage: "[run_id]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "runner",
Aliases: []string{"r"},
Usage: "runner to use; values include: 'local:exec', 'local:docker', 'cluster:k8s'",
Required: true,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "write the output archive to `FILENAME`",
},
},
}
func collectCommand(c *cli.Context) error {
ctx, cancel := context.WithCancel(ProcessContext())
defer cancel()
if c.NArg() != 1 {
return errors.New("missing run id")
}
var (
id = c.Args().First()
runner = c.String("runner")
output = id + ".tgz"
)
if o := c.String("output"); o != "" {
output = o
}
cl, _, err := setupClient(c)
if err != nil {
return err
}
return collect(ctx, cl, c.App.Writer, runner, id, output)
}
func collect(ctx context.Context, cl *client.Client, stdout io.Writer, runner string, runid string, outputFile string) error {
req := &api.OutputsRequest{
Runner: runner,
RunID: runid,
}
resp, err := cl.CollectOutputs(ctx, req)
if err != nil {
if err == context.Canceled {
return fmt.Errorf("interrupted")
}
return err
}
defer resp.Close()
file, err := os.Create(outputFile)
if err != nil {
if err == context.Canceled {
return fmt.Errorf("interrupted")
}
return err
}
defer file.Close()
cr, err := client.ParseCollectResponse(resp, file, stdout)
if err != nil {
return err
}
if !cr.Exists {
logging.S().Errorw("no such testplan run", "run_id", runid, "runner", runner)
return os.Remove(outputFile)
}
logging.S().Infof("created file: %s", outputFile)
return nil
}