Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/assets/examples/screen-custom-layout-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-upgrade-notice-standalone-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/muesli/reflow v0.3.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
go.szostok.io/version v0.0.0-00010101000000-000000000000
go.szostok.io/version v1.0.0
)

require (
Expand All @@ -39,7 +39,6 @@ require (
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.szostok.io/magex v0.0.0-20220919165319-933b3c21e560 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
Expand Down
2 changes: 0 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
go.szostok.io/magex v0.0.0-20220919165319-933b3c21e560 h1:Yh7zSOeab8IXIUB8EFNerdJ+VrbaIS1+u34vlIrQCCs=
go.szostok.io/magex v0.0.0-20220919165319-933b3c21e560/go.mod h1:KYG9H9qrQdtpoUc2ugjHTeiIKdcnAmaWqeI1arLZOgo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
Expand Down
4 changes: 0 additions & 4 deletions examples/upgrade-notice-standalone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"log"
"os"
"time"

"go.szostok.io/version"
"go.szostok.io/version/style"
Expand All @@ -13,8 +12,6 @@ import (
var layoutGoTpl = `
│ A new release is available: {{ .Version | Red }} → {{ .NewVersion | Green }}
│ {{ .ReleaseURL | Underline | Blue }}
│ {{ "Resolved from cache:" | Italic }} {{ .IsFromCache | FmtBool | Italic }}
`

func main() {
Expand All @@ -23,7 +20,6 @@ func main() {
// Learn more at https://version.szostok.io/customization/upgrade-notice/layout/
GoTemplate: layoutGoTpl,
}),
upgrade.WithMinElapseTimeForRecheck(time.Second * 30),
}

ghUpgrade := upgrade.NewGitHubDetector(
Expand Down
12 changes: 12 additions & 0 deletions examples/upgrade-notice-sub-cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ import (

"go.szostok.io/version"
"go.szostok.io/version/extension"
"go.szostok.io/version/style"
"go.szostok.io/version/upgrade"
)

var layoutGoTpl = `
│ A new release is available: {{ .Version | Red }} → {{ .NewVersion | Green }}
│ {{ .ReleaseURL | Underline | Blue }}
│ {{ "Resolved from cache:" | Italic }} {{ .IsFromCache | FmtBool | Italic }}
`

func NewVersionWithCheck() *cobra.Command {
verCmd := extension.NewVersionCobraCmd()

Expand All @@ -21,6 +29,10 @@ func NewVersionWithCheck() *cobra.Command {
ghUpgrade := upgrade.NewGitHubDetector(
"mszostok", "codeowners-validator",
upgrade.WithMinElapseTimeForRecheck(30*time.Second),
upgrade.WithLayout(&style.Layout{
// Learn more at https://version.szostok.io/customization/upgrade-notice/layout/
GoTemplate: layoutGoTpl,
}),
)
return ghUpgrade.PrintIfFoundGreater(cmd.ErrOrStderr(), version.Get().Version)
},
Expand Down
18 changes: 9 additions & 9 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ var cases = []TestCases{
dir: "upgrade-notice-custom",
},

// Upgrade notice sub-command
// Upgrade notice standalone
{
name: "Should return upgrade notice from sub-command check",
cmd: `version check`,
dir: "upgrade-notice-sub-cmd",
name: "Should return upgrade notice from standalone command",
cmd: ``,
dir: "upgrade-notice-standalone",
},
}

Expand Down Expand Up @@ -286,27 +286,27 @@ func TestExamplesRecheckInterval(t *testing.T) {
t.Parallel()

// given
binaryPath := buildBinaryAllLDFlags(t, "upgrade-notice-standalone")
binaryPath := buildBinaryAllLDFlags(t, "upgrade-notice-sub-cmd")

// when
result, err := Exec(binaryPath, "").
result, err := Exec(binaryPath, "version check").
AwaitResultAtMost(10 * time.Second)

// then
// Should return upgrade notice for the first time
require.NoError(t, err)
require.NoError(t, err, result.Stderr)
assert.Equal(t, 0, result.ExitCode)

data := result.Stdout + result.Stderr
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), []byte(data))

// when
result, err = Exec(binaryPath, "").
result, err = Exec(binaryPath, "version check").
AwaitResultAtMost(10 * time.Second)

// then
require.NoError(t, err)
require.NoError(t, err, result.Stderr)
assert.Equal(t, 0, result.ExitCode)
data = result.Stdout + result.Stderr
g = goldie.New(t, goldie.WithNameSuffix("recheck.golden.txt"))
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func (s *Executor) AwaitResultAtMost(timeout time.Duration) (*ExecuteOutput, err

exitCode, err := s.execute(timeout, &buffOut, &buffErr)
if err != nil {
return nil, err
return &ExecuteOutput{
ExitCode: exitCode,
Stdout: buffOut.String(),
Stderr: buffErr.String(),
}, err
}

return &ExecuteOutput{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

│ A new release is available: 0.6.1 → v0.7.4
│ A new release is available: 0.6.1 → v0.7.4
│ https://github.com/mszostok/codeowners-validator/releases/tag/v0.7.4
File renamed without changes.
119 changes: 0 additions & 119 deletions upgrade/github-rel-fetcher.go

This file was deleted.

46 changes: 38 additions & 8 deletions upgrade/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"go.szostok.io/version/style"
"go.szostok.io/version/term"
"go.szostok.io/version/upgrade/github"
)

var defaultLayoutGoTpl = `
Expand Down Expand Up @@ -76,27 +77,47 @@ type LookForLatestReleaseInput struct {
CurrentVersion string
}

// LookForLatestRelease if a given time elapsed, check project's latest release.
// LookForLatestRelease checks project's latest release. In most cases, PrintIfFoundGreater is better option.
//
// Deprecated and will be removed in 1.4.0 release, use LookForGreaterRelease instead.
//
// Old: LookForLatestRelease(upgrade.LookForLatestReleaseInput{CurrentVersion: currentVersion})
// New: LookForGreaterRelease(upgrade.LookForGreaterReleaseInput{CurrentVersion: currentVersion})
func (gh *GitHubDetector) LookForLatestRelease(in LookForLatestReleaseInput) (LookForLatestReleaseOutput, error) {
var empty LookForLatestReleaseOutput
out, err := gh.LookForGreaterRelease(LookForGreaterReleaseInput(in))
return LookForLatestReleaseOutput(out), err
}

// LookForGreaterReleaseOutput holds output data for LookForGreaterRelease function.
type LookForGreaterReleaseOutput struct {
Found bool
ReleaseInfo *Info
}

// LookForGreaterReleaseInput holds input data for LookForGreaterRelease function.
type LookForGreaterReleaseInput struct {
CurrentVersion string
}

// LookForGreaterRelease checks project's latest release. In most cases, PrintIfFoundGreater is better option.
func (gh *GitHubDetector) LookForGreaterRelease(in LookForGreaterReleaseInput) (LookForGreaterReleaseOutput, error) {
var empty LookForGreaterReleaseOutput
if err := gh.validate(); err != nil { // TODO: move to constructor
return empty, err
}

ctx, cancel := context.WithTimeout(context.Background(), gh.updateCheckTimeout)
defer cancel()

statePath := gh.getStateFilePath()

rel, err := GetLatestRelease(ctx, statePath, gh.repo, gh.recheckInterval)
rel, err := gh.getReleaseInfo(ctx)
if err != nil || rel == nil {
return empty, err
}
if !gh.isVerGreater(in.CurrentVersion, rel.Version) {
return empty, err
}

return LookForLatestReleaseOutput{
return LookForGreaterReleaseOutput{
Found: true,
ReleaseInfo: &Info{
Version: in.CurrentVersion,
Expand All @@ -108,9 +129,9 @@ func (gh *GitHubDetector) LookForLatestRelease(in LookForLatestReleaseInput) (Lo
}

// PrintIfFoundGreater prints an upgrade notice if a newer version is available.
// It's a syntax sugar for using the LookForLatestRelease and Render functions.
// It's a syntax sugar for using the LookForGreaterRelease and Render functions.
func (gh *GitHubDetector) PrintIfFoundGreater(w io.Writer, currentVersion string) error {
rel, err := gh.LookForLatestRelease(LookForLatestReleaseInput{
rel, err := gh.LookForGreaterRelease(LookForGreaterReleaseInput{
CurrentVersion: currentVersion,
})
if err != nil {
Expand All @@ -129,6 +150,15 @@ func (gh *GitHubDetector) PrintIfFoundGreater(w io.Writer, currentVersion string
return err
}

func (gh *GitHubDetector) getReleaseInfo(ctx context.Context) (*github.ReleaseInfoResponse, error) {
if gh.recheckInterval == 0 {
return github.GetLatestRelease(ctx, gh.repo)
}

statePath := gh.getStateFilePath()
return github.GetLatestReleaseWithCache(ctx, gh.repo, statePath, gh.recheckInterval)
}

func (gh *GitHubDetector) getStateFilePath() string {
if gh.configDir != "" {
return filepath.Join(gh.configDir, gh.stateFileName)
Expand Down
Loading