Repository navigation
Expand file tree
/
Copy pathopt.go
More file actions
60 lines (52 loc) · 1.43 KB
/
Copy pathopt.go
File metadata and controls
60 lines (52 loc) · 1.43 KB
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
package command
import "github.com/spf13/afero"
// diffUnifiedFlag enables unified diff output (-u). Default off.
type diffUnifiedFlag bool
const (
DiffUnified diffUnifiedFlag = true
DiffNoUnified diffUnifiedFlag = false
)
// DiffFs injects the filesystem used to open File positionals, so tests can
// supply an in-memory filesystem. The zero value falls back to the OS.
type DiffFs struct{ afero.Fs }
// value returns the configured filesystem, defaulting to the OS filesystem.
func (f DiffFs) value() afero.Fs {
if f.Fs == nil {
return afero.NewOsFs()
}
return f.Fs
}
// flags holds the parsed diff options.
type flags struct {
fs DiffFs
unifiedEnabled diffUnifiedFlag
}
// with folds one option value into the flags, returning the updated copy and
// whether the argument was one of this command's option types (false leaves it
// for positional classification).
func (f flags) with(o any) (flags, bool) {
switch v := o.(type) {
case diffUnifiedFlag:
f.unifiedEnabled = v
case DiffFs:
f.fs = v
default:
return f, false
}
return f, true
}
// foldOptions folds the command's own option values into a flags value and
// returns the remaining arguments (positional inputs) in their original order.
func foldOptions(opts []any) (flags, []any) {
var f flags
var rest []any
for _, o := range opts {
next, isOption := f.with(o)
if !isOption {
rest = append(rest, o)
continue
}
f = next
}
return f, rest
}