add built in docu
This commit is contained in:
@@ -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{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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", "\"\""},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"},
|
||||
}
|
||||
}
|
||||
|
||||
84
gokill.go
84
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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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{},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user