-
Notifications
You must be signed in to change notification settings - Fork 16
/
util.go
118 lines (101 loc) · 2.6 KB
/
util.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
package someutils
const (
VERSION = "0.5.1-snapshot"
)
/*
// The CliUtil can be registered as a commandline tool, but NOT a pipeline-able function
type CliUtil interface {
Name() string
Function func([]string) error
}
*/
var (
allCliUtils = make(map[string]CliPipable)
allPipables = make(map[string]CliPipableFactory)
)
// Registers utils for use by 'some' command.
func Register(u CliPipable) {
allCliUtils[u.Name()] = u
}
func RegisterSimple(somefunc CliPipableSimpleFactory) {
RegisterPipable(func() CliPipable { return WrapCliPipable(somefunc()) })
Register(WrapCliPipable(somefunc()))
}
func RegisterPipable(somefunc CliPipableFactory) {
pipable := somefunc()
name := pipable.Name()
allPipables[name] = somefunc
//register as CliUtil if possible
pcu, ok := pipable.(CliPipable)
if ok {
//inPipe, outPipe, errPipe := StdPipes()
Register(pcu)
/*CliUtil{ func () string { return name }, func(call []string) error {
someutil := somefunc()
err := pcu.ParseFlags(call, errPipe)
if err != nil {
return err
}
err = pcu.Exec(inPipe, outPipe, errPipe)
return err*/
}
}
// deprecated. Use CliExists instead.
func Exists(name string) bool {
return CliExists(name)
}
// Has a CLI function been registered?
func CliExists(name string) bool {
_, exists := allCliUtils[name]
return exists
}
// deprecated. Use GetCliUtil, ParseFlags & Exec instead.
func Call(name string, args []string) (error, int) {
ps := StdInvocation()
util := allCliUtils[name]
return CallUtil(util, args, ps)
}
func CallUtil(util CliPipable, args []string, invocation *Invocation) (error, int) {
err, code := util.ParseFlags(args, invocation.ErrPipe.Out)
if err != nil {
return err, code
}
return util.Invoke(invocation)
}
func PipableExists(name string) bool {
_, exists := allPipables[name]
return exists
}
func GetCliPipableFactory(name string) CliPipableFactory {
return allPipables[name]
}
/*
func GetCliPipableFactory(name string) CliPipableFactory {
namedPipableFactory := GetCliPipableFactory(name)
pipableCliUtilFactory := func() CliPipable {
return namedPipableFactory().(CliPipable)
}
return pipableCliUtilFactory
}
*/
func GetPipableFactory(name string) PipableFactory {
namedPipableFactory := GetCliPipableFactory(name)
pipableFactory := func() Pipable {
return namedPipableFactory()
}
return pipableFactory
}
func List() []string {
ret := []string{}
for k, _ := range allCliUtils {
ret = append(ret, k)
}
return ret
}
// ArchiveItem is used by tar & zip
type ArchiveItem struct {
//if FileSystemPath is empty, use Data instead
FileSystemPath string
ArchivePath string
Data []byte
}