Skip to content

Commit

Permalink
Merge pull request #43 from termkit/small-improvements
Browse files Browse the repository at this point in the history
Small improvements
  • Loading branch information
canack authored Mar 12, 2024
2 parents 7e81016 + 612644b commit 46f70c4
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
3 changes: 1 addition & 2 deletions internal/terminal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ func (m *model) syncTerminal(msg tea.Msg) {

if !m.terminalSizeReady {
m.viewport = viewport.New(msg.Width, msg.Height)
m.viewport.YPosition = headerHeight
m.terminalSizeReady = true
m.viewport.YPosition = headerHeight + 1
m.terminalSizeReady = true
} else {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height
Expand Down
8 changes: 4 additions & 4 deletions internal/terminal/handler/information/information.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (
)

var (
gamaVersion string
currentVersion string
newVersionAvailableMsg string
applicationDescription string
)
Expand All @@ -72,16 +72,16 @@ func SetupModelInfo(githubUseCase gu.UseCase, versionUseCase vu.UseCase, lockTab
}

func (m *ModelInfo) Init() tea.Cmd {
gamaVersion = m.versionUseCase.CurrentVersion()
applicationDescription = fmt.Sprintf("Github Actions Manager (%s)", gamaVersion)
currentVersion = m.versionUseCase.CurrentVersion()
applicationDescription = fmt.Sprintf("Github Actions Manager (%s)", currentVersion)

go m.testConnection(context.Background())
go m.checkUpdates(context.Background())
return nil
}

func (m *ModelInfo) checkUpdates(ctx context.Context) {
isUpdateAvailable, version, err := m.versionUseCase.IsUpdateAvailable()
isUpdateAvailable, version, err := m.versionUseCase.IsUpdateAvailable(ctx)
if err != nil {
m.modelError.SetError(err)
m.modelError.SetErrorMessage("failed to check updates")
Expand Down
4 changes: 3 additions & 1 deletion internal/version/repository/ports.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package repository

import "context"

type Repository interface {
CurrentVersion() string
LatestVersion() (string, error)
LatestVersion(ctx context.Context) (string, error)
}
7 changes: 4 additions & 3 deletions internal/version/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@ func (r *Repo) CurrentVersion() string {
return r.currentVersion
}

func (r *Repo) LatestVersion() (string, error) {
func (r *Repo) LatestVersion(ctx context.Context) (string, error) {
var result struct {
TagName string `json:"tag_name"`
}

err := r.do(context.Background(), nil, &result, requestOptions{
err := r.do(ctx, nil, &result, requestOptions{
method: "GET",
path: fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo),
accept: "application/vnd.github+json",
})
// client time out error
if errors.As(err, &context.DeadlineExceeded) {
var deadlineExceededError *url.Error
if errors.As(err, &deadlineExceededError) && deadlineExceededError.Timeout() {
return "", errors.New("request timed out")
} else if err != nil {
return "", err
Expand Down
13 changes: 9 additions & 4 deletions internal/version/repository/repository_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package repository

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func NewRepository() Repository {
return New("")
}

func TestRepo_LatestVersion(t *testing.T) {
ctx := context.Background()
repo := NewRepository()

_, err := repo.LatestVersion()
if err != nil {
t.Error(err)
}
t.Run("Get latest version", func(t *testing.T) {
res, err := repo.LatestVersion(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, res)
})
}
4 changes: 3 additions & 1 deletion internal/version/usecase/ports.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package usecase

import "context"

type UseCase interface {
CurrentVersion() string
IsUpdateAvailable() (isAvailable bool, version string, err error)
IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error)
}
5 changes: 3 additions & 2 deletions internal/version/usecase/usecase.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package usecase

import (
"context"
"github.com/Masterminds/semver/v3"

vr "github.com/termkit/gama/internal/version/repository"
Expand All @@ -20,13 +21,13 @@ func (u *useCase) CurrentVersion() string {
return u.versionRepository.CurrentVersion()
}

func (u *useCase) IsUpdateAvailable() (isAvailable bool, version string, err error) {
func (u *useCase) IsUpdateAvailable(ctx context.Context) (isAvailable bool, version string, err error) {
currentVersion := u.versionRepository.CurrentVersion()
if currentVersion == "under development" {
return false, currentVersion, nil
}

latestVersion, err := u.versionRepository.LatestVersion()
latestVersion, err := u.versionRepository.LatestVersion(ctx)
if err != nil {
return false, currentVersion, err
}
Expand Down

0 comments on commit 46f70c4

Please sign in to comment.