-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
79 lines (65 loc) · 1.98 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
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/alexflint/go-arg"
)
var version string // to be set by compiler
var args struct {
File string `arg:"positional" help:"if not specified, will read from stdin" default:""`
Outfile string `arg:"positional" help:"if not specified, will emit to stdout" default:""`
Version bool `arg:"-v" help:"print version and exit"`
YieldDepsOnly bool `arg:"--yield-deps-only" help:"print dependencies as a JSON array and exit" default:false`
ExposeDeps bool `arg:"--expose-deps" help:"expose dependencies to program" default:false`
DepsVarName string `arg:"--deps-var-name" help:"override deps variable name" default:"deps"`
IgnoreShebang bool `arg:"--ignore-shebang" help:"ignore shebang requirement" default:false`
}
func main() {
arg.MustParse(&args)
if args.Version {
fmt.Println(version)
os.Exit(0)
}
var file []byte
if (args.File != "") && (args.File != "-") {
f, err := os.ReadFile(args.File)
if err != nil {
if os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "File %s does not exist!", args.File)
os.Exit(1)
}
panic(err)
}
file = f
} else {
data, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Printf("Coudln't read from stdin: %s", err.Error())
os.Exit(1)
}
file = data
}
code := string(file)
found, err := find(code)
if err != nil {
panic(err)
}
codelines := strings.Split(code, "\n")
if len(codelines) < 2 {
fmt.Fprintf(os.Stderr, "The code must have at least two lines!\n")
os.Exit(3)
}
shebang := codelines[0]
if !((shebang == "#!/bin/bash") || (shebang == "#!/usr/bin/env bash")) && (!args.IgnoreShebang) {
fmt.Fprintf(os.Stderr, "The code must start with one of those shebangs: #!/bin/bash OR #!/usr/bin/env bash\n")
os.Exit(2)
}
gen := shebang + "\n\n" + gencode(found) + "\n\n" + strings.Join(codelines[1:], "\n")
if args.Outfile == "" {
fmt.Printf("%s", gen)
} else {
os.WriteFile(args.Outfile, []byte(gen), os.FileMode(0o755))
}
}