-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (129 loc) · 3.4 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"github.com/ninadingole/gotest-ls/pkg"
)
var (
// pretty is a flag to print the json output in a pretty format.
pretty = flag.Bool("p", false, "pretty print")
// file is a flag to specify a single file to parse.
file = flag.String("f", "", "file")
// help is a flag to print the help text.
help = flag.Bool("h", false, "help")
)
var (
// errPathIssue is the error message when the user provides both a file and a directory.
errPathIssue = errors.New("ERROR: cannot specify both a file and a directory")
// errNotAFile is the error message when the user provides a directory as a file.
errNotAFile = errors.New("ERROR: required file, provided directory")
)
func main() {
flag.Usage = printHelpText(flag.CommandLine.Output())
flag.Parse()
err := Process(&args{
file: *file,
dirs: flag.Args(),
help: *help,
pretty: *pretty,
}, os.Stdout)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// args is a struct that contains the arguments provided by the user.
type args struct {
file string
dirs []string
help bool
pretty bool
}
// Process is the main function that processes the arguments and prints the output.
func Process(proc *args, writer io.Writer) error {
if requiresHelp(proc) {
printHelpText(writer)()
return nil
}
if err := validateArgs(proc); err != nil {
return err
}
if proc.file != "" {
proc.dirs = append(proc.dirs, proc.file)
}
tests, err := pkg.List(proc.dirs)
if err != nil {
return fmt.Errorf("failed to list the tests: %w", err)
}
if len(tests) == 0 {
_, _ = writer.Write([]byte("No tests found\n"))
return nil
}
marshal, err := json.Marshal(tests)
if err != nil {
return fmt.Errorf("failed to marshal json: %w", err)
}
if proc.pretty {
if err := prettyPrint(marshal, writer); err != nil {
return err
}
} else {
_, _ = writer.Write(marshal)
}
return nil
}
// validateArgs validates the arguments provided by the user.
func validateArgs(args *args) error {
if args.file != "" && len(args.dirs) > 0 {
return errPathIssue
}
if args.file != "" {
stat, err := os.Stat(args.file)
if err != nil {
return fmt.Errorf("failed to get file stat: %w", err)
}
if stat.IsDir() {
return errNotAFile
}
}
return nil
}
// requiresHelp checks if the user has requested help and not provided any required arguments.
func requiresHelp(proc *args) bool {
return (len(proc.dirs) == 0 && proc.file == "") || proc.help
}
// prettyPrint prints the given json in a pretty format.
func prettyPrint(data []byte, writer io.Writer) error {
var prettyJSON bytes.Buffer
err := json.Indent(&prettyJSON, data, "", "\t")
if err != nil {
return fmt.Errorf("failed to prettify output: %w", err)
}
_, err = writer.Write(prettyJSON.Bytes())
return err
}
// printHelpText prints the help text for the program.
func printHelpText(writer io.Writer) func() {
return func() {
writer := writer
_, _ = fmt.Fprintf(writer, `gotest-ls provides a list of all tests in a package or a file in JSON format.
Usage:
gotest-ls [flags] [directories]
Examples:
gotest-ls .
gotest-ls -p ./cmd
gotest-ls -p ./cmd ./pkg
gotest-ls -f ./pkg/random_test.go
gotest-ls -p -f ./pkg/random_test.go
Flags:
-f, --file string Path to a file, cannot be used with directories
-h, --help help for gotest-ls
-p, --pretty Pretty print the output in JSON format
`)
}
}