Skip to content

Commit d541db7

Browse files
authored
Merge pull request #12 from PouuleT/refactoring
Refactoring
2 parents 9692efa + 7629c2d commit d541db7

File tree

4 files changed

+318
-245
lines changed

4 files changed

+318
-245
lines changed

exec.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
)
9+
10+
func execCmd(cmdString string) error {
11+
// Parse the command to execute it
12+
cmdElmnts := strings.Split(cmdString, " ")
13+
if len(cmdElmnts) == 0 {
14+
return fmt.Errorf("no cmd given")
15+
}
16+
17+
// Get the current working directory
18+
pwd, err := os.Getwd()
19+
if err != nil {
20+
log.Warnf("couldn't get current working directory")
21+
return err
22+
}
23+
24+
// Lookup the full path of the binary to be executed
25+
bin, err := exec.LookPath(cmdElmnts[0])
26+
if err != nil {
27+
log.Warnf("Failed to find bin %s", cmdElmnts[0])
28+
return err
29+
}
30+
31+
// Pass stdin / stdout / stderr as proc attributes
32+
procAttr := os.ProcAttr{
33+
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
34+
Dir: pwd,
35+
}
36+
37+
log.Debugf("Going to run `%s ( %s ) %s`", cmdElmnts[0], bin, strings.Join(cmdElmnts[1:], " "))
38+
39+
// Start the process
40+
proc, err = os.StartProcess(bin, cmdElmnts, &procAttr)
41+
if err != nil {
42+
log.Warnf("Failed to start process")
43+
return err
44+
}
45+
46+
// Wait until the end
47+
state, err := proc.Wait()
48+
if err != nil {
49+
log.Warnf("Error while waiting for proc")
50+
return err
51+
}
52+
53+
log.Debugf("Result : %s", state)
54+
55+
return nil
56+
}

0 commit comments

Comments
 (0)