diff options
Diffstat (limited to 'pkg/queue')
| -rw-r--r-- | pkg/queue/job.go | 68 | ||||
| -rw-r--r-- | pkg/queue/queue.go | 61 |
2 files changed, 68 insertions, 61 deletions
diff --git a/pkg/queue/job.go b/pkg/queue/job.go new file mode 100644 index 0000000..e2dd94e --- /dev/null +++ b/pkg/queue/job.go @@ -0,0 +1,68 @@ +package queue + +import ( + "time" + + "github.com/isacikgoz/gitbatch/pkg/git" +) + +// Job relates the type of the operation and the entity +type Job struct { + JobType JobType + Entity *git.RepoEntity +} + +// JobType is the a git operation supported +type JobType string + +const ( + // Fetch is wrapper of git fetch command + Fetch JobType = "fetch" + // Pull is wrapper of git pull command + Pull JobType = "pull" + // Merge is wrapper of git merge command + Merge JobType = "merge" +) + +// starts the job +func (job *Job) start() error { + job.Entity.State = git.Working + // added for testing, TODO: remove + time.Sleep(time.Second) + // TODO: Handle errors? + switch mode := job.JobType; mode { + case Fetch: + if err := git.Fetch(job.Entity, git.FetchOptions{ + RemoteName: job.Entity.Remote.Name, + }); err != nil { + job.Entity.State = git.Fail + return nil + } + case Pull: + if err := git.Fetch(job.Entity, git.FetchOptions{ + RemoteName: job.Entity.Remote.Name, + }); err != nil { + job.Entity.State = git.Fail + return nil + } + if err := git.Merge(job.Entity, git.MergeOptions{ + BranchName: job.Entity.Remote.Branch.Name, + }); err != nil { + job.Entity.State = git.Fail + return nil + } + case Merge: + if err := git.Merge(job.Entity, git.MergeOptions{ + BranchName: job.Entity.Remote.Branch.Name, + }); err != nil { + job.Entity.State = git.Fail + return nil + } + default: + job.Entity.State = git.Available + return nil + } + job.Entity.State = git.Success + job.Entity.Refresh() + return nil +} diff --git a/pkg/queue/queue.go b/pkg/queue/queue.go index 871a380..93626f0 100644 --- a/pkg/queue/queue.go +++ b/pkg/queue/queue.go @@ -2,76 +2,15 @@ package queue import ( "errors" - "fmt" - "time" "github.com/isacikgoz/gitbatch/pkg/git" ) -// Job relates the type of the operation and the entity -type Job struct { - JobType JobType - Entity *git.RepoEntity -} - // JobQueue holds the slice of Jobs type JobQueue struct { series []*Job } -// JobType is the a git operation supported -type JobType string - -const ( - // Fetch is wrapper of git fetch command - Fetch JobType = "fetch" - // Pull is wrapper of git pull command - Pull JobType = "pull" - // Merge is wrapper of git merge command - Merge JobType = "merge" -) - -// CreateJob es its name implies creates a job struct and return its pointer -func CreateJob() (j *Job, err error) { - fmt.Println("Job created.") - return j, nil -} - -// starts the job -func (job *Job) start() error { - job.Entity.State = git.Working - // added for testing, TODO: remove - time.Sleep(time.Second) - // TODO: Handle errors? - switch mode := job.JobType; mode { - case Fetch: - if err := job.Entity.Fetch(); err != nil { - job.Entity.State = git.Fail - return nil - } - job.Entity.RefreshPushPull() - job.Entity.State = git.Success - case Pull: - if err := job.Entity.Pull(); err != nil { - job.Entity.State = git.Fail - return nil - } - job.Entity.RefreshPushPull() - job.Entity.State = git.Success - case Merge: - if err := job.Entity.Merge(); err != nil { - job.Entity.State = git.Fail - return nil - } - job.Entity.RefreshPushPull() - job.Entity.State = git.Success - default: - job.Entity.State = git.Available - return nil - } - return nil -} - // CreateJobQueue creates a jobqueue struct and initialize its slice then return // its pointer func CreateJobQueue() (jobQueue *JobQueue) { |
