Compare commits

...

2 Commits

Author SHA1 Message Date
20120785bd [nix] add devShell 2023-10-30 20:00:43 +01:00
5af4c963ea [actions] handle errors via channel 2023-10-30 19:59:21 +01:00
7 changed files with 47 additions and 28 deletions

View File

@@ -7,10 +7,12 @@ import (
"unknown.com/gokill/internal"
)
type ActionResultChan chan error
type Action interface {
Execute()
DryExecute()
Create(internal.ActionConfig, chan bool) (Action, error)
Create(internal.ActionConfig, ActionResultChan) (Action, error)
}
type DocumentedAction interface {
@@ -23,7 +25,7 @@ type Stage struct {
}
type StagedActions struct {
ActionChan chan bool
ActionChan ActionResultChan
StageCount int
Stages []Stage
}
@@ -40,7 +42,11 @@ func (a StagedActions) executeInternal(f func(Action)) {
}
for range stage.Actions {
<-a.ActionChan
err := <-a.ActionChan
if err != nil {
fmt.Printf("Error occured on Stage %d: %s", idx+1, err)
}
}
}
}
@@ -64,11 +70,11 @@ func (a StagedActions) Execute() {
a.executeInternal(func(a Action) { a.Execute() })
}
func (a StagedActions) Create(config internal.ActionConfig, c chan bool) (Action, error) {
func (a StagedActions) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
return StagedActions{}, nil
}
func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
func NewSingleAction(config internal.ActionConfig, c ActionResultChan) (Action, error) {
for _, availableAction := range GetAllActions() {
if config.Type == availableAction.GetName() {
return availableAction.Create(config, c)
@@ -83,7 +89,7 @@ func NewAction(config []internal.ActionConfig) (Action, error) {
return config[i].Stage < config[j].Stage
})
stagedActions := StagedActions{make(chan bool), 0, []Stage{}}
stagedActions := StagedActions{make(ActionResultChan), 0, []Stage{}}
stageMap := make(map[int][]Action)

View File

@@ -9,20 +9,20 @@ import (
type Printer struct {
Message string
ActionChan chan bool
ActionChan ActionResultChan
}
func (p Printer) Execute() {
fmt.Printf("Print action fires. Message: %s\n", p.Message)
p.ActionChan <- true
p.ActionChan <- nil
}
func (p Printer) DryExecute() {
fmt.Printf("Print action fire test. Message: %s\n", p.Message)
p.ActionChan <- true
p.ActionChan <- nil
}
func (p Printer) Create(config internal.ActionConfig, c chan bool) (Action, error) {
func (p Printer) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
var result Printer
err := json.Unmarshal(config.Options, &result)
@@ -44,6 +44,11 @@ func (p Printer) GetDescription() string {
func (p Printer) GetOptions() []internal.ConfigOption {
return []internal.ConfigOption{
{"message", "string", "Message that should be printed", "\"\""},
{
Name: "message",
Type: "string",
Description: "Message that should be printed",
Default: "\"\"",
},
}
}

View File

@@ -8,13 +8,13 @@ import (
)
type Shutdown struct {
ActionChan chan bool
ActionChan ActionResultChan
}
func (s Shutdown) DryExecute() {
fmt.Println("Test Shutdown executed...")
s.ActionChan <- true
s.ActionChan <- nil
}
@@ -25,10 +25,10 @@ func (s Shutdown) Execute() {
fmt.Println("Shutdown executed...")
s.ActionChan <- true
s.ActionChan <- nil
}
func (s Shutdown) Create(config internal.ActionConfig, c chan bool) (Action, error) {
func (s Shutdown) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
return Shutdown{c}, nil
}

View File

@@ -10,7 +10,7 @@ import (
type TimeOut struct {
Duration time.Duration
ActionChan chan bool
ActionChan ActionResultChan
}
func (t TimeOut) DryExecute() {
@@ -20,10 +20,10 @@ func (t TimeOut) DryExecute() {
func (t TimeOut) Execute() {
fmt.Printf("Waiting %d seconds\n", t.Duration)
time.Sleep(time.Duration(t.Duration) * time.Second)
t.ActionChan <- true
t.ActionChan <- nil
}
func (t TimeOut) Create(config internal.ActionConfig, c chan bool) (Action, error) {
func (t TimeOut) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
var result TimeOut
err := json.Unmarshal(config.Options, &result)

View File

@@ -11,12 +11,12 @@ import (
type Command struct {
Command string `json:"command"`
ActionChan chan bool
ActionChan ActionResultChan
}
func (c Command) DryExecute() {
fmt.Printf("Test Executing Command:\n%s ", c.Command)
c.ActionChan <- true
c.ActionChan <- nil
}
func (c Command) splitCommandString() (string, []string, error) {
@@ -38,8 +38,7 @@ func (c Command) Execute() {
fmt.Println("Executing command: ", c.Command)
if err != nil {
fmt.Println(err)
c.ActionChan <- false
c.ActionChan <- err
return
}
@@ -53,10 +52,10 @@ func (c Command) Execute() {
fmt.Println(string(stdout[:]))
c.ActionChan <- true
c.ActionChan <- nil
}
func CreateCommand(config internal.ActionConfig, c chan bool) (Command, error) {
func CreateCommand(config internal.ActionConfig, c ActionResultChan) (Command, error) {
result := Command{}
err := json.Unmarshal(config.Options, &result)
@@ -74,7 +73,7 @@ func CreateCommand(config internal.ActionConfig, c chan bool) (Command, error) {
return result, nil
}
func (cc Command) Create(config internal.ActionConfig, c chan bool) (Action, error) {
func (cc Command) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
return CreateCommand(config, c)
}

6
flake.lock generated
View File

@@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1697915759,
"narHash": "sha256-WyMj5jGcecD+KC8gEs+wFth1J1wjisZf8kVZH13f1Zo=",
"lastModified": 1698553279,
"narHash": "sha256-T/9P8yBSLcqo/v+FTOBK+0rjzjPMctVymZydbvR/Fak=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "51d906d2341c9e866e48c2efcaac0f2d70bfd43e",
"rev": "90e85bc7c1a6fc0760a94ace129d3a1c61c3d035",
"type": "github"
},
"original": {

View File

@@ -7,8 +7,17 @@
outputs = { self, nixpkgs, ... }:
let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ];
pkgs = nixpkgs.legacyPackages."x86_64-linux";
in
{
devShell."x86_64-linux" = pkgs.mkShell {
packages = with pkgs; [
go
gotools
mdbook
];
};
packages.x86_64-linux.gokill = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec {
pname = "gokill";
version = "1.0";