add TestMode to run actions without effect

This commit is contained in:
2023-08-16 01:27:28 +02:00
parent 381cc31d4b
commit 8f6fc72bfb
7 changed files with 56 additions and 20 deletions

View File

@@ -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{},
}
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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"}
}

View File

@@ -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 {