-
Notifications
You must be signed in to change notification settings - Fork 38
feat(BRE2-810): sudo gating, brev upgrade #320
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
Merged
Merged
Changes from all commits
Commits
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
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,32 @@ | ||
| package upgrade | ||
|
|
||
| import "os/exec" | ||
|
|
||
| // InstallMethod represents how brev was installed on the system. | ||
| type InstallMethod int | ||
|
|
||
| const ( | ||
| // InstallMethodDirect means brev was installed via direct binary download. | ||
| InstallMethodDirect InstallMethod = 0 | ||
| // InstallMethodBrew means brev was installed via Homebrew. | ||
| InstallMethodBrew InstallMethod = 1 | ||
| ) | ||
|
|
||
| // Detector determines how brev was installed. | ||
| type Detector interface { | ||
| Detect() InstallMethod | ||
| } | ||
|
|
||
| // SystemDetector checks the actual system for install method. | ||
| type SystemDetector struct{} | ||
|
|
||
| // Detect checks whether brev was installed via Homebrew or direct download. | ||
| func (SystemDetector) Detect() InstallMethod { | ||
| if _, err := exec.LookPath("brew"); err != nil { | ||
| return InstallMethodDirect | ||
| } | ||
| if exec.Command("brew", "list", "brev").Run() == nil { //nolint:gosec // intentional brew probe | ||
| return InstallMethodBrew | ||
| } | ||
| return InstallMethodDirect | ||
| } |
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,48 @@ | ||
| package upgrade | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
|
|
||
| "github.com/brevdev/brev-cli/pkg/terminal" | ||
| ) | ||
|
|
||
| const installScriptURL = "https://raw.githubusercontent.com/brevdev/brev-cli/main/bin/install-latest.sh" | ||
|
|
||
| // Upgrader executes the actual upgrade process. | ||
| type Upgrader interface { | ||
| UpgradeViaBrew(t *terminal.Terminal) error | ||
| UpgradeViaInstallScript(t *terminal.Terminal) error | ||
| } | ||
|
|
||
| // SystemUpgrader executes upgrade commands on the real system. | ||
| type SystemUpgrader struct{} | ||
|
|
||
| // UpgradeViaBrew runs "brew upgrade brev" with output connected to the terminal. | ||
| func (SystemUpgrader) UpgradeViaBrew(t *terminal.Terminal) error { | ||
| t.Vprint("Running: brew upgrade brev") | ||
| t.Vprint("") | ||
|
|
||
| cmd := exec.Command("brew", "upgrade", "brev") | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
| if err := cmd.Run(); err != nil { | ||
| return fmt.Errorf("brew upgrade failed: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // UpgradeViaInstallScript runs the upstream install-latest.sh script via sudo. | ||
| func (SystemUpgrader) UpgradeViaInstallScript(t *terminal.Terminal) error { | ||
| t.Vprintf("Running: bash -c \"$(curl -fsSL %s)\"\n", installScriptURL) | ||
| t.Vprint("") | ||
|
|
||
| cmd := exec.Command("bash", "-c", fmt.Sprintf(`curl -fsSL "%s" | bash`, installScriptURL)) //nolint:gosec // URL is a compile-time constant | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
| if err := cmd.Run(); err != nil { | ||
| return fmt.Errorf("install script failed: %w", err) | ||
| } | ||
| return nil | ||
| } |
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,162 @@ | ||
| // Package upgrade provides the brev upgrade command. | ||
| package upgrade | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/brevdev/brev-cli/pkg/cmd/agentskill" | ||
| "github.com/brevdev/brev-cli/pkg/cmd/register" | ||
| "github.com/brevdev/brev-cli/pkg/cmd/version" | ||
| "github.com/brevdev/brev-cli/pkg/store" | ||
| "github.com/brevdev/brev-cli/pkg/sudo" | ||
| "github.com/brevdev/brev-cli/pkg/terminal" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // VersionStore fetches the latest release metadata from GitHub. | ||
| type VersionStore interface { | ||
| GetLatestReleaseMetadata() (*store.GithubReleaseMetadata, error) | ||
| } | ||
|
|
||
| // SkillInstaller updates agent skill files after a binary upgrade. | ||
| type SkillInstaller interface { | ||
| InstallSkill(t *terminal.Terminal) error | ||
| } | ||
|
|
||
| // defaultSkillInstaller calls agentskill.InstallSkill using the real home directory. | ||
| type defaultSkillInstaller struct{} | ||
|
|
||
| func (defaultSkillInstaller) InstallSkill(t *terminal.Terminal) error { | ||
| homeDir, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return fmt.Errorf("could not determine home directory: %w", err) | ||
| } | ||
| if err := agentskill.InstallSkill(t, homeDir, false); err != nil { | ||
| return fmt.Errorf("install skill: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type upgradeDeps struct { | ||
| detector Detector | ||
| upgrader Upgrader | ||
| confirmer terminal.Confirmer | ||
| skillInstaller SkillInstaller | ||
| } | ||
|
|
||
| func defaultUpgradeDeps() upgradeDeps { | ||
| return upgradeDeps{ | ||
| detector: SystemDetector{}, | ||
| upgrader: SystemUpgrader{}, | ||
| confirmer: register.TerminalPrompter{}, | ||
| skillInstaller: defaultSkillInstaller{}, | ||
| } | ||
| } | ||
|
|
||
| var ( | ||
| upgradeLong = "Upgrade brev to the latest version." | ||
| upgradeExample = " brev upgrade" | ||
| ) | ||
|
|
||
| // NewCmdUpgrade creates the brev upgrade command. | ||
| func NewCmdUpgrade(t *terminal.Terminal, versionStore VersionStore) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Annotations: map[string]string{"configuration": ""}, | ||
| Use: "upgrade", | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Upgrade brev to the latest version", | ||
| Long: upgradeLong, | ||
| Example: upgradeExample, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return runUpgrade(t, versionStore, defaultUpgradeDeps()) | ||
| }, | ||
| } | ||
| return cmd | ||
| } | ||
|
|
||
| func runUpgrade(t *terminal.Terminal, vs VersionStore, deps upgradeDeps) error { | ||
| t.Vprint("") | ||
| t.Vprintf("Current version: %s\n", version.Version) | ||
|
|
||
| release, err := vs.GetLatestReleaseMetadata() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to check latest version: %w", err) | ||
| } | ||
|
|
||
| if release.TagName == version.Version { | ||
| t.Vprint(t.Green("Already up to date.")) | ||
| return nil | ||
| } | ||
|
|
||
| t.Vprintf("New version available: %s\n", release.TagName) | ||
| t.Vprint("") | ||
|
|
||
| method := deps.detector.Detect() | ||
|
|
||
| var ( | ||
| upgraded bool | ||
| upgradeErr error | ||
| ) | ||
| switch method { | ||
| case InstallMethodBrew: | ||
| upgraded, upgradeErr = upgradeViaBrew(t, deps) | ||
| case InstallMethodDirect: | ||
| upgraded, upgradeErr = upgradeViaDirect(t, deps) | ||
| default: | ||
| return fmt.Errorf("unknown install method") | ||
| } | ||
|
|
||
| if upgradeErr != nil { | ||
| return upgradeErr | ||
| } | ||
|
|
||
| if upgraded { | ||
| if err := deps.skillInstaller.InstallSkill(t); err != nil { | ||
| t.Vprintf(" Warning: skill update failed: %v\n", err) | ||
| t.Vprintf(" You can retry with: brev agent-skill install\n") | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func upgradeViaBrew(t *terminal.Terminal, deps upgradeDeps) (bool, error) { | ||
| t.Vprint("Detected install method: Homebrew") | ||
| t.Vprint("This will run: brew upgrade brev") | ||
| t.Vprint("") | ||
|
|
||
| if !deps.confirmer.ConfirmYesNo("Proceed with upgrade?") { | ||
| t.Vprint("Upgrade canceled.") | ||
| return false, nil | ||
| } | ||
|
|
||
| t.Vprint("") | ||
| if err := deps.upgrader.UpgradeViaBrew(t); err != nil { | ||
| return false, fmt.Errorf("brew upgrade: %w", err) | ||
| } | ||
|
|
||
| t.Vprint("") | ||
| t.Vprint(t.Green("Upgrade complete.")) | ||
| return true, nil | ||
| } | ||
|
|
||
| func upgradeViaDirect(t *terminal.Terminal, deps upgradeDeps) (bool, error) { | ||
| t.Vprint("Detected install method: direct binary install") | ||
| t.Vprint("This will download the latest release and install it to /usr/local/bin/brev") | ||
| t.Vprint("") | ||
|
|
||
| if err := sudo.Gate(t, deps.confirmer, "Upgrade"); err != nil { | ||
| return false, fmt.Errorf("sudo issue: %w", err) | ||
| } | ||
|
|
||
| t.Vprint("") | ||
| if err := deps.upgrader.UpgradeViaInstallScript(t); err != nil { | ||
| return false, fmt.Errorf("direct upgrade: %w", err) | ||
| } | ||
|
|
||
| t.Vprint("") | ||
| t.Vprint(t.Green("Upgrade complete.")) | ||
| return true, nil | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also install the latest skill?