add built in docu

This commit is contained in:
2023-07-18 13:35:33 +02:00
parent a457a313b7
commit ab3954a6cb
7 changed files with 117 additions and 35 deletions

View File

@@ -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{},
}
}

View File

@@ -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", "\"\""},
}
}

View File

@@ -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"},
}
}

View File

@@ -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()
}

View File

@@ -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
}

View File

@@ -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"},
}
}

View File

@@ -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{},
}
}