2018-08-27 19:10:55 +02:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-30 17:40:02 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-09-23 23:04:31 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
2018-08-27 19:10:55 +02:00
|
|
|
)
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
func JobsFromConfig(c *config.Config) ([]Job, error) {
|
2018-08-27 19:10:55 +02:00
|
|
|
js := make([]Job, len(c.Jobs))
|
|
|
|
for i := range c.Jobs {
|
|
|
|
j, err := buildJob(c.Global, c.Jobs[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
js[i] = j
|
|
|
|
}
|
|
|
|
return js, nil
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
func buildJob(c *config.Global, in config.JobEnum) (j Job, err error) {
|
2018-09-23 23:04:31 +02:00
|
|
|
cannotBuildJob := func(e error, name string) (Job, error) {
|
|
|
|
return nil, errors.Wrapf(err, "cannot build job %q", name)
|
|
|
|
}
|
2018-09-24 12:31:29 +02:00
|
|
|
// FIXME prettify this
|
2018-08-27 19:10:55 +02:00
|
|
|
switch v := in.Ret.(type) {
|
2018-08-27 22:21:45 +02:00
|
|
|
case *config.SinkJob:
|
2018-09-23 23:04:31 +02:00
|
|
|
m, err := modeSinkFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = passiveSideFromConfig(c, &v.PassiveJob, m)
|
2018-08-30 17:40:02 +02:00
|
|
|
if err != nil {
|
2018-09-23 23:04:31 +02:00
|
|
|
return cannotBuildJob(err, v.Name)
|
2018-08-30 17:40:02 +02:00
|
|
|
}
|
2018-09-24 12:31:29 +02:00
|
|
|
case *config.SourceJob:
|
|
|
|
m, err := modeSourceFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = passiveSideFromConfig(c, &v.PassiveJob, m)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
2018-08-27 22:21:45 +02:00
|
|
|
case *config.PushJob:
|
2018-09-23 23:04:31 +02:00
|
|
|
m, err := modePushFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = activeSide(c, &v.ActiveJob, m)
|
2018-08-30 17:40:02 +02:00
|
|
|
if err != nil {
|
2018-09-23 23:04:31 +02:00
|
|
|
return cannotBuildJob(err, v.Name)
|
2018-08-30 17:40:02 +02:00
|
|
|
}
|
2018-09-24 12:31:29 +02:00
|
|
|
case *config.PullJob:
|
|
|
|
m, err := modePullFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = activeSide(c, &v.ActiveJob, m)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
2018-08-27 19:10:55 +02:00
|
|
|
default:
|
2018-08-27 22:21:45 +02:00
|
|
|
panic(fmt.Sprintf("implementation error: unknown job type %T", v))
|
2018-08-27 19:10:55 +02:00
|
|
|
}
|
2018-09-23 23:04:31 +02:00
|
|
|
return j, nil
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|