-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcommon_flags.go
More file actions
52 lines (44 loc) · 1.43 KB
/
common_flags.go
File metadata and controls
52 lines (44 loc) · 1.43 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
package goflags
import (
"time"
"github.com/projectdiscovery/utils/process"
)
// CommonFlags contains common flags shared across ProjectDiscovery tools.
// These flags provide consistent behavior across all tools in the ecosystem.
type CommonFlags struct {
// MaxTime is the maximum duration for the entire execution.
// When this duration is reached, an interrupt signal is sent to trigger graceful termination.
// Example values: "1h", "30m", "1h30m", "2h45m30s"
MaxTime time.Duration
}
// AddCommonFlags registers common flags to the flagset and returns a CommonFlags struct.
// The handlers are automatically started after Parse() is called.
//
// Usage:
//
// flagSet := goflags.NewFlagSet()
// flagSet.AddCommonFlags()
// flagSet.Parse()
func (flagSet *FlagSet) AddCommonFlags() *CommonFlags {
cf := &CommonFlags{}
flagSet.CreateGroup("common", "Common",
flagSet.DurationVarP(&cf.MaxTime, "max-time", "mt", 0, "maximum time to run before automatic termination (e.g., 1h, 30m)"),
)
flagSet.commonFlags = cf
return cf
}
// startCommonFlagsHandlers is called by Parse() to start handlers.
func (flagSet *FlagSet) startCommonFlagsHandlers() {
if flagSet.commonFlags != nil {
flagSet.commonFlags.startMaxTimeHandler()
}
}
// startMaxTimeHandler starts the max time handler if MaxTime is set.
func (cf *CommonFlags) startMaxTimeHandler() {
if cf.MaxTime > 0 {
go func() {
<-time.After(cf.MaxTime)
process.SendInterrupt()
}()
}
}