Add parameter validation examples
pellared opened this issue · comments
Robert Pająk commented
Add a new example describing how parameter validation can be implemented. Reference/describe the example here
Originally posted by @pellared in #140 (comment)
- For simple cases it can be done inside task action where the parameter is used
func main() {
flow := goyek.Flow{}
db := flow.RegisterStringParam(goyek.StringParam{
Name: "db",
Usage: "The database to migrate",
})
flow.Register(goyek.Task{
Name: "migrate",
Usage: "Migrate a db",
Params: goyek.Params{db},
Action: func(tf *goyek.TF) {
if db.Get(tf) == "" {
tf.Fatal("Param 'db' is not set")
}
// Do your thing
},
})
flow.Main()
}
- For complex validations (e.g. cross-parameter conditions or time-consuming validation) a dependant task responsible for validation may be handy
func main() {
flow := goyek.Flow{}
db := flow.RegisterStringParam(goyek.StringParam{
Name: "db",
Usage: "The database to migrate",
})
validateMigrate := flow.Register(goyek.Task{
Name: "validate-migrate",
Params: goyek.Params{db},
Action: func(tf *goyek.TF) {
if db.Get(tf) == "" {
tf.Fatal("Param 'db' is not set")
}
},
})
flow.Register(goyek.Task{
Name: "migrate",
Usage: "Migrate a db",
Deps: goyek.Deps{validateMigrate},
Params: goyek.Params{db},
Action: func(tf *goyek.TF) {
// Do your thing
},
})
flow.Main()
}
Arno van Liere commented
Resolved by pull request #142.