-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathcmd.go
More file actions
79 lines (69 loc) · 1.59 KB
/
Copy pathcmd.go
File metadata and controls
79 lines (69 loc) · 1.59 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
package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/chainreactors/logs"
"github.com/chainreactors/spray/core"
"github.com/chainreactors/spray/pkg"
"github.com/jessevdk/go-flags"
)
var ver = "dev"
var DefaultConfig = "config.yaml"
func init() {
logs.Log.SetColorMap(map[logs.Level]func(string) string{
logs.InfoLevel: logs.PurpleBold,
logs.ImportantLevel: logs.GreenBold,
pkg.LogVerbose: logs.Green,
})
}
func Spray() {
var option core.Option
parser := flags.NewParser(&option, flags.Default)
parser.Usage = core.Usage()
_, err := parser.ParseArgs(os.Args[1:])
if err != nil {
if err.(*flags.Error).Type != flags.ErrHelp {
fmt.Println(err.Error())
}
return
}
defer time.Sleep(time.Second)
ctx, canceler := context.WithTimeout(context.Background(), time.Duration(option.Deadline)*time.Second)
go func() {
select {
case <-ctx.Done():
time.Sleep(10 * time.Second)
logs.Log.Errorf("deadline and timeout not work, hard exit!!!")
os.Exit(0)
}
}()
go func() {
exitChan := make(chan os.Signal, 2)
signal.Notify(exitChan, os.Interrupt, syscall.SIGTERM)
go func() {
sigCount := 0
for {
<-exitChan
sigCount++
if sigCount == 1 {
logs.Log.Infof("Exit signal received, saving task and exiting...")
canceler()
} else if sigCount == 2 {
logs.Log.Infof("forcing exit...")
os.Exit(1)
}
}
}()
}()
if err := core.RunWithArgs(ctx, os.Args[1:], core.RunOptions{
DefaultConfig: DefaultConfig,
Version: ver,
}); err != nil {
logs.Log.Errorf("%s", err.Error())
return
}
}