-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
59 lines (48 loc) · 1.55 KB
/
cli.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
package main
import (
"go/ast"
"go/parser"
"strings"
"github.com/sirkon/errors"
)
type cliDefinition struct {
SourcePackage string `short:"s" help:"Source package to look for interfaces in."`
Destination string `short:"d" help:"Destination file to store generated mocks in with the current directory as a root." required:""`
Package string `short:"p" help:"Package name for generated file. Will be ignored if the package exists and has different name."`
Whatever bool `short:"w" help:"Generate 'whatever' recorders."`
Interfaces []interfaceName `arg:"" help:"List of interfaces names to generate mocks for" optional:""`
Version bool `short:"v" help:"Show version and exit."`
}
type packagePath string
type interfaceName struct {
base string
replacement string
}
func (n *interfaceName) UnmarshalText(data []byte) error {
text := string(data)
name, replacement, found := strings.Cut(text, "=")
if !found {
replacement = name
}
ifacename, err := parser.ParseExpr(name)
if err != nil {
return errors.Newf("invalid interface name '%s'", text)
}
switch ifacename.(type) {
case *ast.Ident:
default:
return errors.Newf("invalid interface name '%s'", text)
}
replacementname, err := parser.ParseExpr(replacement)
if err != nil {
return errors.Newf("invalid replacement name '%s'", text)
}
switch replacementname.(type) {
case *ast.Ident:
default:
return errors.Newf("invalid replacement name '%s'", text)
}
n.base = name
n.replacement = replacement
return nil
}