From 4804eb05f1d4a188ac0af8c8d6546ace3b746d3f Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Sat, 1 Dec 2018 00:56:57 +0300 Subject: renamed job package to queue --- pkg/gui/gui.go | 6 +-- pkg/gui/mainview.go | 12 ++--- pkg/gui/textstyle.go | 8 ++-- pkg/job/job.go | 133 --------------------------------------------------- pkg/queue/queue.go | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+), 146 deletions(-) delete mode 100644 pkg/job/job.go create mode 100644 pkg/queue/queue.go diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 5898cb1..ef3df9c 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/isacikgoz/gitbatch/pkg/git" - "github.com/isacikgoz/gitbatch/pkg/job" + "github.com/isacikgoz/gitbatch/pkg/queue" "github.com/jroimartin/gocui" ) @@ -22,7 +22,7 @@ type guiState struct { Repositories []*git.RepoEntity Directories []string Mode mode - Queue *job.JobQueue + Queue *queue.JobQueue } // this struct encapsulates the name and title of a view. the name of a view is @@ -70,7 +70,7 @@ func NewGui(directoies []string) (*Gui, error) { initialState := guiState{ Directories: directoies, Mode: fetchMode, - Queue: job.CreateJobQueue(), + Queue: queue.CreateJobQueue(), } gui := &Gui{ State: initialState, diff --git a/pkg/gui/mainview.go b/pkg/gui/mainview.go index e526f60..eb57526 100644 --- a/pkg/gui/mainview.go +++ b/pkg/gui/mainview.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/isacikgoz/gitbatch/pkg/git" - "github.com/isacikgoz/gitbatch/pkg/job" + "github.com/isacikgoz/gitbatch/pkg/queue" "github.com/jroimartin/gocui" ) @@ -106,18 +106,18 @@ func (gui *Gui) markRepository(g *gocui.Gui, v *gocui.View) error { return err } if r.State == git.Available || r.State == git.Success { - var jt job.JobType + var jt queue.JobType switch mode := gui.State.Mode.ModeID; mode { case FetchMode: - jt = job.Fetch + jt = queue.Fetch case PullMode: - jt = job.Pull + jt = queue.Pull case MergeMode: - jt = job.Merge + jt = queue.Merge default: return nil } - err := gui.State.Queue.AddJob(&job.Job{ + err := gui.State.Queue.AddJob(&queue.Job{ JobType: jt, Entity: r, }) diff --git a/pkg/gui/textstyle.go b/pkg/gui/textstyle.go index 1485572..07b71ea 100644 --- a/pkg/gui/textstyle.go +++ b/pkg/gui/textstyle.go @@ -5,7 +5,7 @@ import ( "github.com/fatih/color" "github.com/isacikgoz/gitbatch/pkg/git" - "github.com/isacikgoz/gitbatch/pkg/job" + "github.com/isacikgoz/gitbatch/pkg/queue" ) var ( @@ -77,11 +77,11 @@ func (gui *Gui) displayString(entity *git.RepoEntity) string { if entity.State == git.Queued { if inQueue, ty := gui.State.Queue.IsInTheQueue(entity); inQueue { switch mode := ty; mode { - case job.Fetch: + case queue.Fetch: suffix = blue.Sprint(queuedSymbol) - case job.Pull: + case queue.Pull: suffix = magenta.Sprint(queuedSymbol) - case job.Merge: + case queue.Merge: suffix = cyan.Sprint(queuedSymbol) default: suffix = green.Sprint(queuedSymbol) diff --git a/pkg/job/job.go b/pkg/job/job.go deleted file mode 100644 index 6ba2997..0000000 --- a/pkg/job/job.go +++ /dev/null @@ -1,133 +0,0 @@ -package job - -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 -} - -// only holds the slice of Jobs -type JobQueue struct { - series []*Job -} - -type JobType string - -const ( - Fetch JobType = "fetch" - Pull JobType = "pull" - Merge JobType = "merge" -) - -// 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 -} - -// creates a jobqueue struct and initialize its slice then return its pointer -func CreateJobQueue() (jobQueue *JobQueue) { - s := make([]*Job, 0) - return &JobQueue{ - series: s, - } -} - -// add job to the queue -func (jobQueue *JobQueue) AddJob(j *Job) error { - for _, job := range jobQueue.series { - if job.Entity.RepoID == j.Entity.RepoID && job.JobType == j.JobType { - return errors.New("Same job already is in the queue") - } - } - jobQueue.series = append(jobQueue.series, j) - return nil -} - -// start the next job of the queue -func (jobQueue *JobQueue) StartNext() (j *Job, finished bool, err error) { - finished = false - if len(jobQueue.series) < 1 { - finished = true - return nil, finished, nil - } - i := len(jobQueue.series)-1 - lastJob := jobQueue.series[i] - jobQueue.series = jobQueue.series[:i] - if err = lastJob.start(); err != nil { - return lastJob, finished, err - } - return lastJob, finished, nil -} - -// delete it from the queue -// TODO: it is not safe if the job has been started -func (jobQueue *JobQueue) RemoveFromQueue(entity *git.RepoEntity) error { - removed := false - for i, job := range jobQueue.series { - if job.Entity.RepoID == entity.RepoID { - jobQueue.series = append(jobQueue.series[:i], jobQueue.series[i+1:]...) - removed = true - } - } - if !removed { - return errors.New("There is no job with given repoID") - } - return nil -} - -// since the job and entity is not tied with its own struct, this function -// returns true if that entity is in the queue along with the jobs type -func (jobQueue *JobQueue) IsInTheQueue(entity *git.RepoEntity) (inTheQueue bool, jt JobType) { - inTheQueue = false - for _, job := range jobQueue.series { - if job.Entity.RepoID == entity.RepoID { - inTheQueue = true - jt = job.JobType - } - } - return inTheQueue, jt -} diff --git a/pkg/queue/queue.go b/pkg/queue/queue.go new file mode 100644 index 0000000..091fa84 --- /dev/null +++ b/pkg/queue/queue.go @@ -0,0 +1,133 @@ +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 +} + +// only holds the slice of Jobs +type JobQueue struct { + series []*Job +} + +type JobType string + +const ( + Fetch JobType = "fetch" + Pull JobType = "pull" + Merge JobType = "merge" +) + +// 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 +} + +// creates a jobqueue struct and initialize its slice then return its pointer +func CreateJobQueue() (jobQueue *JobQueue) { + s := make([]*Job, 0) + return &JobQueue{ + series: s, + } +} + +// add job to the queue +func (jobQueue *JobQueue) AddJob(j *Job) error { + for _, job := range jobQueue.series { + if job.Entity.RepoID == j.Entity.RepoID && job.JobType == j.JobType { + return errors.New("Same job already is in the queue") + } + } + jobQueue.series = append(jobQueue.series, j) + return nil +} + +// start the next job of the queue +func (jobQueue *JobQueue) StartNext() (j *Job, finished bool, err error) { + finished = false + if len(jobQueue.series) < 1 { + finished = true + return nil, finished, nil + } + i := len(jobQueue.series)-1 + lastJob := jobQueue.series[i] + jobQueue.series = jobQueue.series[:i] + if err = lastJob.start(); err != nil { + return lastJob, finished, err + } + return lastJob, finished, nil +} + +// delete it from the queue +// TODO: it is not safe if the job has been started +func (jobQueue *JobQueue) RemoveFromQueue(entity *git.RepoEntity) error { + removed := false + for i, job := range jobQueue.series { + if job.Entity.RepoID == entity.RepoID { + jobQueue.series = append(jobQueue.series[:i], jobQueue.series[i+1:]...) + removed = true + } + } + if !removed { + return errors.New("There is no job with given repoID") + } + return nil +} + +// since the job and entity is not tied with its own struct, this function +// returns true if that entity is in the queue along with the jobs type +func (jobQueue *JobQueue) IsInTheQueue(entity *git.RepoEntity) (inTheQueue bool, jt JobType) { + inTheQueue = false + for _, job := range jobQueue.series { + if job.Entity.RepoID == entity.RepoID { + inTheQueue = true + jt = job.JobType + } + } + return inTheQueue, jt +} -- cgit v1.2.3