-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ktr
authored
Feb 11, 2018
1 parent
b332725
commit 7b0affd
Showing
10 changed files
with
190 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package update_checker | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ktr0731/evans/meta" | ||
) | ||
|
||
type UpdateChecker struct { | ||
client client | ||
version string | ||
} | ||
|
||
func NewUpdateChecker() *UpdateChecker { | ||
return &UpdateChecker{ | ||
client: newGitHubClient(), | ||
version: meta.Version, | ||
} | ||
} | ||
|
||
type ReleaseTag struct { | ||
LatestVersion string | ||
CurrentIsLatest bool | ||
} | ||
|
||
func (u *UpdateChecker) Check(ctx context.Context) (*ReleaseTag, error) { | ||
tag, err := u.client.FetchLatestTag(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ReleaseTag{ | ||
LatestVersion: tag, | ||
CurrentIsLatest: tag == u.version, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package update_checker | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockClient struct { | ||
tag string | ||
err error | ||
} | ||
|
||
func (c *mockClient) FetchLatestTag(ctx context.Context) (string, error) { | ||
return c.tag, c.err | ||
} | ||
|
||
func TestUpdateChecker(t *testing.T) { | ||
client := &mockClient{ | ||
tag: "0.1.0", | ||
} | ||
u := &UpdateChecker{ | ||
client: client, | ||
version: "0.1.0", | ||
} | ||
|
||
t.Run("latest", func(t *testing.T) { | ||
tag, err := u.Check(context.TODO()) | ||
require.NoError(t, err) | ||
require.True(t, tag.CurrentIsLatest) | ||
require.Equal(t, "0.1.0", tag.LatestVersion) | ||
}) | ||
|
||
t.Run("old", func(t *testing.T) { | ||
client.tag = "0.1.1" | ||
tag, err := u.Check(context.TODO()) | ||
require.NoError(t, err) | ||
require.False(t, tag.CurrentIsLatest) | ||
require.Equal(t, "0.1.1", tag.LatestVersion) | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
client.err = errors.New("an error") | ||
_, err := u.Check(context.TODO()) | ||
require.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package update_checker | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/go-github/github" | ||
) | ||
|
||
const ( | ||
owner = "ktr0731" | ||
repo = "evans" | ||
) | ||
|
||
type client interface { | ||
FetchLatestTag(context.Context) (string, error) | ||
} | ||
|
||
type gitHubClient struct { | ||
client *github.Client | ||
} | ||
|
||
func newGitHubClient() client { | ||
return &gitHubClient{ | ||
client: github.NewClient(nil), | ||
} | ||
} | ||
|
||
func (c *gitHubClient) FetchLatestTag(ctx context.Context) (string, error) { | ||
r, _, err := c.client.Repositories.GetLatestRelease(ctx, owner, repo) | ||
if err != nil { | ||
return "", err | ||
} | ||
return r.GetTagName(), nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package meta | ||
|
||
const ( | ||
Name = "evans" | ||
Version = "0.2.5" | ||
) |