Skip to content

Commit 591751b

Browse files
feat: add spinner in upgrade
1 parent f94b4e1 commit 591751b

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

cmd/upgrade/upgrade.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,13 @@ func runUpgrade() error {
113113
return fmt.Errorf("checksum URL failed validation: %w", err)
114114
}
115115

116-
spinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s...", assetName))
117-
118-
tmp, err := downloadToTemp(downloadURL)
116+
tmp, err := downloadToTemp(downloadURL, assetName)
119117
if err != nil {
120-
spinner.Fail("Download failed")
121118
return fmt.Errorf("could not download update: %w", err)
122119
}
123120
defer os.Remove(tmp) //nolint:errcheck
124121

125-
spinner.UpdateText("Verifying checksum...")
122+
spinner, _ := pterm.DefaultSpinner.Start("Verifying checksum...")
126123

127124
expectedHash, err := fetchChecksum(checksumURL)
128125
if err != nil {
@@ -135,7 +132,7 @@ func runUpgrade() error {
135132
return err
136133
}
137134

138-
spinner.Success("Downloaded and verified")
135+
spinner.Success("Checksum verified")
139136

140137
exe, err := os.Executable()
141138
if err != nil {
@@ -208,7 +205,22 @@ func binaryAssetName() string {
208205
return name
209206
}
210207

211-
func downloadToTemp(rawURL string) (string, error) {
208+
// progressWriter tracks bytes written and updates a pterm progress bar in KB increments.
209+
type progressWriter struct {
210+
bar *pterm.ProgressbarPrinter
211+
buf int64
212+
}
213+
214+
func (pw *progressWriter) Write(p []byte) (int, error) {
215+
pw.buf += int64(len(p))
216+
for pw.buf >= 1024 {
217+
pw.bar.Add(1)
218+
pw.buf -= 1024
219+
}
220+
return len(p), nil
221+
}
222+
223+
func downloadToTemp(rawURL, assetName string) (string, error) {
212224
ctx, cancel := context.WithTimeout(context.Background(), downloadTimeout)
213225
defer cancel()
214226

@@ -234,7 +246,30 @@ func downloadToTemp(rawURL string) (string, error) {
234246
}
235247
defer tmp.Close() //nolint:errcheck
236248

237-
if _, err := io.Copy(tmp, io.LimitReader(resp.Body, maxBinarySize)); err != nil {
249+
limited := io.LimitReader(resp.Body, maxBinarySize)
250+
251+
totalBytes := resp.ContentLength
252+
if totalBytes > 0 {
253+
totalKB := int(totalBytes / 1024)
254+
bar, _ := pterm.DefaultProgressbar.
255+
WithTotal(totalKB).
256+
WithTitle(fmt.Sprintf("Downloading %s (%.1f MB)", assetName, float64(totalBytes)/1024/1024)).
257+
Start()
258+
pw := &progressWriter{bar: bar}
259+
_, err = io.Copy(tmp, io.TeeReader(limited, pw))
260+
_, _ = bar.Stop()
261+
} else {
262+
// Content-Length unknown — fall back to spinner
263+
spinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s...", assetName))
264+
_, err = io.Copy(tmp, limited)
265+
if err != nil {
266+
spinner.Fail("Download failed")
267+
} else {
268+
spinner.Success("Downloaded")
269+
}
270+
}
271+
272+
if err != nil {
238273
_ = os.Remove(tmp.Name())
239274
return "", err
240275
}

install.sh

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ resolve_version() {
109109
info "Fetching latest stable release..."
110110
LATEST_URL="https://api.github.com/repos/${GITHUB_REPO}/releases/latest"
111111
if command -v curl > /dev/null 2>&1; then
112-
VERSION="$(curl -fsSL "${LATEST_URL}" | grep '"tag_name"' | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/')"
112+
RELEASE_JSON="$(curl -fsSL --retry 3 "${LATEST_URL}" 2>&1)" || {
113+
fatal "No stable release found. If you want the nightly build, run:\n curl -sfL https://raw.githubusercontent.com/${GITHUB_REPO}/main/install.sh | CREATEOS_CHANNEL=nightly sh -"
114+
}
115+
VERSION="$(printf '%s' "${RELEASE_JSON}" | grep '"tag_name"' | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/')"
113116
elif command -v wget > /dev/null 2>&1; then
114-
VERSION="$(wget -qO- "${LATEST_URL}" | grep '"tag_name"' | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/')"
117+
RELEASE_JSON="$(wget -qO- "${LATEST_URL}")" || {
118+
fatal "No stable release found. If you want the nightly build, run:\n curl -sfL https://raw.githubusercontent.com/${GITHUB_REPO}/main/install.sh | CREATEOS_CHANNEL=nightly sh -"
119+
}
120+
VERSION="$(printf '%s' "${RELEASE_JSON}" | grep '"tag_name"' | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/')"
115121
else
116122
fatal "curl or wget is required to download createos."
117123
fi
@@ -169,9 +175,11 @@ resolve_install_dir() {
169175
}
170176

171177
# ---------------------------------------------------------------------------
172-
# Download helper (curl or wget)
178+
# Download helpers (curl or wget)
173179
# ---------------------------------------------------------------------------
174-
download() {
180+
181+
# download_silent: no progress output — used for small files (checksums, API calls)
182+
download_silent() {
175183
URL="$1"
176184
DEST="$2"
177185
if command -v curl > /dev/null 2>&1; then
@@ -183,6 +191,19 @@ download() {
183191
fi
184192
}
185193

194+
# download_progress: shows progress bar — used for the binary
195+
download_progress() {
196+
URL="$1"
197+
DEST="$2"
198+
if command -v curl > /dev/null 2>&1; then
199+
curl -fL --retry 3 --retry-delay 2 --progress-bar -o "${DEST}" "${URL}"
200+
elif command -v wget > /dev/null 2>&1; then
201+
wget --show-progress -qO "${DEST}" "${URL}" 2>&1
202+
else
203+
fatal "curl or wget is required."
204+
fi
205+
}
206+
186207
# ---------------------------------------------------------------------------
187208
# Verify SHA256 checksum
188209
# ---------------------------------------------------------------------------
@@ -230,11 +251,10 @@ install_binary() {
230251
trap 'rm -rf "${TMP_DIR}"' EXIT
231252

232253
info "Downloading ${ASSET}..."
233-
download "${BINARY_URL}" "${TMP_BINARY}" \
254+
download_progress "${BINARY_URL}" "${TMP_BINARY}" \
234255
|| fatal "Failed to download binary from ${BINARY_URL}"
235256

236-
info "Downloading checksum..."
237-
download "${CHECKSUM_URL}" "${TMP_CHECKSUM}" \
257+
download_silent "${CHECKSUM_URL}" "${TMP_CHECKSUM}" \
238258
|| fatal "Failed to download checksum from ${CHECKSUM_URL}"
239259

240260
EXPECTED_HASH="$(cat "${TMP_CHECKSUM}" | tr -d '[:space:]')"

0 commit comments

Comments
 (0)