diff --git a/actions/actions.go b/actions/actions.go index d581f2f..3ad78a9 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -9,6 +9,7 @@ import ( type Action interface { Execute() + DryExecute() } type Stage struct { @@ -21,7 +22,7 @@ type StagedActions struct { Stages []Stage } -func (a StagedActions) Execute() { +func (a StagedActions) executeInternal(f func(Action)) { for idx, stage := range a.Stages { if idx < a.StageCount { continue @@ -29,7 +30,7 @@ func (a StagedActions) Execute() { fmt.Printf("Execute Stage %v\n", idx+1) for actionidx, _ := range stage.Actions { - go stage.Actions[actionidx].Execute() + go f(stage.Actions[actionidx]) } for range stage.Actions { @@ -38,6 +39,25 @@ func (a StagedActions) Execute() { } } +var TestRun bool + +func Fire(a Action) { + if TestRun { + a.DryExecute() + return + } + + a.Execute() +} + +func (a StagedActions) DryExecute() { + a.executeInternal(func(a Action) { a.DryExecute() }) +} + +func (a StagedActions) Execute() { + a.executeInternal(func(a Action) { a.Execute() }) +} + func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) { if config.Type == "Print" { return NewPrint(config, c) @@ -47,6 +67,10 @@ func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) return NewTimeOut(config, c) } + if config.Type == "Command" { + return NewCommand(config, c) + } + if config.Type == "Shutdown" { return NewShutdown(config, c) } @@ -91,6 +115,7 @@ func GetDocumenters() []internal.Documenter { return []internal.Documenter{ Printer{}, TimeOut{}, + Command{}, Shutdown{}, } } diff --git a/actions/printer.go b/actions/printer.go index e436743..ee456ab 100644 --- a/actions/printer.go +++ b/actions/printer.go @@ -17,6 +17,11 @@ func (p Printer) Execute() { p.ActionChan <- true } +func (p Printer) DryExecute() { + fmt.Printf("Print action fire test. Message: %s", p.Message) + p.ActionChan <- true +} + func NewPrint(config internal.ActionConfig, c chan bool) (Action, error) { var result Printer err := json.Unmarshal(config.Options, &result) diff --git a/actions/shutdown.go b/actions/shutdown.go index 67c2e81..262f418 100644 --- a/actions/shutdown.go +++ b/actions/shutdown.go @@ -11,6 +11,13 @@ type Shutdown struct { ActionChan chan bool } +func (c Shutdown) DryExecute() { + fmt.Println("Test Shutdown executed...") + + c.ActionChan <- true + +} + func (c Shutdown) Execute() { if err := exec.Command("shutdown", "-h", "now").Run(); err != nil { fmt.Println("Failed to initiate shutdown:", err) diff --git a/actions/timeout.go b/actions/timeout.go index eb84ae9..6d7dc85 100644 --- a/actions/timeout.go +++ b/actions/timeout.go @@ -13,6 +13,10 @@ type TimeOut struct { ActionChan chan bool } +func (t TimeOut) DryExecute() { + t.Execute() +} + func (t TimeOut) Execute() { fmt.Printf("Waiting %d seconds\n", t.Duration/time.Second) time.Sleep(t.Duration) diff --git a/gokill.go b/gokill.go index c14728f..0b8aabb 100644 --- a/gokill.go +++ b/gokill.go @@ -40,6 +40,8 @@ func GetDocumentation() string { func main() { configFilePath := flag.String("c", "", "path to config file") showDoc := flag.Bool("d", false, "show doc") + testRun := flag.Bool("t", false, "test run") + flag.Parse() if *showDoc { @@ -52,6 +54,8 @@ func main() { return } + actions.TestRun = *testRun + configFile, err := os.ReadFile(*configFilePath) if err != nil { diff --git a/triggers/ethernet.go b/triggers/ethernet.go index 1041c3b..6fe530a 100644 --- a/triggers/ethernet.go +++ b/triggers/ethernet.go @@ -25,7 +25,6 @@ func isEthernetConnected(deviceName string) bool { } if string(content[:4]) == "down" { - fmt.Println("Ethernet is disconnected") return false } @@ -33,25 +32,21 @@ func isEthernetConnected(deviceName string) bool { } func (t EthernetDisconnect) Listen() { - fmt.Println("EthernetDisconnect listens") - if t.WaitTillConnected { - for !isEthernetConnected("enp0s31f6") { + for !isEthernetConnected(t.InterfaceName) { time.Sleep(1 * time.Second) } } for { - if !isEthernetConnected("enp0s31f6") { - fmt.Println("Ethernet is disconnected") + if !isEthernetConnected(t.InterfaceName) { break } time.Sleep(1 * time.Second) } - fmt.Println("EthernetDisconnect fires") - t.action.Execute() + actions.Fire(t.action) } // func NewTimeOut(d time.Duration, action actions.Action) EthernetDisconnect { @@ -60,17 +55,12 @@ func NewEthernetDisconnect(config internal.KillSwitchConfig) (EthernetDisconnect WaitTillConnected: true, } - fmt.Println(string(config.Options)) err := json.Unmarshal(config.Options, &result) - fmt.Println(result) - if err != nil { return EthernetDisconnect{}, err } - fmt.Println(result.InterfaceName) - if result.InterfaceName == "" { return EthernetDisconnect{}, internal.OptionMissingError{"interfaceName"} } diff --git a/triggers/timeout.go b/triggers/timeout.go index 9b02a33..5e4e171 100644 --- a/triggers/timeout.go +++ b/triggers/timeout.go @@ -10,19 +10,20 @@ import ( ) type TimeOut struct { - d time.Duration - action actions.Action + Duration int + action actions.Action } func (t TimeOut) Listen() { fmt.Println("TimeOut listens") - time.Sleep(t.d) + fmt.Println(t.Duration) + time.Sleep(time.Duration(t.Duration) * time.Second) fmt.Println("TimeOut fires") - t.action.Execute() + actions.Fire(t.action) } func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) { - result := TimeOut{d: 0 * time.Second} + var result TimeOut err := json.Unmarshal(config.Options, &result) if err != nil {