-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
45 lines (41 loc) · 808 Bytes
/
main.go
File metadata and controls
45 lines (41 loc) · 808 Bytes
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"syscall"
)
func main() {
if len(os.Args) == 1 {
fmt.Fprintln(os.Stderr, "Usage: ifne [-n] command [args]")
os.Exit(255)
}
do := true
if os.Args[1] == "-n" {
do = false
os.Args = os.Args[:1+copy(os.Args[1:], os.Args[2:])]
}
b, _ := ioutil.ReadAll(os.Stdin)
if (do == true && len(b) == 0) || (do == false && len(b) > 0) {
return
}
cmd := exec.Command(os.Args[1], os.Args[2:]...)
cmd.Stdin = bytes.NewBuffer(b)
b, err := cmd.CombinedOutput()
if err == nil {
return
}
fmt.Fprintf(os.Stderr, "%s: %s\n", err, string(b))
status := 1
state := cmd.ProcessState
if state != nil {
if ws, ok := state.Sys().(syscall.WaitStatus); ok {
if ws.Exited() {
status = ws.ExitStatus()
}
}
}
os.Exit(status)
}