-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec.go
48 lines (40 loc) · 980 Bytes
/
exec.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
package utils
import (
"fmt"
"github.com/fzbian/server-inquiry/enums"
"github.com/gofiber/fiber/v2"
"os"
"os/exec"
)
/*
Exec run a command in the linux operating system
Parameters:
- string (command): takes the command dictated by the request
Return:
- string: the answer given by the system
- error: if there is an error, it is returned so that when the function is used, it can be reported.
*/
func Exec(c *fiber.Ctx, command string) (string, error) {
//Debian
cmd := exec.Command("/bin/bash", "-c", command)
out, err := cmd.Output()
if err != nil {
if err.Error() == "exit status 127" {
return "", err
} else {
fmt.Println(Problem(enums.CantReadOutputCmd, err))
}
}
// Saves logs
err = Log(c, command)
if err != nil {
fmt.Println(Problem(enums.CantSaveLog, err))
}
// Returns the output in string format
return string(out), nil
}
func ClearTerminal() error {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
return cmd.Run()
}