-
Notifications
You must be signed in to change notification settings - Fork 13
feat: introduce version info & update command #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
76070bc
Introduce version info & update command
irisyann 628067e
Fix lint error
irisyann b2a1b69
Update readme
irisyann f2f1417
Ensure version prefix is trimmed before running comparison
irisyann c56f82f
Add test cases for checker and update
irisyann 351b747
Fix lint issues
irisyann 03bcd45
Apply copilot feedback
irisyann 6ce7f58
refactor: centralise semver validation and guard update cache against…
irisyann f0cfb42
test: extract classifyInstallPath and add tests for install method de…
irisyann 1d31f0a
Update docs to guide users to alias the coingecko-cli binary to cg
irisyann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,137 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/charmbracelet/huh" | ||
| "github.com/coingecko/coingecko-cli/internal/updater" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var fetchLatestFunc = updater.FetchLatest | ||
|
|
||
| var updateCmd = &cobra.Command{ | ||
| Use: "update", | ||
| Short: "Upgrade the CLI to the latest version", | ||
| RunE: runUpdate, | ||
| } | ||
|
|
||
| func init() { | ||
| updateCmd.Flags().String("method", "", "Install method override (homebrew, go, script)") | ||
| rootCmd.AddCommand(updateCmd) | ||
| } | ||
|
|
||
| func runUpdate(cmd *cobra.Command, args []string) error { | ||
| method, _ := cmd.Flags().GetString("method") | ||
| if method == "" { | ||
| method = detectInstallMethod() | ||
| } else { | ||
| switch method { | ||
| case "homebrew", "go", "script": | ||
| default: | ||
| return fmt.Errorf("unknown install method %q — must be one of: homebrew, go, script", method) | ||
| } | ||
|
irisyann marked this conversation as resolved.
|
||
| } | ||
|
|
||
| warnf("Checking for updates...\n") | ||
| latest, err := fetchLatestFunc() | ||
| if err != nil { | ||
| return fmt.Errorf("checking for updates: %w", err) | ||
| } | ||
| if !updater.ValidVersion(latest) { | ||
| return fmt.Errorf("unexpected version format from GitHub: %q", latest) | ||
| } | ||
|
|
||
| currentVer := strings.TrimPrefix(version, "v") | ||
| if latest == currentVer { | ||
| warnf("Already up to date (%s).\n", version) | ||
| return nil | ||
| } | ||
| if updater.VersionGreater(currentVer, latest) { | ||
| warnf("Current version (v%s) is ahead of the latest release (v%s).\n", currentVer, latest) | ||
| return nil | ||
| } | ||
|
|
||
| warnf("Current: v%s → Latest: v%s (install via: %s)\n\n", currentVer, latest, method) | ||
|
|
||
| var confirmed bool | ||
| if err := huh.NewConfirm(). | ||
| Title(fmt.Sprintf("Update cg v%s → v%s?", currentVer, latest)). | ||
| Value(&confirmed). | ||
| Run(); err != nil { | ||
| if errors.Is(err, huh.ErrUserAborted) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
| if !confirmed { | ||
| return nil | ||
| } | ||
|
|
||
| return runInstallCommand(method) | ||
| } | ||
|
|
||
| func detectInstallMethod() string { | ||
| exe, err := os.Executable() | ||
| if err != nil { | ||
| return "script" | ||
| } | ||
| exe, err = filepath.EvalSymlinks(exe) | ||
| if err != nil { | ||
| return "script" | ||
| } | ||
| return classifyInstallPath(exe) | ||
| } | ||
|
|
||
| // classifyInstallPath returns the install method ("homebrew", "go", or "script") | ||
| // for a resolved executable path. | ||
| func classifyInstallPath(exe string) string { | ||
| if strings.Contains(exe, "/Cellar/") || | ||
| strings.Contains(exe, "/homebrew/") || | ||
| strings.Contains(exe, "/opt/homebrew/") { | ||
| return "homebrew" | ||
| } | ||
|
|
||
| gobin := os.Getenv("GOBIN") | ||
| if gobin == "" { | ||
| gopath := os.Getenv("GOPATH") | ||
| if gopath == "" { | ||
| home, _ := os.UserHomeDir() | ||
| gopath = filepath.Join(home, "go") | ||
| } | ||
| gobin = filepath.Join(gopath, "bin") | ||
| } | ||
| if strings.HasPrefix(exe, gobin+string(filepath.Separator)) { | ||
| return "go" | ||
| } | ||
|
|
||
| return "script" | ||
| } | ||
|
|
||
| func runInstallCommand(method string) error { | ||
| var name string | ||
| var args []string | ||
| switch method { | ||
| case "homebrew": | ||
| name = "brew" | ||
| args = []string{"upgrade", "coingecko/coingecko-cli/cg"} | ||
| case "go": | ||
| warnf("Note: 'go install' produces a binary named 'coingecko-cli'. If you invoke this CLI as 'cg', make sure you have an alias or symlink (e.g. alias cg=coingecko-cli).\n\n") | ||
| name = "go" | ||
| args = []string{"install", "github.com/coingecko/coingecko-cli@latest"} | ||
|
irisyann marked this conversation as resolved.
|
||
| default: // "script" | ||
| name = "sh" | ||
| args = []string{"-c", "curl -fsSL https://raw.githubusercontent.com/coingecko/coingecko-cli/main/install.sh | sh"} | ||
| } | ||
|
irisyann marked this conversation as resolved.
|
||
|
|
||
| c := exec.Command(name, args...) | ||
| c.Stdin = os.Stdin | ||
| c.Stdout = os.Stdout | ||
| c.Stderr = os.Stderr | ||
| return c.Run() | ||
| } | ||
This file contains hidden or 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,158 @@ | ||
| package cmd | ||
|
irisyann marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/coingecko/coingecko-cli/internal/updater" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestValidVersion(t *testing.T) { | ||
| valid := []string{"1.2.3", "0.0.1", "10.20.30", "0.0.0"} | ||
| for _, v := range valid { | ||
| assert.True(t, updater.ValidVersion(v), "expected valid: %s", v) | ||
| } | ||
|
|
||
| invalid := []string{"v1.2.3", "1.2", "1.2.3.4", "1.2.x", "", "abc", "1.2.", "1.2.3-rc1"} | ||
| for _, v := range invalid { | ||
| assert.False(t, updater.ValidVersion(v), "expected invalid: %s", v) | ||
| } | ||
| } | ||
|
|
||
| func TestRunUpdate_AlreadyUpToDate(t *testing.T) { | ||
| orig := fetchLatestFunc | ||
| fetchLatestFunc = func() (string, error) { return "1.2.3", nil } | ||
| t.Cleanup(func() { fetchLatestFunc = orig }) | ||
|
|
||
| origVersion := version | ||
| version = "1.2.3" | ||
| t.Cleanup(func() { version = origVersion }) | ||
|
|
||
| err := updateCmd.RunE(updateCmd, nil) | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestRunUpdate_CurrentAhead(t *testing.T) { | ||
| orig := fetchLatestFunc | ||
| fetchLatestFunc = func() (string, error) { return "1.0.0", nil } | ||
| t.Cleanup(func() { fetchLatestFunc = orig }) | ||
|
|
||
| origVersion := version | ||
| version = "2.0.0" | ||
| t.Cleanup(func() { version = origVersion }) | ||
|
|
||
| err := updateCmd.RunE(updateCmd, nil) | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestRunUpdate_InvalidVersionFromGitHub(t *testing.T) { | ||
| orig := fetchLatestFunc | ||
| fetchLatestFunc = func() (string, error) { return "not-a-version", nil } | ||
| t.Cleanup(func() { fetchLatestFunc = orig }) | ||
|
|
||
| origVersion := version | ||
| version = "1.2.3" | ||
| t.Cleanup(func() { version = origVersion }) | ||
|
|
||
| err := updateCmd.RunE(updateCmd, nil) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "unexpected version format") | ||
| } | ||
|
|
||
| func TestRunUpdate_InvalidMethod(t *testing.T) { | ||
| require.NoError(t, updateCmd.Flags().Set("method", "invalid")) | ||
| t.Cleanup(func() { _ = updateCmd.Flags().Set("method", "") }) | ||
|
|
||
| err := updateCmd.RunE(updateCmd, nil) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "unknown install method") | ||
| } | ||
|
|
||
| func TestClassifyInstallPath_Homebrew(t *testing.T) { | ||
| cases := []struct { | ||
| path string | ||
| desc string | ||
| }{ | ||
| {"/usr/local/Cellar/cg/1.0.0/bin/cg", "Cellar path"}, | ||
| {"/home/linuxbrew/.linuxbrew/homebrew/bin/cg", "homebrew path"}, | ||
| {"/opt/homebrew/bin/cg", "opt/homebrew path"}, | ||
| } | ||
| for _, tc := range cases { | ||
| assert.Equal(t, "homebrew", classifyInstallPath(tc.path), tc.desc) | ||
| } | ||
| } | ||
|
|
||
| func TestClassifyInstallPath_Go_ExplicitGOBIN(t *testing.T) { | ||
| gobin := t.TempDir() | ||
| t.Setenv("GOBIN", gobin) | ||
| t.Setenv("GOPATH", "") | ||
|
|
||
| exe := filepath.Join(gobin, "cg") | ||
| assert.Equal(t, "go", classifyInstallPath(exe)) | ||
| } | ||
|
|
||
| func TestClassifyInstallPath_Go_DefaultGOPATH(t *testing.T) { | ||
| home, err := os.UserHomeDir() | ||
| require.NoError(t, err) | ||
|
|
||
| t.Setenv("GOBIN", "") | ||
| t.Setenv("GOPATH", "") | ||
|
|
||
| exe := filepath.Join(home, "go", "bin", "cg") | ||
| assert.Equal(t, "go", classifyInstallPath(exe)) | ||
| } | ||
|
|
||
| func TestClassifyInstallPath_Go_ExplicitGOPATH(t *testing.T) { | ||
| gopath := t.TempDir() | ||
| t.Setenv("GOBIN", "") | ||
| t.Setenv("GOPATH", gopath) | ||
|
|
||
| exe := filepath.Join(gopath, "bin", "cg") | ||
| assert.Equal(t, "go", classifyInstallPath(exe)) | ||
| } | ||
|
|
||
| func TestClassifyInstallPath_Script(t *testing.T) { | ||
| t.Setenv("GOBIN", "") | ||
| t.Setenv("GOPATH", "") | ||
|
|
||
| cases := []string{ | ||
| "/usr/local/bin/cg", | ||
| "/home/user/.local/bin/cg", | ||
| "/tmp/cg", | ||
| } | ||
| for _, exe := range cases { | ||
| assert.Equal(t, "script", classifyInstallPath(exe), "path: %s", exe) | ||
| } | ||
| } | ||
|
|
||
| func TestClassifyInstallPath_GoBinNotParentDir(t *testing.T) { | ||
| gobin := "/home/user/go/bin" | ||
| t.Setenv("GOBIN", gobin) | ||
|
|
||
| // A path that starts with the gobin string but isn't under it (no separator) | ||
| exe := gobin + "extra/cg" | ||
| assert.Equal(t, "script", classifyInstallPath(exe)) | ||
| } | ||
|
|
||
| func TestDetectInstallMethod_ReturnsValidMethod(t *testing.T) { | ||
| method := detectInstallMethod() | ||
| assert.Contains(t, []string{"homebrew", "go", "script"}, method) | ||
| } | ||
|
|
||
| func TestRunUpdate_FetchError(t *testing.T) { | ||
| orig := fetchLatestFunc | ||
| fetchLatestFunc = func() (string, error) { return "", fmt.Errorf("network timeout") } | ||
| t.Cleanup(func() { fetchLatestFunc = orig }) | ||
|
|
||
| origVersion := version | ||
| version = "1.2.3" | ||
| t.Cleanup(func() { version = origVersion }) | ||
|
|
||
| err := updateCmd.RunE(updateCmd, nil) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "network timeout") | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.