-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.go
39 lines (31 loc) · 929 Bytes
/
execute.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
// Copyright 2020 Serge Léger. All rights reserved.
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// +build linux
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
)
// executeUnsafe executes the command using the default bash interpreter.
func executeUnsafe(cmdStr string) (string, error) {
errOut := strings.Replace(cmdStr, "\n", "; ", -1)
log.Println("execute: ", errOut)
var buf bytes.Buffer
cmd := exec.Command("bash", "-c", cmdStr)
cmd.Stdout = &buf
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("error: could not execute %q: %w", cmdStr, err)
}
return buf.String(), nil
}
// executeSafe never executes the command, it is added to the output wrapped in a code
// block. It is the default mode.
func executeSafe(cmdStr string) (string, error) {
log.Println("execute: ", cmdStr)
return "\n```\n" + cmdStr + "\n```\n", nil
}