forked from bokwoon95/wgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (76 loc) · 1.64 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
80
81
82
83
84
85
86
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
)
const helptext = `Usage:
wgo [FLAGS] <command> [ARGUMENTS...]
wgo gcc -o main main.c
wgo go build -o main main.go
wgo -file .c gcc -o main main.c
wgo -file=.go go build -o main main.go
wgo run [FLAGS] [GO_BUILD_FLAGS] <package> [ARGUMENTS...]
wgo run main.go
wgo run -file .html main.go arg1 arg2 arg3
wgo run -file .html . arg1 arg2 arg3
wgo run -file=.css -file=.js -tags=fts5 ./cmd/my_project arg1 arg2 arg3
Pass in the -h flag to the wgo/wgo run to learn what flags there are i.e. wgo -h, wgo run -h
Core documentation resides at https://github.com/tmc/wgo#quickstart
`
func main() {
if len(os.Args) == 1 {
fmt.Print(helptext)
return
}
userInterrupt := make(chan os.Signal, 1)
signal.Notify(userInterrupt, syscall.SIGTERM, syscall.SIGINT)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-userInterrupt // Soft interrupt.
cancel()
<-userInterrupt // Hard interrupt.
os.Exit(1)
}()
// Construct the list of WgoCmds from os.Args.
wgoCmds, err := WgoCommands(ctx, os.Args)
if err != nil {
if errors.Is(err, flag.ErrHelp) {
return
}
log.Fatal(err)
}
// Run the WgoCmds in parallel.
results := make(chan error, len(wgoCmds))
var wg sync.WaitGroup
for _, wgoCmd := range wgoCmds {
wgoCmd := wgoCmd
wg.Add(1)
go func() {
defer wg.Done()
results <- wgoCmd.Run()
}()
}
go func() {
wg.Wait()
close(results)
}()
// Wait for results.
ok := true
for err := range results {
if err != nil {
fmt.Println(err)
ok = false
}
}
if !ok {
os.Exit(1)
}
}