scpt/defaults.go

54 lines
1.2 KiB
Go
Raw Normal View History

package scpt
import (
"errors"
"fmt"
"github.com/gen2brain/dlgs"
"os"
"os/exec"
)
func displayDialog(args map[string]interface{}) (interface{}, error) {
title, ok := args["title"]
if !ok {
return nil, errors.New("title not provided")
}
text, ok := args[""]
if !ok {
return nil, errors.New("text not provided")
}
switch args["type"] {
case "yesno":
return dlgs.Question(fmt.Sprint(title), fmt.Sprint(text), true)
case "info":
return dlgs.Info(fmt.Sprint(title), fmt.Sprint(text))
case "error":
return dlgs.Error(fmt.Sprint(title), fmt.Sprint(text))
case "entry":
defaultText, ok := args["default"]
if !ok {
defaultText = ""
}
input, _, err := dlgs.Entry(fmt.Sprint(title), fmt.Sprint(text), fmt.Sprint(defaultText))
return input, err
default:
return nil, fmt.Errorf("unknown dialog type: %v", args["type"])
}
}
func doShellScript(args map[string]interface{}) (interface{}, error) {
script, ok := args[""].(string)
if ok {
cmd := exec.Command("sh", "-c", script)
cmd.Stdout = os.Stdout
_ = cmd.Run()
return "", nil
} else {
return nil, errors.New("script not provided")
}
}
func print(args map[string]interface{}) (interface{}, error) {
fmt.Println(args)
return nil, nil
}