-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
217 lines (186 loc) · 4.81 KB
/
args.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package args
import (
"os"
"path/filepath"
"strings"
"unicode"
)
// FileSystem is an abstraction over the native filesystem
type FileSystem interface {
// Stat returns a FileInfo describing the named file.
Stat(string) (os.FileInfo, error)
// Getwd returns a rooted path name corresponding to the current directory.
Getwd() (string, error)
}
// Args describes command line arguments
type Args struct {
fs FileSystem
// All lists of all arguments in command line (not including the command itself)
all []string
// Completed lists of all completed arguments in command line,
// If the last one is still being typed - no space after it,
// it won't appear in this list of arguments.
completed []string
// Last argument in command line, the one being typed, if the last
// character in the command line is a space, this argument will be empty,
// otherwise this would be the last word.
last string
// LastCompleted is the last argument that was fully typed.
// If the last character in the command line is space, this would be the
// last word, otherwise, it would be the word before that.
lastCompleted string
}
// ArgOptions represents a way to set optional values to a installer
// option.
// The ArgOptions shows what options are available to change.
type ArgOptions interface {
SetFileSystem(FileSystem)
}
// ArgOption captures a tweak that can be applied to the Arg.
type ArgOption func(ArgOptions)
type args struct {
fileSystem FileSystem
}
func (s *args) SetFileSystem(fs FileSystem) {
s.fileSystem = fs
}
// OptionFileSystem allows the setting a filesystem option to configure
// the args.
func OptionFileSystem(fs FileSystem) ArgOption {
return func(opt ArgOptions) {
opt.SetFileSystem(fs)
}
}
// New creates a Args from the command line arguments.
func New(line string, options ...ArgOption) *Args {
opt := new(args)
for _, option := range options {
option(opt)
}
var (
all []string
completed []string
)
parts := splitFields(line)
if len(parts) > 0 {
all = parts[1:]
completed = removeLast(parts[1:])
}
return &Args{
fs: opt.fileSystem,
all: all,
completed: completed,
last: last(parts),
lastCompleted: last(completed),
}
}
// AllCommands returns all the commands.
func (a *Args) AllCommands() (c []string) {
for _, v := range a.all {
if !strings.HasPrefix(v, "-") {
c = append(c, v)
}
}
return
}
// CompletedCommands returns all the potential completed commands
func (a *Args) CompletedCommands() (c []string) {
for _, v := range a.completed {
if !strings.HasPrefix(v, "-") {
c = append(c, v)
}
}
return
}
// Directory gives the directory of the current written
// last argument if it represents a file name being written.
// in case that it is not, we fall back to the current directory.
func (a *Args) Directory() string {
if info, err := a.fs.Stat(a.last); err == nil && info.IsDir() {
return a.fixPathForm(a.last, a.last)
}
dir := filepath.Dir(a.last)
if info, err := a.fs.Stat(dir); err != nil || !info.IsDir() {
return "./"
}
return a.fixPathForm(a.last, dir)
}
// From captures a set of Args from a index.
func (a *Args) From(i int) *Args {
if i > len(a.all) {
i = len(a.all)
}
a.all = a.all[i:]
if i > len(a.completed) {
i = len(a.completed)
}
a.completed = a.completed[i:]
return a
}
// Last returns the last argument
func (a *Args) Last() string {
return a.last
}
// LastCompleted returns the last completed argument
func (a *Args) LastCompleted() string {
return a.lastCompleted
}
// fixPathForm changes a file name to a relative name
func (a *Args) fixPathForm(last string, file string) string {
// get working directory for relative name
workDir, err := a.fs.Getwd()
if err != nil {
return file
}
abs, err := filepath.Abs(file)
if err != nil {
return file
}
// if last is absolute, return path as absolute
if filepath.IsAbs(last) {
return a.fixDirPath(abs)
}
rel, err := filepath.Rel(workDir, abs)
if err != nil {
return file
}
// fix ./ prefix of path
if rel != "." && strings.HasPrefix(last, ".") {
rel = "./" + rel
}
return a.fixDirPath(rel)
}
func (a *Args) fixDirPath(path string) string {
info, err := a.fs.Stat(path)
if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") {
path += "/"
}
return path
}
func splitFields(line string) []string {
parts := strings.Fields(line)
if len(line) > 0 && unicode.IsSpace(rune(line[len(line)-1])) {
parts = append(parts, "")
}
parts = splitLastEqual(parts)
return parts
}
func splitLastEqual(line []string) []string {
if len(line) == 0 {
return line
}
parts := strings.Split(line[len(line)-1], "=")
return append(line[:len(line)-1], parts...)
}
func removeLast(a []string) []string {
if len(a) > 0 {
return a[:len(a)-1]
}
return a
}
func last(args []string) string {
if len(args) == 0 {
return ""
}
return args[len(args)-1]
}