-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from CircleCI-Public/update-check
[CIRCLE-13826]: Periodic update check for user
- Loading branch information
Showing
32 changed files
with
2,602 additions
and
113 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cmd | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/logger" | ||
"github.com/CircleCI-Public/circleci-cli/settings" | ||
"github.com/CircleCI-Public/circleci-cli/update" | ||
"github.com/CircleCI-Public/circleci-cli/version" | ||
"github.com/briandowns/spinner" | ||
) | ||
|
||
func checkForUpdates(opts *settings.Config) error { | ||
if opts.SkipUpdateCheck { | ||
return nil | ||
} | ||
|
||
updateCheck := &settings.UpdateCheck{ | ||
LastUpdateCheck: time.Time{}, | ||
} | ||
|
||
err := updateCheck.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if update.ShouldCheckForUpdates(updateCheck) { | ||
log := logger.NewLogger(opts.Debug) | ||
slug := "CircleCI-Public/circleci-cli" | ||
|
||
spr := spinner.New(spinner.CharSets[14], 100*time.Millisecond) | ||
spr.Suffix = " Checking for updates..." | ||
spr.Start() | ||
|
||
check, err := update.CheckForUpdates(opts.GitHubAPI, slug, version.Version, PackageManager) | ||
|
||
if err != nil { | ||
spr.Stop() | ||
return err | ||
} | ||
|
||
if !check.Found { | ||
spr.Suffix = "No updates found." | ||
time.Sleep(300 * time.Millisecond) | ||
spr.Stop() | ||
|
||
updateCheck.LastUpdateCheck = time.Now() | ||
err = updateCheck.WriteToDisk() | ||
return err | ||
} | ||
|
||
if update.IsLatestVersion(check) { | ||
spr.Suffix = "Already up-to-date." | ||
time.Sleep(300 * time.Millisecond) | ||
spr.Stop() | ||
|
||
updateCheck.LastUpdateCheck = time.Now() | ||
err = updateCheck.WriteToDisk() | ||
return err | ||
} | ||
spr.Stop() | ||
|
||
log.Debug(update.DebugVersion(check)) | ||
log.Info(update.ReportVersion(check)) | ||
log.Info(update.HowToUpdate(check)) | ||
|
||
log.Info("\n") // Print a new-line after all of that | ||
|
||
updateCheck.LastUpdateCheck = time.Now() | ||
err = updateCheck.WriteToDisk() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return 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,128 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/settings" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/gbytes" | ||
"github.com/onsi/gomega/gexec" | ||
"github.com/onsi/gomega/ghttp" | ||
) | ||
|
||
var _ = Describe("Check", func() { | ||
var ( | ||
command *exec.Cmd | ||
err error | ||
checkCLI string | ||
tempHome string | ||
testServer *ghttp.Server | ||
updateCheck *settings.UpdateCheck | ||
updateFile *os.File | ||
) | ||
|
||
BeforeEach(func() { | ||
checkCLI, err = gexec.Build("github.com/CircleCI-Public/circleci-cli") | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
tempHome, _, updateFile = withTempSettings() | ||
|
||
updateCheck = &settings.UpdateCheck{ | ||
LastUpdateCheck: time.Time{}, | ||
} | ||
|
||
updateCheck.FileUsed = updateFile.Name() | ||
err = updateCheck.WriteToDisk() | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
testServer = ghttp.NewServer() | ||
|
||
command = exec.Command(checkCLI, "help", | ||
"--github-api", testServer.URL(), | ||
) | ||
|
||
command.Env = append(os.Environ(), | ||
fmt.Sprintf("HOME=%s", tempHome), | ||
) | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(os.RemoveAll(tempHome)).To(Succeed()) | ||
}) | ||
|
||
Describe("update auto checks with a new release", func() { | ||
var ( | ||
response string | ||
) | ||
|
||
BeforeEach(func() { | ||
checkCLI, err = gexec.Build("github.com/CircleCI-Public/circleci-cli", | ||
"-ldflags", | ||
"-X github.com/CircleCI-Public/circleci-cli/cmd.AutoUpdate=false -X github.com/CircleCI-Public/circleci-cli/cmd.PackageManager=release", | ||
) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
tempHome, _, _ = withTempSettings() | ||
|
||
command = exec.Command(checkCLI, "help", | ||
"--github-api", testServer.URL(), | ||
) | ||
|
||
command.Env = append(os.Environ(), | ||
fmt.Sprintf("HOME=%s", tempHome), | ||
) | ||
|
||
response = ` | ||
[ | ||
{ | ||
"id": 1, | ||
"tag_name": "v1.0.0", | ||
"name": "v1.0.0", | ||
"published_at": "2013-02-27T19:35:32Z", | ||
"assets": [ | ||
{ | ||
"id": 1, | ||
"name": "linux_amd64.zip", | ||
"label": "short description", | ||
"content_type": "application/zip", | ||
"size": 1024 | ||
} | ||
] | ||
} | ||
] | ||
` | ||
|
||
testServer.AppendHandlers( | ||
ghttp.CombineHandlers( | ||
ghttp.VerifyRequest("GET", "/repos/CircleCI-Public/circleci-cli/releases"), | ||
ghttp.RespondWith(http.StatusOK, response), | ||
), | ||
) | ||
|
||
}) | ||
|
||
AfterEach(func() { | ||
testServer.Close() | ||
}) | ||
|
||
Context("using a binary release", func() { | ||
It("with flag should tell the user how to update and install", func() { | ||
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
Eventually(session.Err.Contents()).Should(BeEmpty()) | ||
|
||
Eventually(session.Out).Should(gbytes.Say("You are running 0.0.0-dev")) | ||
Eventually(session.Out).Should(gbytes.Say("A new release is available (.*)")) | ||
Eventually(session.Out).Should(gbytes.Say("You can update with `circleci update install`")) | ||
|
||
Eventually(session).Should(gexec.Exit(0)) | ||
}) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.