-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
55 lines (50 loc) · 1015 Bytes
/
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
package main
import (
"flag"
"fmt"
"io"
"os"
"github.com/elves/upgrade-scripts-for-0.17/fix"
"src.elv.sh/pkg/diag"
"src.elv.sh/pkg/parse"
)
var (
rewrite = flag.Bool("w", false, "rewrite files")
lambda = flag.Bool("lambda", true, "migrate lambda syntax")
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
process("[stdin]", os.Stdin, os.Stdout)
} else {
for _, arg := range args {
f, err := os.OpenFile(arg, os.O_RDWR, 0)
if err != nil {
diag.ShowError(os.Stderr, err)
continue
}
w := os.Stdout
if *rewrite {
w = f
}
process(arg, f, w)
}
}
}
func process(name string, r io.Reader, w io.Writer) {
code, err := io.ReadAll(r)
if err != nil {
diag.ShowError(os.Stderr, err)
return
}
fixed, err := fix.Fix(parse.Source{Name: name, Code: string(code)}, fix.Opts{MigrateLambda: *lambda})
if err != nil {
diag.ShowError(os.Stderr, err)
return
}
if s, ok := w.(io.Seeker); ok {
s.Seek(0, io.SeekStart)
}
fmt.Fprint(w, fixed)
}