-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript_run.go
More file actions
100 lines (86 loc) · 2.92 KB
/
script_run.go
File metadata and controls
100 lines (86 loc) · 2.92 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
package script
import (
"os"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
stscobra "github.com/stackvista/stackstate-cli/internal/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stackvista/stackstate-cli/internal/util"
)
type ScriptRunArgs struct {
Script string
ArgumentsScript string
TimeoutMs int32
ScriptFile string
}
const (
ScriptFlag = "script"
TimeoutFlag = "timeout"
ArgumentsScriptFlag = "arguments-script"
)
func ScriptRunCommand(cli *di.Deps) *cobra.Command {
args := &ScriptRunArgs{}
cmd := &cobra.Command{
Use: "run",
Short: "Execute an STSL script on the server",
Long: "Execute an STSL (SUSE Observability Scripting Language) script on the server. Scripts can be provided inline or loaded from a file. Use --arguments-script to pass variables to the script.",
Example: `# run a script from file
sts script run --file "path/to/my.script"
# run a script with variables provided by an arguments-script
sts script run --script "x+y" --arguments-script "[x: 1, y: 2]"`,
RunE: cli.CmdRunEWithApi(RunScriptRunCommand(args)),
}
cmd.Flags().StringVar(&args.Script, ScriptFlag, "", "A script to run")
cmd.Flags().StringVar(&args.ArgumentsScript, ArgumentsScriptFlag, "",
"An extra script that generates arguments to be used as variables when the main script is executed, return format: java.util.Map",
)
cmd.Flags().Int32VarP(&args.TimeoutMs, TimeoutFlag, "t", 0, "Timeout in milli-seconds for script execution")
common.AddFileFlagVar(cmd, &args.ScriptFile, "Path to a file containing the script to run")
stscobra.MarkMutexFlags(cmd, []string{ScriptFlag, common.FileFlag}, "input", true)
return cmd
}
func RunScriptRunCommand(args *ScriptRunArgs) di.CmdWithApiFn {
return func(
cmd *cobra.Command,
cli *di.Deps,
api *stackstate_api.APIClient,
serverInfo *stackstate_api.ServerInfo,
) common.CLIError {
script := args.Script
if args.ScriptFile != "" {
b, err := os.ReadFile(args.ScriptFile)
if err != nil {
return common.NewReadFileError(err, args.ScriptFile)
}
script = string(b)
}
// execute script
scriptRequest := stackstate_api.ExecuteScriptRequest{
TimeoutMs: util.Int32NilP(args.TimeoutMs),
Script: script,
ArgumentsScript: util.StringNilP(args.ArgumentsScript),
}
scriptResponse, resp, err := api.ScriptingApi.
ScriptExecute(cli.Context).
ExecuteScriptRequest(scriptRequest).
Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
// print response
value := scriptResponse.Result["value"]
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"result": scriptResponse.Result,
})
} else {
if value == nil {
cli.Printer.Success("script executed (no response)")
} else {
cli.Printer.PrintStruct(value)
}
}
return nil
}
}