From 1cf58953254217d4654d9e91c8f43a89ba5c2dc0 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 16 Oct 2025 08:46:12 -0300 Subject: [PATCH] fix: simplify code Signed-off-by: Carlos Alexandro Becker --- go.mod | 1 - go.sum | 2 -- internal/app/app.go | 2 +- internal/update/update.go | 22 +++++++--------------- internal/update/update_test.go | 25 ++++++++++++++++--------- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 9423cc72e2ffed0d4090b362c3f0ab5d816d0500..5f32e148b92ac8e6c456157465061c759d267dd9 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.25.0 require ( github.com/JohannesKaufmann/html-to-markdown v1.6.0 github.com/MakeNowJust/heredoc v1.0.0 - github.com/Masterminds/semver/v3 v3.4.0 github.com/PuerkitoBio/goquery v1.10.3 github.com/alecthomas/chroma/v2 v2.20.0 github.com/anthropics/anthropic-sdk-go v1.13.0 diff --git a/go.sum b/go.sum index 5220f4d34a2e5e4926d9c6099285c76c44f2351a..2d53e85a40001ea9241e4c7ee728baa734a889d9 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,6 @@ github.com/JohannesKaufmann/html-to-markdown v1.6.0 h1:04VXMiE50YYfCfLboJCLcgqF5 github.com/JohannesKaufmann/html-to-markdown v1.6.0/go.mod h1:NUI78lGg/a7vpEJTz/0uOcYMaibytE4BUOQS8k78yPQ= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= -github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0= -github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk= github.com/PuerkitoBio/goquery v1.10.3 h1:pFYcNSqHxBD06Fpj/KsbStFRsgRATgnf3LeXiUkhzPo= github.com/PuerkitoBio/goquery v1.10.3/go.mod h1:tMUX0zDMHXYlAQk6p35XxQMqMweEKB7iK7iLNd4RH4Y= diff --git a/internal/app/app.go b/internal/app/app.go index 4e4c7572111059352994af53611b8ca7f8dd3c53..42a417d20d403ae63d90b5845d10cafe76e46722 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -353,7 +353,7 @@ func (app *App) checkForUpdates(ctx context.Context) { checkCtx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() info, err := update.Check(checkCtx) - if err != nil || info == nil || !info.Available { + if err != nil || !info.Available() { return } app.events <- pubsub.UpdateAvailableMsg{ diff --git a/internal/update/update.go b/internal/update/update.go index d48ff6db266c9b4a24f86551eba7077cec01b8ab..100ea63c1f1b589f89b8f32b9a292bcb2fa6bbf8 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -9,7 +9,6 @@ import ( "strings" "time" - "github.com/Masterminds/semver/v3" "github.com/charmbracelet/crush/internal/version" ) @@ -23,35 +22,28 @@ type Info struct { CurrentVersion string LatestVersion string ReleaseURL string - Available bool } +func (i Info) Available() bool { return i.CurrentVersion != i.LatestVersion } + // Check checks if a new version is available. -func Check(ctx context.Context) (*Info, error) { - info := &Info{ +func Check(ctx context.Context) (Info, error) { + info := Info{ CurrentVersion: version.Version, + LatestVersion: version.Version, } - cv, err := semver.NewVersion(version.Version) - if err != nil { - // its devel, unknown, etc + if info.CurrentVersion == "devel" || info.CurrentVersion == "unknown" { return info, nil } release, err := fetchLatestRelease(ctx) if err != nil { - return nil, fmt.Errorf("failed to fetch latest release: %w", err) - } - - lv, err := semver.NewVersion(release.TagName) - if err != nil { - return nil, fmt.Errorf("failed to parse latest version: %w", err) + return info, fmt.Errorf("failed to fetch latest release: %w", err) } info.LatestVersion = strings.TrimPrefix(release.TagName, "v") info.ReleaseURL = release.HTMLURL - info.Available = lv.GreaterThan(cv) - return info, nil } diff --git a/internal/update/update_test.go b/internal/update/update_test.go index 5b387c5b89b160eb3e8455ab9c507d5a2c64b1ee..2da3b61fb72de8fe50e0d266bf8ce19805e24536 100644 --- a/internal/update/update_test.go +++ b/internal/update/update_test.go @@ -1,7 +1,6 @@ package update import ( - "context" "testing" "github.com/charmbracelet/crush/internal/version" @@ -9,18 +8,26 @@ import ( ) func TestCheckForUpdate_DevelopmentVersion(t *testing.T) { - // Test that development versions don't trigger updates. - ctx := context.Background() - - // Temporarily set version to development version. originalVersion := version.Version version.Version = "unknown" - defer func() { + t.Cleanup(func() { version.Version = originalVersion - }() + }) - info, err := Check(ctx) + info, err := Check(t.Context()) + require.NoError(t, err) + require.NotNil(t, info) + require.False(t, info.Available()) +} + +func TestCheckForUpdate_Old(t *testing.T) { + originalVersion := version.Version + version.Version = "0.10.0" + t.Cleanup(func() { + version.Version = originalVersion + }) + info, err := Check(t.Context()) require.NoError(t, err) require.NotNil(t, info) - require.False(t, info.Available) + require.True(t, info.Available()) }