Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion plugins/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package plugins

import (
"github.com/Matrix86/driplane/utils"
"errors"
"os"
"os/exec"
"strings"
"time"

"github.com/Matrix86/driplane/utils"
)

// UtilPackage contains useful generic methods
Expand Down Expand Up @@ -106,3 +110,41 @@ func (c *UtilPackage) Sha512File(filename string) UtilResponse {
Value: hash,
}
}

// Ecexute shell command
func (c *UtilPackage) ExecCommand(commandParts []string, inputData string) UtilResponse {
if len(commandParts) == 0 {
return UtilResponse{
Error: errors.New("no command provided"),
Status: false,
Value: "",
}
}

executable := commandParts[0]
args := commandParts[1:]

cmd := exec.Command(executable, args...)

// If inputData is provided, write it to the command's Stdin
// This replaces '<<EOF'
if inputData != "" {
cmd.Stdin = strings.NewReader(inputData)
}

output, err := cmd.CombinedOutput()

if err != nil {
return UtilResponse{
Error: err,
Status: false,
Value: string(output),
}
}

return UtilResponse{
Error: nil,
Status: true,
Value: string(output),
}
}
75 changes: 75 additions & 0 deletions plugins/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package plugins

import (
"os"
"runtime"
"strings"
"testing"
)

Expand Down Expand Up @@ -129,3 +131,76 @@ func TestUtilPluginSha512Method(t *testing.T) {
t.Errorf("Value has a bad value: expected=%s had=%s", expected, res.Value)
}
}

func TestUtilPluginExecCommandMethod(t *testing.T) {
u := GetUtil()

var echoCmd []string
expectedOutput := "hello world"
if runtime.GOOS == "windows" {
echoCmd = []string{"cmd", "/C", "echo", expectedOutput}
} else {
echoCmd = []string{"echo", expectedOutput}
}

tests := []struct {
name string // test name
cmdParts []string // command
inputData string // stdin
expectedStatus bool // status field of the response
expectError bool // Error field of the response
expectedValue string // returned value of the command
}{
{
name: "Error: no command ",
cmdParts: []string{},
inputData: "",
expectedStatus: false,
expectError: true,
expectedValue: "",
},
{
name: "Success: Simple Echo command",
cmdParts: echoCmd,
inputData: "",
expectedStatus: true,
expectError: false,
expectedValue: expectedOutput,
},
{
name: "Error: Command not exist",
cmdParts: []string{"not_existing_command_12345"},
inputData: "",
expectedStatus: false,
expectError: true,
expectedValue: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
response := u.ExecCommand(tt.cmdParts, tt.inputData)

if response.Status != tt.expectedStatus {
t.Errorf("Wrong status: expected %v, obtained %v", tt.expectedStatus, response.Status)
}

if tt.expectError && response.Error == nil {
t.Errorf("Expected error but obtained nil")
}
if !tt.expectError && response.Error != nil {
t.Errorf("Error not expected but obtained: %v", response.Error)
}

cleanOutput := strings.TrimSpace(response.Value)

if tt.expectedValue != "" {

if !strings.Contains(cleanOutput, tt.expectedValue) {
t.Errorf("Wrong output. \n Expected: %q\n Obtained: %q", tt.expectedValue, cleanOutput)
}
}
})
}

}
23 changes: 23 additions & 0 deletions src_docs/content/doc/filters/js/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,26 @@ The return value will be a JS object containing the following fields:
|------------------------------------------------|---------------------------------------------------------------------------------------------|
| cache.Put(key string, value string, ttl int64) | add the `value` in the cache using the key `key` and it will be deleted after `ttl` seconds |
| cache.Get(key string) | get the value stored in the cache with the key `key` |


### Exec COmmand

This package contains functions for executing system commands and handling system interactions.

#### Return

The return value will be a JS object containing the following fields:

| Name | Type | Description |
|--------|----------|----------------------------------------------------------|
| Status | _BOOL_ | if true the operation was successful |
| Error | _STRING_ | if status is false it contains the reason of the failure |
| Value | _STRING_ | if status is true it contains the standard output |

#### Functions

| Prototype | Description |
|-----------------------------------------------------------|----------------------------------------------------------------------------------------------|
| util.ExecCommand(commandParts []string, inputData string) | Executes the system command defined in `commandParts`. <br> - `commandParts[0]` is the |
| | executable. <br> - `commandParts[1:]` are the arguments. <br> - inputData` is written to the|
| | Standard Input (stdin) of the command. |
Loading