-
Notifications
You must be signed in to change notification settings - Fork 1
/
value.go
165 lines (143 loc) · 3.33 KB
/
value.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
package main
import (
"bytes"
"errors"
"os"
"path/filepath"
"strconv"
"strings"
)
type ValueDef interface {
Values(*Shell, StdStreams) ([]string, error)
Value(*Shell, StdStreams) (string, error)
}
type LiteralValueDef struct {
Val string
Expand bool
}
var _ ValueDef = LiteralValueDef{}
func (d LiteralValueDef) Values(sh *Shell, std StdStreams) ([]string, error) {
if d.Expand {
exp, err := filepath.Glob(d.Val)
if err != nil {
return nil, err
}
if exp != nil {
return exp, nil
}
}
return []string{d.Val}, nil
}
func (d LiteralValueDef) Value(sh *Shell, std StdStreams) (string, error) {
return d.Val, nil
}
func ParamValueDef(param string) (ValueDef, error) {
if len(param) == 0 {
return nil, errors.New("invalid empty param")
}
switch p0 := param[0]; {
case p0 >= '0' && p0 <= '9':
argnum, err := strconv.ParseInt(param, 10, 64)
if err != nil {
return nil, err
}
return ArgValueDef{Number: int(argnum)}, nil
case strings.IndexByte("?#@$", p0) != -1:
return SpecialVarValueDef{Name: p0}, nil
default:
return VarValueDef{Name: param}, nil
}
}
type VarValueDef struct {
Name string
}
func (d VarValueDef) Values(sh *Shell, std StdStreams) ([]string, error) {
return []string{sh.GetVar(d.Name)}, nil
}
func (d VarValueDef) Value(sh *Shell, std StdStreams) (string, error) {
return sh.GetVar(d.Name), nil
}
type ArgValueDef struct {
Number int
}
func (d ArgValueDef) Values(sh *Shell, std StdStreams) ([]string, error) {
return []string{sh.GetArg(d.Number)}, nil
}
func (d ArgValueDef) Value(sh *Shell, std StdStreams) (string, error) {
return sh.GetArg(d.Number), nil
}
type SpecialVarValueDef struct {
Name byte
}
func (d SpecialVarValueDef) Values(sh *Shell, std StdStreams) ([]string, error) {
switch d.Name {
case '?':
return []string{strconv.Itoa(sh.LastExitCode())}, nil
case '#':
return []string{strconv.Itoa(sh.ArgCount())}, nil
case '@':
return sh.GetArgs(), nil
case '$':
return []string{strconv.Itoa(os.Getpid())}, nil
default:
panic("bug!")
}
}
func (d SpecialVarValueDef) Value(sh *Shell, std StdStreams) (string, error) {
switch d.Name {
case '?':
return strconv.Itoa(sh.LastExitCode()), nil
case '#':
return strconv.Itoa(sh.ArgCount()), nil
case '@':
return strings.Join(sh.GetArgs(), " "), nil
case '$':
return strconv.Itoa(os.Getpid()), nil
default:
panic("bug!")
}
}
type CommandValueDef struct {
Cmd Command
}
func (d CommandValueDef) Values(sh *Shell, std StdStreams) ([]string, error) {
v, err := d.Value(sh, std)
if err != nil {
return nil, err
}
return []string{v}, nil
}
func (d CommandValueDef) Value(sh *Shell, std StdStreams) (string, error) {
var buf bytes.Buffer
std.Out = &buf
job, err := d.Cmd.StartJob(sh, std)
if err != nil {
return "", err
}
res := job.Wait()
if res.ExitCode != 0 {
return "", res
}
return strings.TrimSuffix(buf.String(), "\n"), nil
}
type CompositeValueDef struct {
Parts []ValueDef
}
func (d CompositeValueDef) Values(sh *Shell, std StdStreams) ([]string, error) {
v, err := d.Value(sh, std)
if err != nil {
return nil, err
}
return []string{v}, nil
}
func (d CompositeValueDef) Value(sh *Shell, std StdStreams) (string, error) {
var b strings.Builder
for _, part := range d.Parts {
s, err := part.Value(sh, std)
if err != nil {
return "", err
}
b.WriteString(s)
}
return b.String(), nil
}