-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
60 lines (54 loc) · 1.48 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
package main
import (
"flag"
"fmt"
"github.com/andygeiss/esp32-transpiler/transpile"
"os"
)
func main() {
source, target := getFlags()
checkFlagsAreValid(source, target)
safeTranspile(source, target)
}
func checkFlagsAreValid(source, target string) {
if source == "" || target == "" {
flag.Usage()
os.Exit(1)
}
}
func getFlags() (string, string) {
source := flag.String("source", "", "Golang source file")
target := flag.String("target", "", "Arduino sketch file")
flag.Parse()
return *source, *target
}
func printUsage() {
fmt.Print("This program transpiles Golang source into corresponding Arduino sketches.\n\n")
fmt.Print("Options:\n")
flag.PrintDefaults()
fmt.Print("\n")
fmt.Print("Example:\n")
fmt.Printf("\tesp32 -source impl/blink/controller.go -target impl/blink/controller.transpile\n\n")
}
func safeTranspile(source, target string) {
// Read the Golang source file.
in, err := os.Open(source)
if err != nil {
fmt.Fprintf(os.Stderr, "Go source file [%s] could not be opened! %v", source, err)
os.Exit(1)
}
defer in.Close()
// Create the Arduino sketch file.
os.Remove(target)
out, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_SYNC, 0666)
if err != nil {
fmt.Fprintf(os.Stderr, "Arduino sketch file [%s] could not be opened! %v", target, err)
os.Exit(1)
}
// Transpiles the Golang source into Arduino sketch.
service := transpile.NewService(in, out)
if err := service.Start(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
}