/
capture_local.go
67 lines (58 loc) · 2.08 KB
/
capture_local.go
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
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package starlark
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/vladimirvivien/gexe"
"go.starlark.net/starlark"
)
// captureLocalFunc is a built-in starlark function that runs a provided command on the local machine.
// The output of the command is stored in a file at a specified location under the workdir directory.
// Starlark format: capture_local(cmd=<command> [,workdir=path][,file_name=name][,desc=description][,append=append])
func captureLocalFunc(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var cmdStr, workdir, fileName, desc string
var append bool
if err := starlark.UnpackArgs(
identifiers.captureLocal, args, kwargs,
"cmd", &cmdStr,
"workdir?", &workdir,
"file_name?", &fileName,
"desc?", &desc,
"append?", &append,
); err != nil {
return starlark.None, fmt.Errorf("%s: %s", identifiers.captureLocal, err)
}
if len(workdir) == 0 {
dir, err := getWorkdirFromThread(thread)
if err != nil {
return starlark.None, err
}
workdir = dir
}
if len(fileName) == 0 {
fileName = fmt.Sprintf("%s.txt", sanitizeStr(cmdStr))
}
filePath := filepath.Join(workdir, fileName)
if err := os.MkdirAll(workdir, 0744); err != nil && !os.IsExist(err) {
msg := fmt.Sprintf("%s error: %s", identifiers.captureLocal, err)
return starlark.String(msg), nil
}
p := gexe.StartProc(cmdStr)
// upon error, write error in file, return filepath
if p.Err() != nil {
msg := fmt.Sprintf("%s error: %s: %s", identifiers.captureLocal, p.Err(), p.Result())
if err := captureOutput(strings.NewReader(msg), filePath, desc, append); err != nil {
msg := fmt.Sprintf("%s error: %s", identifiers.captureLocal, err)
return starlark.String(msg), nil
}
return starlark.String(filePath), nil
}
if err := captureOutput(p.Out(), filePath, desc, append); err != nil {
msg := fmt.Sprintf("%s error: %s", identifiers.captureLocal, err)
return starlark.String(msg), nil
}
return starlark.String(filePath), nil
}