-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.go
112 lines (96 loc) · 2.45 KB
/
config.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
package main
import (
"errors"
"io"
"os"
"path/filepath"
"regexp"
"github.com/codegangsta/cli"
)
// Config is the command configuration
type Config struct {
Pattern *regexp.Regexp
Module *regexp.Regexp
Reverse bool
Start *regexp.Regexp
End *regexp.Regexp
Format string
Paths []string
Recursive bool
Root string
Output io.Writer
}
func makeConfig(ctx *cli.Context) (*Config, []error) {
var errs []error
if ctx.GlobalBool("help") || ctx.NumFlags() == 0 {
errs = append(errs, errors.New(""))
return nil, errs
}
if ctx.GlobalString("pattern") == "" {
errs = append(errs, errors.New("specify --pattern (-p) to extract imports"))
}
pattern, err := regexp.Compile(ctx.GlobalString("pattern"))
if err != nil {
errs = append(errs, errors.New(regexErrorMessage("--pattern (-p)")+err.Error()))
}
module, err := regexp.Compile(ctx.GlobalString("module"))
if err != nil {
errs = append(errs, errors.New(regexErrorMessage("--module (-m)")+err.Error()))
}
if ctx.GlobalString("module") == "" {
module = nil
}
start, err := regexp.Compile(ctx.GlobalString("start"))
if err != nil {
errs = append(errs, errors.New(regexErrorMessage("--start (-s)")+err.Error()))
}
if ctx.GlobalString("start") == "" {
start = nil
}
end, err := regexp.Compile(ctx.GlobalString("end"))
if err != nil {
errs = append(errs, errors.New(regexErrorMessage("--end (-e)")+err.Error()))
}
if ctx.GlobalString("end") == "" {
end = nil
}
root := ctx.GlobalString("root")
if root != "" {
root, err = filepath.Abs(root)
if err != nil {
errs = append(errs, errors.New(regexErrorMessage("--root")+err.Error()))
}
}
output := ctx.App.Writer
outfile := ctx.GlobalString("output")
if outfile != "" {
file, err := os.Create(outfile)
if err != nil {
errs = append(errs, errors.New("cannot create the output file: "+outfile))
} else {
output = file
}
}
paths := ctx.Args()
if len(paths) == 0 {
errs = append(errs, errors.New("specify source codes"))
}
if len(errs) > 0 {
return nil, errs
}
return &Config{
Pattern: pattern,
Module: module,
Reverse: ctx.GlobalBool("reverse"),
Start: start,
End: end,
Format: ctx.GlobalString("format"),
Paths: paths,
Recursive: ctx.GlobalBool("recursive"),
Root: root,
Output: output,
}, nil
}
func regexErrorMessage(flag string) string {
return "The argument of " + flag + " is invalid. Specify a valid regular expression.\n"
}