From 7c7bd14a4583e3d2facf146fb8a177a0c61067c6 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Mon, 1 Jul 2024 11:46:26 +0200 Subject: [PATCH] refactor(upgrade): move download inline --- src/upgrade/cli.go | 25 +------------------------ src/upgrade/install.go | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/upgrade/cli.go b/src/upgrade/cli.go index f84735e7..93cef002 100644 --- a/src/upgrade/cli.go +++ b/src/upgrade/cli.go @@ -1,10 +1,7 @@ package upgrade import ( - "context" "fmt" - "io" - "net/http" "os" "github.com/charmbracelet/bubbles/spinner" @@ -12,7 +9,6 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/jandedobbeleer/oh-my-posh/src/build" "github.com/jandedobbeleer/oh-my-posh/src/platform" - "github.com/jandedobbeleer/oh-my-posh/src/platform/net" ) var ( @@ -98,6 +94,7 @@ func (m *model) View() string { var message string m.spinner.Spinner = spinner.Dot + switch m.state { case validating: message = "Validating current installation" @@ -128,23 +125,3 @@ func Run(env platform.Environment) { os.Exit(1) } } - -func downloadAsset(asset string) (io.ReadCloser, error) { - url := fmt.Sprintf("https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/%s", asset) - - req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil) - if err != nil { - return nil, err - } - - resp, err := net.HTTPClient.Do(req) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("failed to download installer: %s", url) - } - - return resp.Body, nil -} diff --git a/src/upgrade/install.go b/src/upgrade/install.go index ccfcffae..f13bccf6 100644 --- a/src/upgrade/install.go +++ b/src/upgrade/install.go @@ -2,13 +2,16 @@ package upgrade import ( "bytes" + "context" "fmt" "io" + "net/http" "os" "path/filepath" "runtime" "github.com/jandedobbeleer/oh-my-posh/src/platform" + "github.com/jandedobbeleer/oh-my-posh/src/platform/net" ) func install() error { @@ -28,14 +31,25 @@ func install() error { setState(downloading) - data, err := downloadAsset(asset) + url := fmt.Sprintf("https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/%s", asset) + + req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil) if err != nil { return err } - defer data.Close() + resp, err := net.HTTPClient.Do(req) + if err != nil { + return err + } - newBytes, err := io.ReadAll(data) + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("failed to download installer: %s", url) + } + + defer resp.Body.Close() + + newBytes, err := io.ReadAll(resp.Body) if err != nil { return err }