-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd.go
More file actions
77 lines (62 loc) · 2.18 KB
/
cmd.go
File metadata and controls
77 lines (62 loc) · 2.18 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
package vera
import (
"bytes"
"os/exec"
"strings"
)
// newCommand returns a "pre-configured" exec.Cmd plus the stdout and stderr buffers used to intercept
// command line errors and output
func newCommand(args ...Param) (cmd *exec.Cmd, stdout, stderr *bytes.Buffer) {
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}
// create new command with
cmd = exec.Command("veracrypt", genArgs(append([]Param{textOnly, nonInteractive}, args...))...)
cmd.Stdout = stdout
cmd.Stderr = stderr
return
}
// ExecCommandWithStdin adds a string to the stdin of a command, executes the command and returns a buffer with the
// console output and eventually an error, if an error was encountered.
func ExecCommandWithStdin(stdin string, args ...Param) (stdout *bytes.Buffer, err error) {
cmd, stdout, stderr := newCommand(args...)
cmd.Stdin = strings.NewReader(stdin)
if err = cmd.Run(); err != nil {
return stdout, parseError(stderr.String())
}
return stdout, nil
}
// ExecCommand executes a simple command and returns a buffer with the console output and eventually an
// error, if an error was encountered.
func ExecCommand(args ...Param) (stdout *bytes.Buffer, err error) {
cmd, stdout, stderr := newCommand(args...)
if err = cmd.Run(); err != nil {
return stdout, parseError(stderr.String())
}
return stdout, nil
}
// genArgs takes a slice of Param structs and generates a slice of strings to pass to the cmd instance
func genArgs(p []Param) []string {
// add the two required parameters
var args []string
var params []string
for _, param := range p {
if param.Name == trueCrypt.Name || param.Name == "tc" {
// prepend --truecrypt to avoid any issues, see "veracrypt --help" for more details
params = append([]string{param.String()}, params...)
continue
}
// add cli arguments to separate list
if param.Name == "" {
args = append(args, param.Value)
} else {
str := param.String()
if len(param.Name) < 3 && strings.Contains(str, " ") {
params = append(params, strings.Split(str, " ")...)
} else {
params = append(params, str)
}
}
}
// create a new list with the flags and parameters at the beginning and the args at the end
return append(params, args...)
}