Skip to content

Commit

Permalink
Merge pull request #6 from Raphexion/nijo/refactor-to-use-backend-abs…
Browse files Browse the repository at this point in the history
…traction

Nijo/refactor to use backend abstraction
  • Loading branch information
Raphexion authored Oct 5, 2021
2 parents 36ef6a4 + 2fce364 commit 06d3a91
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 274 deletions.
32 changes: 32 additions & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package backend

import (
"nina/noko"
)

// Backend
type Backend interface {
Init() error

// Timers
GetTimers() ([]noko.Timer, error)
GetTimersWithState(state string) ([]noko.Timer, error)
GetRunningTimer() (*noko.Timer, error)
PauseTimer(timer *noko.Timer) error
StartTimer(timer *noko.Timer) error
LogTimer(timer *noko.Timer) error
CreateTimer(project *noko.Project) (*noko.Timer, error)
DeleteTimer(timer *noko.Timer) error
SetDescription(timer *noko.Timer, description string) error
SetDescriptionOnRunningTimer(description string) error
AddOrSubTimer(timer *noko.Timer, minutes int) error
PauseRunningTimer() error
GetProjects() ([]noko.Project, error)
GetSomeProjects(withTimer bool) ([]noko.Project, error)

// Entries
GetEntries() ([]noko.Entry, error)

// Output
Write(p []byte) (n int, err error)
}
85 changes: 85 additions & 0 deletions backend/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package backend

import (
"bytes"
"errors"
"nina/noko"
)

var ErrNotImplemented = errors.New("not implemented")

type MockBackend struct {
Projects []noko.Project
Timers []noko.Timer
Entries []noko.Entry
Output *bytes.Buffer
}

func (m *MockBackend) Init() error {
b := make([]byte, 0, 4096)
m.Output = bytes.NewBuffer(b)
return nil
}

func (m *MockBackend) Write(p []byte) (n int, err error) {
return m.Output.Write(p)
}

func (m *MockBackend) GetTimers() ([]noko.Timer, error) {
return m.Timers, nil
}

func (m *MockBackend) GetTimersWithState(state string) ([]noko.Timer, error) {
return nil, ErrNotImplemented
}

func (m *MockBackend) GetRunningTimer() (*noko.Timer, error) {
return nil, ErrNotImplemented
}

func (m *MockBackend) PauseTimer(timer *noko.Timer) error {
return ErrNotImplemented
}
func (m *MockBackend) StartTimer(timer *noko.Timer) error {
return ErrNotImplemented
}

func (m *MockBackend) LogTimer(timer *noko.Timer) error {
return ErrNotImplemented
}

func (m *MockBackend) CreateTimer(project *noko.Project) (*noko.Timer, error) {
return nil, ErrNotImplemented
}

func (m *MockBackend) DeleteTimer(timer *noko.Timer) error {
return ErrNotImplemented
}

func (m *MockBackend) SetDescription(timer *noko.Timer, description string) error {
return ErrNotImplemented
}

func (m *MockBackend) SetDescriptionOnRunningTimer(description string) error {
return ErrNotImplemented
}

func (m *MockBackend) AddOrSubTimer(timer *noko.Timer, minutes int) error {
return ErrNotImplemented
}

func (m *MockBackend) PauseRunningTimer() error {
return ErrNotImplemented
}

func (m *MockBackend) GetProjects() ([]noko.Project, error) {
return m.Projects, nil
}

func (m *MockBackend) GetSomeProjects(withTimer bool) ([]noko.Project, error) {
return nil, ErrNotImplemented
}

func (m *MockBackend) GetEntries() ([]noko.Entry, error) {
return m.Entries, nil
}
124 changes: 45 additions & 79 deletions mid/mid.go → backend/real.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
package mid
package backend

import (
"context"
"errors"
"nina/noko"
"nina/utils"
"os"
"time"
)

