-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (66 loc) · 1.54 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
package main
import (
"os"
"io"
"fmt"
"log"
"path"
hasht "crypto/sha256" //hash type, changeable
)
var (
dbfilename = path.Join(os.Getenv("HOME"), ".gorundb.gob")
storedir = path.Join(os.Getenv("HOME"), ".gorun")
)
const (
perms = 0644
dirperms = 0755
)
const latestTime = 0x7FFFFFFFFFFFFFFF //Latest possible time with an int64
func main() {
err := os.MkdirAll(storedir, dirperms)
if err != nil {
log.Fatalln(err)
}
if len(os.Args) < 2 { //no filename
log.Fatalln("No filename given")
}
scriptname := os.Args[1]
scriptfile, err := os.Open(scriptname, os.O_RDONLY, 0)
if err != nil {
log.Fatalln(err)
}
defer scriptfile.Close()
hash := hasht.New()
io.Copy(hash, scriptfile) //feed data to hash func
hashstr := fmt.Sprintf("%x", hash.Sum()) //get hash as hex string
table, err := readTable(dbfilename) //get our data ready
if err != nil {
log.Fatalln(err)
}
metadata, ok := table[hashstr] //look for record of scriptfile
if !ok {
err = compile(scriptfile, hashstr)
if err != nil {
log.Fatalln(err)
}
} else {
if _, err = os.Stat(path.Join(storedir, hashstr)); err != nil {
err = compile(scriptfile, hashstr)
if err != nil {
log.Fatalln(err)
}
}
}
metadata.Hash = hashstr
metadata.Lastused, _, err = os.Time()
if err != nil {
log.Println(err)
metadata.Lastused = latestTime
}
metadata.Filename = scriptname
table[hashstr] = metadata
if err := writeTable(table, dbfilename); err != nil {
log.Println(err)
}
os.Exec(path.Join(storedir, hashstr), os.Args[1:], os.Environ())
}