-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun.go
More file actions
63 lines (55 loc) · 1.63 KB
/
run.go
File metadata and controls
63 lines (55 loc) · 1.63 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
package cmd
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/privateerproj/privateer-sdk/command"
)
// runCmd represents the run command, which executes all plugins specified in the configuration.
var runCmd = &cobra.Command{
Use: "run",
Short: "Run plugins that have been specified in the config.",
Long: `
When everything is battoned down, it is time to run forth.`,
Run: func(cmd *cobra.Command, args []string) {
logger.Trace("run called")
if len(args) > 0 {
logger.Error(fmt.Sprintf(
"Unknown args: %v", args))
} else {
exitCode := Run()
os.Exit(int(exitCode))
}
},
}
func init() {
rootCmd.AddCommand(runCmd)
}
// Run executes all plugins with handling for the command line.
// It sets up signal handlers for graceful shutdown and returns the exit code
// from the plugin execution.
//
// Returns:
// - exitCode: The exit code from the plugin execution (0 for success, non-zero for failure)
func Run() (exitCode int) {
// Setup for handling SIGTERM (Ctrl+C)
setupCloseHandler()
return command.Run(logger, command.GetInstalledPlugins)
}
// setupCloseHandler creates a signal listener on a new goroutine which will notify
// the program if it receives an interrupt from the OS (SIGINT or SIGTERM).
// When an interrupt is received, it logs an error message and exits with the
// Aborted exit code.
//
// Reference: https://golangcode.com/handle-ctrl-c-exit-in-terminal/
func setupCloseHandler() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
logger.Error("Test execution was aborted by user")
os.Exit(int(command.Aborted))
}()
}