From ab3954a6cbd3c21778d426150feb434581447d17 Mon Sep 17 00:00:00 2001 From: kalipso Date: Tue, 18 Jul 2023 13:35:33 +0200 Subject: [PATCH] add built in docu --- actions/actions.go | 7 ++++ actions/printer.go | 14 ++++++++ actions/timeout.go | 14 ++++++++ gokill.go | 84 ++++++++++++++++++++++++++------------------ internal/config.go | 13 +++++++ triggers/timeout.go | 14 ++++++++ triggers/triggers.go | 6 ++++ 7 files changed, 117 insertions(+), 35 deletions(-) diff --git a/actions/actions.go b/actions/actions.go index 92b44e8..34d09b4 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -87,3 +87,10 @@ func NewAction(config []internal.ActionConfig) (Action, error) { return stagedActions, nil //return Action{}, fmt.Errorf("Error parsing config: Action with type %s does not exists", config.Type) } + +func GetDocumenters() []internal.Documenter { + return []internal.Documenter{ + Printer{}, + TimeOut{}, + } +} diff --git a/actions/printer.go b/actions/printer.go index 1b0be25..43b4ac9 100644 --- a/actions/printer.go +++ b/actions/printer.go @@ -26,3 +26,17 @@ func NewPrint(config internal.ActionConfig, c chan bool) (Action, error) { return Printer{fmt.Sprintf("%v", message), c}, nil } + +func (p Printer) GetName() string { + return "Print" +} + +func (p Printer) GetDescription() string { + return "When triggered prints the configured message to stdout" +} + +func (p Printer) GetOptions() []internal.ConfigOption { + return []internal.ConfigOption{ + {"message", "string", "Message that should be printed", "\"\""}, + } +} diff --git a/actions/timeout.go b/actions/timeout.go index 36f0f91..cd0b97d 100644 --- a/actions/timeout.go +++ b/actions/timeout.go @@ -28,3 +28,17 @@ func NewTimeOut(config internal.ActionConfig, c chan bool) (Action, error) { return TimeOut{time.Duration(duration.(float64)) * time.Second, c}, nil } + +func (p TimeOut) GetName() string { + return "TimeOut" +} + +func (p TimeOut) GetDescription() string { + return "When triggered waits given duration before continuing with next stage" +} + +func (p TimeOut) GetOptions() []internal.ConfigOption { + return []internal.ConfigOption{ + {"duration", "string", "duration in seconds", "0"}, + } +} diff --git a/gokill.go b/gokill.go index b8c728c..0a33bee 100644 --- a/gokill.go +++ b/gokill.go @@ -5,19 +5,38 @@ import ( "flag" "fmt" + "unknown.com/gokill/actions" "unknown.com/gokill/internal" "unknown.com/gokill/triggers" ) -func main() { - configFile := flag.String("c", "", "path to config file") - flag.Parse() +func GetDocumentation() string { + actions := actions.GetDocumenters() - if *configFile == "" { - fmt.Println("No config file given. Use --help to show usage.") - //return + result := "Available Actions:\n\n" + lineBreak := "----------------------------" + + writeOptions := func(documenters []internal.Documenter) { + for _, act := range documenters { + result += lineBreak + result += fmt.Sprintf("\nName: %v\nDescription: %v\nValues:\n", act.GetName(), act.GetDescription()) + + for _, opt := range act.GetOptions() { + result += fmt.Sprintf("\tName: %v\n\tType: %v\n\tDescr: %v\n\tDefault: %v\n", + opt.Name, opt.Type, opt.Description, opt.Default) + result += lineBreak + "\n\n" + } + } } + writeOptions(actions) + result += "\n\nAvailable Triggers:\n\n" + writeOptions(triggers.GetDocumenters()) + + return result +} + +func main() { b := []byte(` [ @@ -75,6 +94,20 @@ func main() { ] `) + configFile := flag.String("c", "", "path to config file") + showDoc := flag.Bool("d", false, "show doc") + flag.Parse() + + if *showDoc { + fmt.Print(GetDocumentation()) + return + } + + if *configFile == "" { + fmt.Println("No config file given. Use --help to show usage.") + //return + } + var f []internal.KillSwitchConfig err := json.Unmarshal(b, &f) @@ -83,36 +116,17 @@ func main() { return } - trigger, err := triggers.NewTrigger(f[0]) + var triggerList []triggers.Trigger + for _, cfg := range f { + trigger, err := triggers.NewTrigger(cfg) - if err != nil { - fmt.Println(err) - return + if err != nil { + fmt.Println(err) + return + } + + trigger.Listen() //TODO: not block here + triggerList = append(triggerList, trigger) } - trigger.Listen() - - //stagedActions := actions.StagedActions{make(chan bool), 0, []actions.Stage{}} - - //stageOne := actions.Stage{[]actions.Action{ - // actions.Printer{"first action\n", stagedActions.ActionChan}, - // actions.Printer{"second actiloo\n", stagedActions.ActionChan}, - // actions.TimeOut{stagedActions.ActionChan}, - //}} - - //stageTwo := actions.Stage{[]actions.Action{ - // actions.Printer{"third action\n", stagedActions.ActionChan}, - // actions.TimeOut{stagedActions.ActionChan}, - //}} - - //stageThree := actions.Stage{[]actions.Action{ - // actions.Printer{"four action\n", stagedActions.ActionChan}, - // actions.Printer{"five action\n", stagedActions.ActionChan}, - // actions.Printer{"six action\n", stagedActions.ActionChan}, - //}} - - //stagedActions.Stages = []actions.Stage{stageOne, stageTwo, stageThree} - - //timeOut := triggers.NewTimeOut(2*time.Second, stagedActions) - //timeOut.Listen() } diff --git a/internal/config.go b/internal/config.go index 77df040..eb238a7 100644 --- a/internal/config.go +++ b/internal/config.go @@ -24,3 +24,16 @@ type KillSwitchConfig struct { Options Options `json:"options"` Actions []ActionConfig `json:"actions"` } + +type ConfigOption struct { + Name string + Type string + Description string + Default string +} + +type Documenter interface { + GetName() string + GetDescription() string + GetOptions() []ConfigOption +} diff --git a/triggers/timeout.go b/triggers/timeout.go index 04e0709..a20146e 100644 --- a/triggers/timeout.go +++ b/triggers/timeout.go @@ -38,3 +38,17 @@ func NewTimeOut(config internal.KillSwitchConfig) (TimeOut, error) { return TimeOut{time.Duration(duration.(float64)) * time.Second, action}, nil } + +func (p TimeOut) GetName() string { + return "TimeOut" +} + +func (p TimeOut) GetDescription() string { + return "Triggers after given duration." +} + +func (p TimeOut) GetOptions() []internal.ConfigOption { + return []internal.ConfigOption{ + {"duration", "string", "duration in seconds", "0"}, + } +} diff --git a/triggers/triggers.go b/triggers/triggers.go index 61755dc..5248e9a 100644 --- a/triggers/triggers.go +++ b/triggers/triggers.go @@ -17,3 +17,9 @@ func NewTrigger(config internal.KillSwitchConfig) (Trigger, error) { return nil, fmt.Errorf("Error parsing config: Trigger with type %s does not exists", config.Type) } + +func GetDocumenters() []internal.Documenter { + return []internal.Documenter{ + TimeOut{}, + } +}