-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathactions_steps_shell.go
More file actions
135 lines (118 loc) · 3.62 KB
/
actions_steps_shell.go
File metadata and controls
135 lines (118 loc) · 3.62 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
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
package diecast
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/ghetzel/go-stockutil/executil"
"github.com/ghetzel/go-stockutil/fileutil"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/typeutil"
"github.com/husobee/vestigo"
shellwords "github.com/mattn/go-shellwords"
)
// [type=shell] Return the output of a command as a response.
// Valid Configurations
//
// Command string passed to the user's $SHELL:
//
// data: 'some shell command'
//
// Command array executed via exec*() system calls:
//
// data: ['command', '--arg', 'x=1', '--help']
//
// Command options specified as an object:
//
// data:
// command: # interpreted the same as above (string or array)
// inherit: true|false # whether the current shell environment should be inherited by the command
// env:
// X: abc
// Y: zyx
//
// -------------------------------------------------------------------------------------------------
type ShellStep struct{}
func (step *ShellStep) Perform(config *StepConfig, w http.ResponseWriter, req *http.Request, prev *StepConfig) (any, error) {
var cmd *executil.Cmd
var command any
var inherit = true
var env = make(map[string]any)
// parse options format
if typeutil.IsMap(config.Data) {
var cfg = maputil.M(config.Data)
command = cfg.Get(`command`).Value
} else {
command = config.Data
}
// parse command line
var args []string
if typeutil.IsArray(command) {
args = sliceutil.Stringify(command)
} else {
var script = typeutil.String(command)
// put multiline strings into a temp file and execute it as a standalone script
if strings.Contains(script, "\n") {
if shell := executil.FindShell(); shell != `` {
if tmpfile, err := fileutil.WriteTempFile(script, `diecast-`); err == nil {
config.logstep("multiline script written to %s", tmpfile)
defer os.Remove(tmpfile)
args = []string{shell, tmpfile}
} else {
return nil, fmt.Errorf("failed write temporary file: %v", err)
}
} else {
return nil, fmt.Errorf("cannot locate user shell")
}
} else if a, err := shellwords.Parse(script); err == nil {
args = a
} else {
return nil, fmt.Errorf("failed to parse command line: %v", err)
}
}
if len(args) > 0 {
cmd = executil.Command(args[0], args[1:]...)
} else {
return nil, fmt.Errorf("command array cannot be empty")
}
if cmd != nil {
cmd.Timeout = config.getTimeout()
cmd.InheritEnv = inherit
cmd.Stdin = prev
cmd.OnStart = func(s executil.Status) {
config.logstep("command started (timeout: %v)", cmd.Timeout)
}
cmd.OnComplete = func(s executil.Status) {
config.logstep("%v", s)
}
// explicitly-set environment variables
for k, v := range env {
cmd.SetEnv(k, v)
}
// request headers (prefixed with REQ_HEADER_)
for k, v := range maputil.M(req.Header).MapNative() {
k = stringutil.Underscore(k)
k = strings.ToUpper(k)
cmd.SetEnv(fmt.Sprintf("REQ_HEADER_%s", k), v)
}
// querystring params (prefixed with REQ_PARAM_)
for k, v := range maputil.M(req.URL.Query()).MapNative() {
k = strings.TrimPrefix(k, `:`)
k = stringutil.Underscore(k)
k = strings.ToUpper(k)
cmd.SetEnv(fmt.Sprintf("REQ_PARAM_%s", k), v)
}
// positional URL parameters (prefixed with REQ_PARAM_)
for _, k := range vestigo.ParamNames(req) {
k = strings.TrimPrefix(k, `:`)
var kName = stringutil.Underscore(k)
kName = strings.ToUpper(kName)
cmd.SetEnv(fmt.Sprintf("REQ_PARAM_%s", kName), vestigo.Param(req, k))
}
return cmd.CombinedOutput()
} else {
return nil, fmt.Errorf("invalid shell")
}
}