add TestMode to run actions without effect
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
type Action interface {
|
type Action interface {
|
||||||
Execute()
|
Execute()
|
||||||
|
DryExecute()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stage struct {
|
type Stage struct {
|
||||||
@@ -21,7 +22,7 @@ type StagedActions struct {
|
|||||||
Stages []Stage
|
Stages []Stage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a StagedActions) Execute() {
|
func (a StagedActions) executeInternal(f func(Action)) {
|
||||||
for idx, stage := range a.Stages {
|
for idx, stage := range a.Stages {
|
||||||
if idx < a.StageCount {
|
if idx < a.StageCount {
|
||||||
continue
|
continue
|
||||||
@@ -29,7 +30,7 @@ func (a StagedActions) Execute() {
|
|||||||
|
|
||||||
fmt.Printf("Execute Stage %v\n", idx+1)
|
fmt.Printf("Execute Stage %v\n", idx+1)
|
||||||
for actionidx, _ := range stage.Actions {
|
for actionidx, _ := range stage.Actions {
|
||||||
go stage.Actions[actionidx].Execute()
|
go f(stage.Actions[actionidx])
|
||||||
}
|
}
|
||||||
|
|
||||||
for range stage.Actions {
|
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) {
|
func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||||
if config.Type == "Print" {
|
if config.Type == "Print" {
|
||||||
return NewPrint(config, c)
|
return NewPrint(config, c)
|
||||||
@@ -47,6 +67,10 @@ func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error)
|
|||||||
return NewTimeOut(config, c)
|
return NewTimeOut(config, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.Type == "Command" {
|
||||||
|
return NewCommand(config, c)
|
||||||
|
}
|
||||||
|
|
||||||
if config.Type == "Shutdown" {
|
if config.Type == "Shutdown" {
|
||||||
return NewShutdown(config, c)
|
return NewShutdown(config, c)
|
||||||
}
|
}
|
||||||
@@ -91,6 +115,7 @@ func GetDocumenters() []internal.Documenter {
|
|||||||
return []internal.Documenter{
|
return []internal.Documenter{
|
||||||
Printer{},
|
Printer{},
|
||||||
TimeOut{},
|
TimeOut{},
|
||||||
|
Command{},
|
||||||
Shutdown{},
|
Shutdown{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,11 @@ func (p Printer) Execute() {
|
|||||||
p.ActionChan <- true
|
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) {
|
func NewPrint(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||||
var result Printer
|
var result Printer
|
||||||
err := json.Unmarshal(config.Options, &result)
|
err := json.Unmarshal(config.Options, &result)
|
||||||
|
|||||||
@@ -11,6 +11,13 @@ type Shutdown struct {
|
|||||||
ActionChan chan bool
|
ActionChan chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Shutdown) DryExecute() {
|
||||||
|
fmt.Println("Test Shutdown executed...")
|
||||||
|
|
||||||
|
c.ActionChan <- true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (c Shutdown) Execute() {
|
func (c Shutdown) Execute() {
|
||||||
if err := exec.Command("shutdown", "-h", "now").Run(); err != nil {
|
if err := exec.Command("shutdown", "-h", "now").Run(); err != nil {
|
||||||
fmt.Println("Failed to initiate shutdown:", err)
|
fmt.Println("Failed to initiate shutdown:", err)
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ type TimeOut struct {
|
|||||||
ActionChan chan bool
|
ActionChan chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t TimeOut) DryExecute() {
|
||||||
|
t.Execute()
|
||||||
|
}
|
||||||
|
|
||||||
func (t TimeOut) Execute() {
|
func (t TimeOut) Execute() {
|
||||||
fmt.Printf("Waiting %d seconds\n", t.Duration/time.Second)
|
fmt.Printf("Waiting %d seconds\n", t.Duration/time.Second)
|
||||||
time.Sleep(t.Duration)
|
time.Sleep(t.Duration)
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ func GetDocumentation() string {
|
|||||||
func main() {
|
func main() {
|
||||||
configFilePath := flag.String("c", "", "path to config file")
|
configFilePath := flag.String("c", "", "path to config file")
|
||||||
showDoc := flag.Bool("d", false, "show doc")
|
showDoc := flag.Bool("d", false, "show doc")
|
||||||
|
testRun := flag.Bool("t", false, "test run")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *showDoc {
|
if *showDoc {
|
||||||
@@ -52,6 +54,8 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actions.TestRun = *testRun
|
||||||
|
|
||||||
configFile, err := os.ReadFile(*configFilePath)
|
configFile, err := os.ReadFile(*configFilePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ func isEthernetConnected(deviceName string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if string(content[:4]) == "down" {
|
if string(content[:4]) == "down" {
|
||||||
fmt.Println("Ethernet is disconnected")
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,25 +32,21 @@ func isEthernetConnected(deviceName string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t EthernetDisconnect) Listen() {
|
func (t EthernetDisconnect) Listen() {
|
||||||
fmt.Println("EthernetDisconnect listens")
|
|
||||||
|
|
||||||
if t.WaitTillConnected {
|
if t.WaitTillConnected {
|
||||||
for !isEthernetConnected("enp0s31f6") {
|
for !isEthernetConnected(t.InterfaceName) {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if !isEthernetConnected("enp0s31f6") {
|
if !isEthernetConnected(t.InterfaceName) {
|
||||||
fmt.Println("Ethernet is disconnected")
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("EthernetDisconnect fires")
|
actions.Fire(t.action)
|
||||||
t.action.Execute()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func NewTimeOut(d time.Duration, action actions.Action) EthernetDisconnect {
|
// func NewTimeOut(d time.Duration, action actions.Action) EthernetDisconnect {
|
||||||
@@ -60,17 +55,12 @@ func NewEthernetDisconnect(config internal.KillSwitchConfig) (EthernetDisconnect
|
|||||||
WaitTillConnected: true,
|
WaitTillConnected: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(config.Options))
|
|
||||||
err := json.Unmarshal(config.Options, &result)
|
err := json.Unmarshal(config.Options, &result)
|
||||||
|
|
||||||
fmt.Println(result)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return EthernetDisconnect{}, err
|
return EthernetDisconnect{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(result.InterfaceName)
|
|
||||||
|
|
||||||
if result.InterfaceName == "" {
|
if result.InterfaceName == "" {
|
||||||
return EthernetDisconnect{}, internal.OptionMissingError{"interfaceName"}
|
return EthernetDisconnect{}, internal.OptionMissingError{"interfaceName"}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,19 +10,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TimeOut struct {
|
type TimeOut struct {
|
||||||
d time.Duration
|
Duration int
|
||||||
action actions.Action
|
action actions.Action
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TimeOut) Listen() {
|
func (t TimeOut) Listen() {
|
||||||
fmt.Println("TimeOut listens")
|
fmt.Println("TimeOut listens")
|
||||||
time.Sleep(t.d)
|
fmt.Println(t.Duration)
|
||||||
|
time.Sleep(time.Duration(t.Duration) * time.Second)
|
||||||
fmt.Println("TimeOut fires")
|
fmt.Println("TimeOut fires")
|
||||||
t.action.Execute()
|
actions.Fire(t.action)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) {
|
func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) {
|
||||||
result := TimeOut{d: 0 * time.Second}
|
var result TimeOut
|
||||||
err := json.Unmarshal(config.Options, &result)
|
err := json.Unmarshal(config.Options, &result)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user