2021-03-01 21:43:10 +00:00
|
|
|
package scpt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gen2brain/dlgs"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2021-03-01 23:01:21 +00:00
|
|
|
// Default function to display a dialog
|
2021-03-01 21:43:10 +00:00
|
|
|
func displayDialog(args map[string]interface{}) (interface{}, error) {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Get title
|
2021-03-01 21:43:10 +00:00
|
|
|
title, ok := args["title"]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("title not provided")
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Get unnamed argument as text
|
2021-03-01 21:43:10 +00:00
|
|
|
text, ok := args[""]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("text not provided")
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Display correct dialog based on given type
|
2021-03-01 21:43:10 +00:00
|
|
|
switch args["type"] {
|
|
|
|
case "yesno":
|
2021-03-01 23:01:21 +00:00
|
|
|
// Display yes or no dialog, returning bool based on user input
|
2021-03-01 21:43:10 +00:00
|
|
|
return dlgs.Question(fmt.Sprint(title), fmt.Sprint(text), true)
|
|
|
|
case "info":
|
2021-03-01 23:01:21 +00:00
|
|
|
// Display info dialog, returning bool based on success
|
2021-03-01 21:43:10 +00:00
|
|
|
return dlgs.Info(fmt.Sprint(title), fmt.Sprint(text))
|
|
|
|
case "error":
|
2021-03-01 23:01:21 +00:00
|
|
|
// Display error dialog, returning bool based on success
|
2021-03-01 21:43:10 +00:00
|
|
|
return dlgs.Error(fmt.Sprint(title), fmt.Sprint(text))
|
|
|
|
case "entry":
|
2021-03-01 23:01:21 +00:00
|
|
|
// Check if default text given
|
2021-03-01 21:43:10 +00:00
|
|
|
defaultText, ok := args["default"]
|
|
|
|
if !ok {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Set to empty if not given
|
2021-03-01 21:43:10 +00:00
|
|
|
defaultText = ""
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Display entry dialog
|
2021-03-01 21:43:10 +00:00
|
|
|
input, _, err := dlgs.Entry(fmt.Sprint(title), fmt.Sprint(text), fmt.Sprint(defaultText))
|
2021-03-01 23:01:21 +00:00
|
|
|
// Return user input
|
2021-03-01 21:43:10 +00:00
|
|
|
return input, err
|
|
|
|
default:
|
2021-03-01 23:01:21 +00:00
|
|
|
// If type unknown, return error
|
2021-03-01 21:43:10 +00:00
|
|
|
return nil, fmt.Errorf("unknown dialog type: %v", args["type"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:01:21 +00:00
|
|
|
// Default function to run a shell script using `sh -c`
|
2021-03-01 21:43:10 +00:00
|
|
|
func doShellScript(args map[string]interface{}) (interface{}, error) {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Get unnamed argument and assert its type as string
|
2021-03-01 21:43:10 +00:00
|
|
|
script, ok := args[""].(string)
|
2021-03-01 23:01:21 +00:00
|
|
|
// If assertion successful
|
2021-03-01 21:43:10 +00:00
|
|
|
if ok {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Create new exec.Cmd containing `sh -c <script>`
|
2021-03-01 21:43:10 +00:00
|
|
|
cmd := exec.Command("sh", "-c", script)
|
2021-03-01 23:01:21 +00:00
|
|
|
// Set command I/O
|
2021-03-01 21:43:10 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
2021-03-01 23:01:21 +00:00
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
// Run command
|
2021-03-01 21:43:10 +00:00
|
|
|
_ = cmd.Run()
|
|
|
|
return "", nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("script not provided")
|
|
|
|
}
|
2021-03-02 00:44:33 +00:00
|
|
|
}
|