-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (101 loc) · 2.18 KB
/
main.go
File metadata and controls
117 lines (101 loc) · 2.18 KB
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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
)
type Resolved struct {
Key CacheKey
Binary string
WorkDir string
}
func main() {
cfg := ParseArgs(os.Args)
if cfg.Action == ActionClearCache {
clearCache()
return
}
content, _, err := readContent(cfg)
if err != nil {
fatal(err)
}
if cfg.Action == ActionExplain {
fmt.Print(string(content))
return
}
resolved, err := resolve(content, cfg.NoCache)
if err != nil {
fatal(err)
}
switch cfg.Action {
case ActionBuild:
if err := copyBinary(resolved.Binary, cfg.OutputPath); err != nil {
fatal(err)
}
fmt.Printf("Compiled into %s\n", cfg.OutputPath)
case ActionMigrate:
if err := migrateScript(resolved, cfg.MigrateDir); err != nil {
fatal(err)
}
fmt.Printf("Migrated to %s\n", cfg.MigrateDir)
case ActionRun:
RunAndExit(resolved.Binary, cfg.Args)
}
}
func fatal(err error) {
fmt.Fprintln(os.Stderr, "goscript:", err)
os.Exit(1)
}
// readContent reads/generates the Go source for the given config.
// Returns the source bytes and the detected InlineMode (for informational use).
func readContent(cfg Config) ([]byte, InlineMode, error) {
if cfg.Input == InputInline {
src, mode, err := InlineToScript(cfg.InlineCode, cfg.FieldSep, cfg.Parallel, cfg.Regex)
return src, mode, err
}
abs, err := filepath.Abs(cfg.ScriptPath)
if err != nil {
return nil, ModeSimple, err
}
raw, err := os.ReadFile(abs)
if err != nil {
return nil, ModeSimple, err
}
return raw, ModeSimple, nil
}
func resolve(raw []byte, noCache bool) (*Resolved, error) {
key := HashContent(raw)
if !noCache {
r, hit, err := LookupCache(key)
if err != nil {
return nil, err
}
if hit {
return r, nil
}
}
return PrepareScript(key, raw)
}
func copyBinary(src string, dst string) error {
var perms = os.O_CREATE | os.O_WRONLY | os.O_TRUNC
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(dst, perms, 0o755)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
func clearCache() {
dir := filepath.Join(cacheRoot(), "goscript")
if err := os.RemoveAll(dir); err != nil {
fatal(err)
}
fmt.Println("Cache cleared.")
}