func GetTimers() ([]noko.Timer, error) {
type RealBackend struct {
client *noko.Client
output *os.File
}

func (m *RealBackend) Init() error {
m.client = noko.NewClient()
m.output = os.Stdout

return nil
}

func (m *RealBackend) Write(p []byte) (n int, err error) {
return m.output.Write(p)
}

func (m *RealBackend) GetTimers() ([]noko.Timer, error) {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.GetTimers(ctx)
}

func GetTimersWithState(state string) ([]noko.Timer, error) {
allTimers, err := GetTimers()
func (m *RealBackend) GetTimersWithState(state string) ([]noko.Timer, error) {
allTimers, err := m.GetTimers()
if err != nil {
return nil, err
}
Expand All @@ -31,8 +47,8 @@ func GetTimersWithState(state string) ([]noko.Timer, error) {
return timers, nil
}

func GetRunningTimer() (*noko.Timer, error) {
timers, err := GetTimers()
func (m *RealBackend) GetRunningTimer() (*noko.Timer, error) {
timers, err := m.GetTimers()
if err != nil {
return nil, err
}
Expand All @@ -46,100 +62,69 @@ func GetRunningTimer() (*noko.Timer, error) {
return nil, errors.New("no running timer found")
}

func PauseTimer(timer *noko.Timer) error {
func (m *RealBackend) PauseTimer(timer *noko.Timer) error {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.PauseTimer(ctx, timer)
}

func StartTimer(timer *noko.Timer) error {
func (m *RealBackend) StartTimer(timer *noko.Timer) error {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.StartTimer(ctx, timer)
}

func LogTimer(timer *noko.Timer) error {
func (m *RealBackend) LogTimer(timer *noko.Timer) error {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.LogTimer(ctx, timer)
}

func CreateTimer(projectName string) (*noko.Timer, error) {
project, err := ProjectWithName(projectName)
if err != nil {
return nil, err
}

func (m *RealBackend) CreateTimer(project *noko.Project) (*noko.Timer, error) {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()

return client.CreateTimerForProject(ctx, project)
}

func DeleteTimer(timer *noko.Timer) error {
func (m *RealBackend) DeleteTimer(timer *noko.Timer) error {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.DeleteTimer(ctx, timer)
}

func TimerWithName(name string) (*noko.Timer, error) {
timers, err := GetTimers()

if err != nil {
return nil, err
}

var alternatives []string
for _, timer := range timers {
alternatives = append(alternatives, timer.Project.Name)
}

bestName, err := utils.ClosestMatch(name, alternatives)
if err != nil {
return nil, err
}

for _, timer := range timers {
if timer.Project.Name == bestName {
return &timer, nil
}
}

return nil, errors.New("unable to find a timer")
}

func SetDescription(timer *noko.Timer, description string) error {
func (m *RealBackend) SetDescription(timer *noko.Timer, description string) error {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.EditTimer(ctx, timer, description)
}

func SetDescriptionOnRunningTimer(description string) error {
timer, err := GetRunningTimer()
func (m *RealBackend) SetDescriptionOnRunningTimer(description string) error {
timer, err := m.GetRunningTimer()
if err != nil {
return err
}

return SetDescription(timer, description)
return m.SetDescription(timer, description)
}

func AddOrSubTimer(timer *noko.Timer, minutes int) error {
func (m *RealBackend) AddOrSubTimer(timer *noko.Timer, minutes int) error {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.AddOrSubTimer(ctx, timer, minutes)
}

func PauseRunningTimer() error {
timer, _ := GetRunningTimer()
func (m *RealBackend) PauseRunningTimer() error {
timer, _ := m.GetRunningTimer()
if timer != nil {
err := PauseTimer(timer)
err := m.PauseTimer(timer)
if err != nil {
return err
}
Expand All @@ -148,20 +133,20 @@ func PauseRunningTimer() error {
return nil
}

func GetProjects() ([]noko.Project, error) {
func (m *RealBackend) GetProjects() ([]noko.Project, error) {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.GetProjects(ctx)
}

func GetSomeProjects(withTimer bool) ([]noko.Project, error) {
allProjects, err := GetProjects()
func (m *RealBackend) GetSomeProjects(withTimer bool) ([]noko.Project, error) {
allProjects, err := m.GetProjects()
if err != nil {
return nil, err
}

timers, err := GetTimers()
timers, err := m.GetTimers()
if err != nil {
return nil, err
}
Expand All @@ -181,30 +166,11 @@ func GetSomeProjects(withTimer bool) ([]noko.Project, error) {
return projects, nil
}

func ProjectWithName(name string) (*noko.Project, error) {
projects, err := GetProjects()

if err != nil {
return nil, err
}

var alternatives []string
for _, project := range projects {
alternatives = append(alternatives, project.Name)
}

bestName, err := utils.ClosestMatch(name, alternatives)
if err != nil {
return nil, err
}

for _, project := range projects {
if project.Name == bestName {
return &project, nil
}
}

return nil, errors.New("unable to find a timer")
func (m *RealBackend) GetEntries() ([]noko.Entry, error) {
client := noko.NewClient()
ctx, cancel := standardContext()
defer cancel()
return client.GetEntries(ctx, false)
}

func standardContext() (context.Context, context.CancelFunc) {
Expand Down
Loading

0 comments on commit 06d3a91

Please sign in to comment.