-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (137 loc) · 3.72 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
)
var dbg *log.Logger
func main() {
args := flag.NewFlagSet("goscr", flag.ContinueOnError)
var debug bool
var keep bool
var force bool
var code string
var imports []string
args.BoolVar(&debug, "d", false, "Enable debug logging")
args.BoolVar(&keep, "k", false, "Keep temporary files even on compilation error")
args.BoolVar(&force, "f", false, "Force rebuild even if code is already compiled")
args.StringVar(&code, "c", "", "Pass code on the command line instead of script file")
args.Func("i", "Import hint (can specify multiple times)", func(i string) error {
imports = append(imports, i)
return nil
})
if err := args.Parse(os.Args[1:]); err != nil {
fmt.Println("Pass script file (if not using -c) and script args after the above flags")
os.Exit(1)
}
if debug {
dbg = log.New(os.Stderr, "debug ", log.LstdFlags)
} else {
dbg = log.New(io.Discard, "debug ", log.LstdFlags)
}
var runArgs []string
if code != "" {
dbg.Println("Using code from command line")
runArgs = append([]string{"goscr"}, args.Args()...)
} else {
if len(args.Args()) == 0 {
args.Usage()
fmt.Println("Pass script file (if not using -c) and script args after the above flags")
os.Exit(1)
}
dbg.Println("Reading code from", args.Arg(0))
codeRaw, err := os.ReadFile(args.Arg(0))
if err != nil {
log.Fatalln(err)
}
code = StripShebang(string(codeRaw))
runArgs = args.Args()
}
hash, err := HashAndCreateIfNeeded(code, keep, force, imports)
if err != nil {
log.Fatalln(err)
}
err = RunProgram(hash, runArgs)
if err != nil {
log.Fatalln(err)
}
}
// HashAndCreateIfNeeded will check if we have already compiled the provided code and compile it if not.
// A hash of the code is returned for use later.
func HashAndCreateIfNeeded(code string, keep, force bool, imports []string) (string, error) {
// Check if already compiled
hash := Hash(code)
dbg.Println("Code hash is", hash)
// Grab read lock for check
rd, err := LockRead(hash)
if err != nil {
return "", err
}
defer rd.Unlock()
basedir, err := Workdir()
if err != nil {
return "", err
}
workdir := filepath.Join(basedir, hash)
exists, err := DirExists(workdir)
if err != nil {
return "", err
}
if force || !exists {
// Upgrade to write lock
rd.Unlock()
wr, err := LockWrite(hash)
if err != nil {
return "", err
}
defer wr.Unlock()
if force && exists {
dbg.Println("Removing existing dir", workdir)
os.RemoveAll(workdir)
}
dbg.Println("Creating code in", workdir, "with imports", imports)
err = Create(code, imports, workdir)
if err != nil {
os.RemoveAll(workdir) // Cleanup as something failed
return "", fmt.Errorf("failed to create code compilation direcory: %s", err)
}
dbg.Println("Compiling code")
err = Compile(workdir)
if err != nil {
if !keep {
os.RemoveAll(workdir) // Cleanup as something failed
}
return "", fmt.Errorf("failed to compile code: %s", err)
}
}
return hash, nil
}
// RunProgram executes the compiled program for a script with the provided hash
func RunProgram(hash string, args []string) error {
lck, err := LockRead(hash)
if err != nil {
return err
}
defer lck.Unlock()
basedir, err := Workdir()
if err != nil {
return err
}
workdir := filepath.Join(basedir, hash)
binary := filepath.Join(workdir, "goscr")
dbg.Println("Executing", binary, "with args", args)
cmd := exec.Command(binary, args...)
cmd.Args = cmd.Args[1:] // Our args already has in it what we wish to pass as the binary name
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed to execute compiled script in %s: %s", workdir, err)
}
return nil
}