refactor(upgrade): move download inline

This commit is contained in:
Jan De Dobbeleer 2024-07-01 11:46:26 +02:00 committed by Jan De Dobbeleer
parent ddd2d154df
commit 7c7bd14a45
2 changed files with 18 additions and 27 deletions

View file

@ -1,10 +1,7 @@
package upgrade package upgrade
import ( import (
"context"
"fmt" "fmt"
"io"
"net/http"
"os" "os"
"github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/spinner"
@ -12,7 +9,6 @@ import (
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/jandedobbeleer/oh-my-posh/src/build" "github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/platform/net"
) )
var ( var (
@ -98,6 +94,7 @@ func (m *model) View() string {
var message string var message string
m.spinner.Spinner = spinner.Dot m.spinner.Spinner = spinner.Dot
switch m.state { switch m.state {
case validating: case validating:
message = "Validating current installation" message = "Validating current installation"
@ -128,23 +125,3 @@ func Run(env platform.Environment) {
os.Exit(1) 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
}

View file

@ -2,13 +2,16 @@ package upgrade
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/platform/net"
) )
func install() error { func install() error {
@ -28,14 +31,25 @@ func install() error {
setState(downloading) 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 { if err != nil {
return err 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 { if err != nil {
return err return err
} }