fix(install): validate permissions before upgrade

resolves #5318
This commit is contained in:
Jan De Dobbeleer 2024-07-21 20:22:04 +02:00 committed by Jan De Dobbeleer
parent 55bc366a8a
commit 0b4a9ceb31
3 changed files with 12 additions and 16 deletions

View file

@ -86,7 +86,6 @@ func executeUpgrade(latest string) {
return
}
fmt.Printf("\n❌ %s\n\n", err)
os.Exit(1)
}

View file

@ -58,7 +58,7 @@ func (m *model) Init() tea.Cmd {
go func() {
if err := install(m.tag); err != nil {
m.error = err
program.Quit()
program.Send(resultMsg(fmt.Sprintf("❌ upgrade failed: %v", err)))
return
}
@ -132,11 +132,7 @@ func Run(latest string) error {
title = titleStyle.Render(title)
program = tea.NewProgram(initialModel(latest))
resultModel, err := program.Run()
if err != nil {
return err
}
resultModel, _ := program.Run()
programModel, OK := resultModel.(*model)
if !OK {

View file

@ -2,6 +2,7 @@ package upgrade
import (
"bytes"
"errors"
"fmt"
"io"
"os"
@ -16,6 +17,15 @@ func install(tag string) error {
return err
}
targetDir := filepath.Dir(executable)
fileName := filepath.Base(executable)
newPath := filepath.Join(targetDir, fmt.Sprintf(".%s.new", fileName))
fp, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775)
if err != nil {
return errors.New("we do not have permissions to update")
}
setState(downloading)
data, err := downloadAndVerify(tag)
@ -25,15 +35,6 @@ func install(tag string) error {
setState(installing)
targetDir := filepath.Dir(executable)
fileName := filepath.Base(executable)
newPath := filepath.Join(targetDir, fmt.Sprintf(".%s.new", fileName))
fp, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775)
if err != nil {
return err
}
_, err = io.Copy(fp, bytes.NewReader(data))
// windows will have a lock when we do not close the file
fp.Close()