Skip to content

Commit 16137d0

Browse files
authored
Add update command to the cli (#43)
1 parent 5da79a5 commit 16137d0

2 files changed

Lines changed: 132 additions & 1 deletion

File tree

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func checkVersion(cmd *cobra.Command) error {
8484
message := fmt.Sprintf("%s %s",
8585
warningStyle.Render("There's a new version of major available."),
8686
fmt.Sprintf("Run %s to get the newest version.",
87-
commandStyle.Render("brew update && brew upgrade major")))
87+
commandStyle.Render("major update")))
8888

8989
cmd.Println(message)
9090
cmd.Println() // Add a blank line for spacing

cmd/update.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"runtime"
8+
"strings"
9+
10+
"github.com/charmbracelet/lipgloss"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var updateCmd = &cobra.Command{
15+
Use: "update",
16+
Short: "Update the major CLI to the latest version",
17+
Long: `Automatically detects your installation method (brew or direct install) and updates to the latest version.`,
18+
Run: func(cmd *cobra.Command, args []string) {
19+
if err := runUpdate(cmd); err != nil {
20+
errorStyle := lipgloss.NewStyle().
21+
Bold(true).
22+
Foreground(lipgloss.Color("#FF0000"))
23+
24+
cmd.Println(errorStyle.Render(fmt.Sprintf("✗ Update failed: %s", err.Error())))
25+
os.Exit(1)
26+
}
27+
},
28+
}
29+
30+
func init() {
31+
rootCmd.AddCommand(updateCmd)
32+
}
33+
34+
func runUpdate(cmd *cobra.Command) error {
35+
titleStyle := lipgloss.NewStyle().
36+
Bold(true).
37+
Foreground(lipgloss.Color("#87D7FF"))
38+
39+
stepStyle := lipgloss.NewStyle().
40+
Foreground(lipgloss.Color("#87D7FF"))
41+
42+
successStyle := lipgloss.NewStyle().
43+
Bold(true).
44+
Foreground(lipgloss.Color("#00FF00"))
45+
46+
cmd.Println(titleStyle.Render("🔄 Updating Major CLI..."))
47+
cmd.Println()
48+
49+
// Detect installation method
50+
installMethod := detectInstallMethod()
51+
52+
cmd.Println(stepStyle.Render(fmt.Sprintf("▸ Detected installation method: %s", installMethod)))
53+
54+
switch installMethod {
55+
case "brew":
56+
return updateViaBrew(cmd, stepStyle, successStyle)
57+
case "direct":
58+
return updateViaDirect(cmd, stepStyle, successStyle)
59+
default:
60+
return fmt.Errorf("could not detect installation method")
61+
}
62+
}
63+
64+
func detectInstallMethod() string {
65+
// Check if installed via brew
66+
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
67+
// Check if brew is available
68+
if _, err := exec.LookPath("brew"); err == nil {
69+
// Check if major is installed via brew
70+
brewListCmd := exec.Command("brew", "list", "major")
71+
if err := brewListCmd.Run(); err == nil {
72+
return "brew"
73+
}
74+
}
75+
}
76+
77+
// Otherwise assume direct install
78+
return "direct"
79+
}
80+
81+
func updateViaBrew(cmd *cobra.Command, stepStyle, successStyle lipgloss.Style) error {
82+
cmd.Println(stepStyle.Render("▸ Updating via Homebrew..."))
83+
84+
// Update brew first
85+
updateCmd := exec.Command("brew", "update")
86+
updateCmd.Stdout = os.Stdout
87+
updateCmd.Stderr = os.Stderr
88+
if err := updateCmd.Run(); err != nil {
89+
return fmt.Errorf("failed to update Homebrew: %w", err)
90+
}
91+
92+
// Upgrade major
93+
upgradeCmd := exec.Command("brew", "upgrade", "major")
94+
upgradeCmd.Stdout = os.Stdout
95+
upgradeCmd.Stderr = os.Stderr
96+
97+
if err := upgradeCmd.Run(); err != nil {
98+
// Check if it's already up to date
99+
if strings.Contains(err.Error(), "already installed") {
100+
cmd.Println()
101+
cmd.Println(successStyle.Render("✓ Major CLI is already up to date!"))
102+
return nil
103+
}
104+
return fmt.Errorf("failed to upgrade major: %w", err)
105+
}
106+
107+
cmd.Println()
108+
cmd.Println(successStyle.Render("✓ Successfully updated Major CLI!"))
109+
return nil
110+
}
111+
112+
func updateViaDirect(cmd *cobra.Command, stepStyle, successStyle lipgloss.Style) error {
113+
cmd.Println(stepStyle.Render("▸ Downloading latest version..."))
114+
115+
// Use the install script
116+
installScriptURL := "https://raw.githubusercontent.com/major-technology/cli/main/install.sh"
117+
118+
// Download and execute the install script
119+
curlCmd := exec.Command("bash", "-c", fmt.Sprintf("curl -fsSL %s | bash", installScriptURL))
120+
curlCmd.Stdout = os.Stdout
121+
curlCmd.Stderr = os.Stderr
122+
curlCmd.Stdin = os.Stdin // Allow password prompt for sudo
123+
124+
if err := curlCmd.Run(); err != nil {
125+
return fmt.Errorf("failed to download and install update: %w", err)
126+
}
127+
128+
cmd.Println()
129+
cmd.Println(successStyle.Render("✓ Successfully updated Major CLI!"))
130+
return nil
131+
}

0 commit comments

Comments
 (0)