From b2f222f550c95ac3bb5d684309ac2fd4d1649193 Mon Sep 17 00:00:00 2001 From: kalipso Date: Wed, 16 Aug 2023 00:16:31 +0200 Subject: [PATCH] add Shutdown action --- actions/actions.go | 5 +++++ actions/shutdown.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 actions/shutdown.go diff --git a/actions/actions.go b/actions/actions.go index 34d09b4..38eabe9 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -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{}, } } diff --git a/actions/shutdown.go b/actions/shutdown.go new file mode 100644 index 0000000..67c2e81 --- /dev/null +++ b/actions/shutdown.go @@ -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{} +}