diff --git a/cmd/config_job_control.go b/cmd/config_job_control.go index dc8a1e4..3baf715 100644 --- a/cmd/config_job_control.go +++ b/cmd/config_job_control.go @@ -31,6 +31,8 @@ func (j *ControlJob) JobName() string { return j.Name } +func (j *ControlJob) JobType() JobType { return JobTypeControl } + func (j *ControlJob) JobStatus(ctx context.Context) (*JobStatus, error) { return &JobStatus{Tasks: nil}, nil } diff --git a/cmd/config_job_local.go b/cmd/config_job_local.go index 2d56530..7bbe517 100644 --- a/cmd/config_job_local.go +++ b/cmd/config_job_local.go @@ -84,6 +84,8 @@ func (j *LocalJob) JobName() string { return j.Name } +func (j *LocalJob) JobType() JobType { return JobTypeLocal } + func (j *LocalJob) JobStart(ctx context.Context) { rootLog := ctx.Value(contextKeyLog).(Logger) diff --git a/cmd/config_job_pull.go b/cmd/config_job_pull.go index e3201f0..c3e2413 100644 --- a/cmd/config_job_pull.go +++ b/cmd/config_job_pull.go @@ -94,6 +94,8 @@ func (j *PullJob) JobName() string { return j.Name } +func (j *PullJob) JobType() JobType { return JobTypePull } + func (j *PullJob) JobStart(ctx context.Context) { log := ctx.Value(contextKeyLog).(Logger) diff --git a/cmd/config_job_source.go b/cmd/config_job_source.go index ba43186..79b8559 100644 --- a/cmd/config_job_source.go +++ b/cmd/config_job_source.go @@ -76,6 +76,8 @@ func (j *SourceJob) JobName() string { return j.Name } +func (j *SourceJob) JobType() JobType { return JobTypeSource } + func (j *SourceJob) JobStart(ctx context.Context) { log := ctx.Value(contextKeyLog).(Logger) diff --git a/cmd/config_parse.go b/cmd/config_parse.go index 2c70224..d39f405 100644 --- a/cmd/config_parse.go +++ b/cmd/config_parse.go @@ -162,20 +162,25 @@ func parseJob(c JobParsingContext, i map[string]interface{}) (j Job, err error) } } - jobtype, err := extractStringField(i, "type", true) + jobtypeStr, err := extractStringField(i, "type", true) + if err != nil { + return + } + + jobtype, err := ParseUserJobType(jobtypeStr) if err != nil { return } switch jobtype { - case "pull": + case JobTypePull: return parsePullJob(c, name, i) - case "source": + case JobTypeSource: return parseSourceJob(c, name, i) - case "local": + case JobTypeLocal: return parseLocalJob(c, name, i) default: - return nil, errors.Errorf("unknown job type '%s'", jobtype) + panic(fmt.Sprintf("implementation error: unknown job type %s", jobtype)) } } diff --git a/cmd/daemon.go b/cmd/daemon.go index c4dfb69..139d472 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -28,10 +28,36 @@ func init() { type Job interface { JobName() string + JobType() JobType JobStart(ctxt context.Context) JobStatus(ctxt context.Context) (*JobStatus, error) } +type JobType string + +const ( + JobTypePull JobType = "pull" + JobTypeSource JobType = "source" + JobTypeLocal JobType = "local" + JobTypeControl JobType = "control" +) + +func ParseUserJobType(s string) (JobType, error) { + switch s { + case "pull": + return JobTypePull, nil + case "source": + return JobTypeSource, nil + case "local": + return JobTypeLocal, nil + } + return "", fmt.Errorf("unknown job type '%s'", s) +} + +func (j JobType) String() string { + return string(j) +} + func doDaemon(cmd *cobra.Command, args []string) { conf, err := ParseConfig(rootArgs.configFile)