Skip to content

Commit

Permalink
Updated shell task, return multiple responses
Browse files Browse the repository at this point in the history
  • Loading branch information
phonkee committed Jul 22, 2015
1 parent 85a093e commit d9a0daf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
1 change: 1 addition & 0 deletions example/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"GET": {
"type": "shell",
"config": {
"single_result": 0,
"commands": [
{
"chdir": "/tmp",
Expand Down
53 changes: 23 additions & 30 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,35 @@ Run all commands and return results
*/
func (s *ShellTask) Run(r *http.Request, data map[string]interface{}) (response *Response) {

results := []map[string]interface{}{}
results := []*Response{}

response = NewResponse(http.StatusOK)

// run all commands
for _, command := range s.Config.Commands {

cmdresult := map[string]interface{}{
"out": nil,
"error": nil,
}
cmdresp := NewResponse(http.StatusOK)

var (
b string
e error
b string
e error
finalCommand string
cmd *exec.Cmd
)
if b, e = s.Interpolate(command.Command, data); e != nil {
cmdresult["error"] = e
results = append(results, cmdresult)
continue
cmdresp.Status(http.StatusInternalServerError).Error(e)
goto Append
}

finalCommand := b
finalCommand = b

// show command in result
if command.ReturnCommand {
cmdresult["command"] = finalCommand
cmdresp.AddValue("command", finalCommand)
}

// run command
cmd := exec.Command(s.Config.Shell, "-c", finalCommand)
cmd = exec.Command(s.Config.Shell, "-c", finalCommand)

// change directory if needed
if command.Chdir != "" {
Expand All @@ -170,30 +168,27 @@ func (s *ShellTask) Run(r *http.Request, data map[string]interface{}) (response

// get output
if out, err := cmd.Output(); err != nil {
cmdresult["error"] = err
results = append(results, cmdresult)
continue
cmdresp.Status(http.StatusInternalServerError).Error(err)
goto Append
} else {

// format out
if re, f, e := Format(string(strings.TrimSpace(string(out))), command.Format); e == nil {
cmdresult["out"] = re
cmdresult["format"] = f
cmdresp.Result(re).AddValue("format", f)
} else {
cmdresult["error"] = e
cmdresp.Status(http.StatusInternalServerError).Error(e)
}

results = append(results, cmdresult)
goto Append
}

Append:
results = append(results, cmdresp)
}

// single result
if s.Config.singleResultIndex != -1 {
response.Result(results[s.Config.singleResultIndex])
} else {
response.Result(map[string]interface{}{
"commands": results,
})
response.Result(results)
}

return
Expand Down Expand Up @@ -752,11 +747,9 @@ func (rt *RedisTask) Run(r *http.Request, data map[string]interface{}) (response
Args []interface{} `json:"args,omitempty"`
}



var (
address string
err error
err error
)
if address, err = rt.Interpolate(rt.config.Address, data); err != nil {
response.Error(err)
Expand Down Expand Up @@ -1121,7 +1114,7 @@ func (m *MySQLTask) Run(r *http.Request, data map[string]interface{}) (response
var (
db *sqlx.DB
rows *sqlx.Rows
err error
err error
)

for _, query := range m.config.Queries {
Expand Down Expand Up @@ -1432,7 +1425,7 @@ func (f *FilesystemDirectoryTask) Run(r *http.Request, data map[string]interface

var (
err error
fi os.FileInfo
fi os.FileInfo
)
if fi, err = os.Stat(final); err != nil {
return response.Status(http.StatusNotFound)
Expand Down

0 comments on commit d9a0daf

Please sign in to comment.