-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
116 lines (95 loc) · 2.27 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"io/ioutil"
"path"
"time"
"os"
"os/exec"
"os/signal"
"errors"
"github.com/mitchellh/go-homedir"
)
var version string
var commit string
var payloadDir string
func delay(fn func(), delay time.Duration) chan<- bool {
cancel := make(chan bool, 1)
go func () {
wait := make(chan bool)
go func () {
time.Sleep(delay)
wait <- true
close(wait)
}()
select {
case <-wait:
fn()
case <-cancel:
}
}()
return cancel
}
func deployRuntime() (string, error) {
if len(payloadDir) == 0 {
return "", errors.New("Invalid payload directory.")
}
home, err := homedir.Dir()
if err != nil {
return "", err
}
runxHome := path.Join(home, ".runx")
err = os.Mkdir(runxHome, 0700)
if err != nil && !os.IsExist(err) {
return "", err
}
files, err := ioutil.ReadDir(runxHome)
if err != nil {
return "", err
}
for _, file := range files {
if file.IsDir() && file.Name() != payloadDir {
remove := path.Join(runxHome, file.Name())
os.RemoveAll(remove)
}
}
dir := path.Join(runxHome, payloadDir)
err = os.Mkdir(dir, 0700)
if os.IsExist(err) {
return dir, nil
}
cancel := delay(func () {
log.Println("Preparing for first use.")
}, 500 * time.Millisecond)
err = RestoreAssets(dir, "runtime")
cancel <- true
if err != nil {
return "", err
}
return dir, nil
}
func main() {
// We exclude the first argument since it's just the current process path.
args := os.Args[1:]
if len(args) == 1 && (args[0] == "-v" || args[0] == "--version") {
fmt.Fprintln(os.Stderr, "runx", version, commit)
return
}
dir, err := deployRuntime()
if err != nil {
log.Fatal(err)
return
}
ruby := setupRuntime(dir)
script := path.Join(dir, "runtime", "lib", "app", "runx.rb")
args = append([]string{script}, args...)
// Disable all default signal behavior (e.g. SIGINT)
// in case child process has specific signal handling.
signals := make(chan os.Signal, 1)
signal.Notify(signals)
cmd := exec.Command(ruby, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Run()
}