add Shutdown action

This commit is contained in:
2023-08-16 00:16:31 +02:00
parent c2402e122f
commit b2f222f550
2 changed files with 43 additions and 0 deletions

View File

@@ -47,6 +47,10 @@ func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error)
return NewTimeOut(config, c)
}
if config.Type == "Shutdown" {
return NewShutdown(config, c)
}
return nil, fmt.Errorf("Error parsing config: Action with type %s does not exists", config.Type)
}
@@ -92,5 +96,6 @@ func GetDocumenters() []internal.Documenter {
return []internal.Documenter{
Printer{},
TimeOut{},
Shutdown{},
}
}

38
actions/shutdown.go Normal file
View File

@@ -0,0 +1,38 @@
package actions
import (
"fmt"
"os/exec"
"unknown.com/gokill/internal"
)
type Shutdown struct {
ActionChan chan bool
}
func (c Shutdown) Execute() {
if err := exec.Command("shutdown", "-h", "now").Run(); err != nil {
fmt.Println("Failed to initiate shutdown:", err)
}
fmt.Println("Shutdown executed...")
c.ActionChan <- true
}
func NewShutdown(config internal.ActionConfig, c chan bool) (Action, error) {
return Shutdown{c}, nil
}
func (p Shutdown) GetName() string {
return "Shutdown"
}
func (p Shutdown) GetDescription() string {
return "When triggered shuts down the machine"
}
func (p Shutdown) GetOptions() []internal.ConfigOption {
return []internal.ConfigOption{}
}