forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
50 lines (39 loc) · 1.32 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"net/http"
"time"
"github.com/google/go-github/github"
)
// githubTimeout defines how long to wait for a response from GitHub
// when checking for new SAM Local versions.
const githubTimeout = 3
// checkVersionResult contains information on the current version of AWS SAM CLI, and
// whether there are any newer versions available to upgrade to.
type checkVersionResult struct {
IsUpToDate bool
LatestVersion string
}
// checkVersion checks whether the current version of AWS SAM CLI is the latest
func checkVersion() (*checkVersionResult, error) {
const RepoOwner = "awslabs"
const RepoName = "aws-sam-local"
// Create a HTTP client with appropriate timeouts configured
client := &http.Client{
Timeout: time.Duration(githubTimeout * time.Second),
}
// Get the latest version details from Github release
gh := github.NewClient(client)
releases, _, err := gh.Repositories.ListReleases(context.Background(), RepoOwner, RepoName, nil)
if err != nil || len(releases) == 0 {
return &checkVersionResult{}, err
}
// Grab the latest release - without the first 'v' character from the tag
// ie. v0.0.1 -> 0.0.1
latest := releases[0]
latestVersion := (*latest.TagName)[1:]
return &checkVersionResult{
LatestVersion: latestVersion,
IsUpToDate: version == latestVersion,
}, nil
}