goyek / goyek

Task automation Go library

Home Page:https://pkg.go.dev/github.com/goyek/goyek/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add parameter validation examples

pellared opened this issue · comments

Add a new example describing how parameter validation can be implemented. Reference/describe the example here

Originally posted by @pellared in #140 (comment)

  1. 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()
}
  1. 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()
}

Resolved by pull request #142.