/
list.go
70 lines (62 loc) · 1.54 KB
/
list.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
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/zoncoen/scenarigo"
"github.com/zoncoen/scenarigo/cmd/scenarigo/cmd/config"
"github.com/zoncoen/scenarigo/reporter"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "list the test scenario files",
Long: "Lists the test scenario files as relative paths from the current directory.",
RunE: list,
SilenceErrors: true,
SilenceUsage: true,
}
func init() {
rootCmd.AddCommand(listCmd)
}
func list(cmd *cobra.Command, args []string) error {
opts := []func(*scenarigo.Runner) error{}
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if cfg != nil {
if len(args) > 0 {
cfg.Scenarios = nil
}
opts = append(opts, scenarigo.WithConfig(cfg))
}
if len(args) > 0 {
opts = append(opts, scenarigo.WithScenarios(args...))
}
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
for _, arg := range args {
opts = append(opts, scenarigo.WithScenarios(arg))
}
r, err := scenarigo.NewRunner(opts...)
if err != nil {
return err
}
var retErr error
reporterOpts := []reporter.Option{reporter.WithWriter(io.Discard)}
reporter.Run(func(rptr reporter.Reporter) {
for _, file := range r.ScenarioFiles() {
rel, err := filepath.Rel(wd, file)
if err != nil {
retErr = fmt.Errorf("failed to get releative path: %w", err)
break
}
fmt.Fprintln(cmd.OutOrStdout(), rel)
}
}, reporterOpts...)
return retErr
}