-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatRunner.go
67 lines (63 loc) · 1.47 KB
/
fatRunner.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
var println=fmt.Println
var fatBinDir = os.Getenv("HOME") + "/bin/"
var fatBinName = "goFatBin"
var fatScriptName = "goFatBin.go"
var fatScript = fatBinDir + "/scripts/" + fatScriptName
var fatBin = fatBinDir + fatBinName
func main() {
err := os.MkdirAll(fatBinDir, 770)
check(err)
scriptInfo,err := os.Stat(fatScript)
check(err)
scriptTime := scriptInfo.ModTime();
binInfo,err := os.Stat(fatBin)
var binTime time.Time
if err == nil {
binTime = binInfo.ModTime()
} else if os.IsNotExist(err) {
binTime = scriptTime.Add(-1 * time.Hour)
} else { panic(err) }
if scriptTime.After(binTime) {
//read script
readFp,err := os.Open(fatScript)
check(err)
defer readFp.Close()
scanner := bufio.NewScanner(readFp)
tmpFile,exists := os.LookupEnv("XDG_RUNTIME_DIR")
if exists {
tmpFile = strings.TrimSuffix(tmpFile,"/")
} else {
tmpFile = "/tmp"
}
tmpFile = tmpFile + "/" + fatScriptName
writeFp,err := os.Create(tmpFile)
check(err)
defer writeFp.Close()
scanner.Scan() //firts line is shebang
for scanner.Scan() {
writeFp.WriteString(scanner.Text())
writeFp.WriteString("\n")
}
cmd := "go"
args := []string{ "build", "-o", fatBin, tmpFile}
output,err := exec.Command(cmd, args...).CombinedOutput()
check(err,string(output))
}
syscall.Exec(fatBin, os.Args, os.Environ())
}
func check(e error, output ... string) {
if e != nil {
println(output)
panic(e)
}
}