/
testcase_get.go
69 lines (57 loc) · 1.43 KB
/
testcase_get.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
package cmd
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"github.com/spf13/cobra"
)
var (
// testCaseGetCmd represents the testCaseGetCmd command
testCaseGetCmd = &cobra.Command{
Use: "get <test-case-ref> [file]",
Aliases: []string{"download"},
Short: "Download test case definition",
Long: `Download the JavaScript definition of test case
<test-case-ref> can be 'organisation-name/test-case-name' or 'test-case-uid'.
[file] can be '-' to output to STDOUT (default) or path to file
to write to.
`,
Run: runTestCaseGet,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
log.Fatal("Missing argument: test case reference")
}
if len(args) > 2 {
log.Fatal("Too many arguments")
}
},
ValidArgsFunction: completeOrgaAndCase,
}
)
func init() {
TestCaseCmd.AddCommand(testCaseGetCmd)
}
func runTestCaseGet(cmd *cobra.Command, args []string) {
client := NewClient()
testCaseUID := mustLookupTestCase(client, args[0])
success, response, err := client.DownloadTestCaseDefinition(testCaseUID)
if err != nil {
log.Fatal(err)
}
if !success {
log.Fatalf("Test case definition could not be downloaded!\n%s\n", string(response))
}
if len(args) == 2 && args[1] != "-" {
err := ioutil.WriteFile(args[1], response, 0644)
if err != nil {
log.Fatal(err)
}
} else {
_, err := io.Copy(os.Stdout, bytes.NewReader(response))
if err != nil {
log.Fatal(err)
}
}
}