Start work on a command to self update kitty-tool
This commit is contained in:
parent
36dd5b2d00
commit
d54fe3c16a
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"kitty/tools/cli"
|
||||
"kitty/tools/cmd/at"
|
||||
"kitty/tools/cmd/update_self"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
@ -13,5 +14,6 @@ var _ = fmt.Print
|
||||
func KittyToolEntryPoints(root *cli.Command) {
|
||||
// @
|
||||
at.EntryPoint(root)
|
||||
|
||||
// update-self
|
||||
update_self.EntryPoint(root)
|
||||
}
|
||||
|
||||
97
tools/cmd/update_self/main.go
Normal file
97
tools/cmd/update_self/main.go
Normal file
@ -0,0 +1,97 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package update_self
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"kitty/tools/cli"
|
||||
"kitty/tools/tty"
|
||||
"kitty/tools/tui"
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type Options struct {
|
||||
FetchVersion string
|
||||
}
|
||||
|
||||
func fetch_latest_version() (string, error) {
|
||||
b, err := utils.DownloadAsSlice("https://sw.kovidgoyal.net/kitty/current-version.txt", nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to fetch the latest available kitty version: %w", err)
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
func update_self(version string) (err error) {
|
||||
exe := ""
|
||||
exe, err = os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to determine path to kitty-tool: %w", err)
|
||||
}
|
||||
exe, err = filepath.EvalSymlinks(exe)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rv := "v" + version
|
||||
if version == "nightly" {
|
||||
rv = version
|
||||
}
|
||||
if version == "latest" {
|
||||
rv, err = fetch_latest_version()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
dest, err := os.CreateTemp(filepath.Dir(exe), "kitty-tool.")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { os.Remove(dest.Name()) }()
|
||||
|
||||
url := fmt.Sprintf("https://github.com/kovidgoyal/kitty/releases/download/%s/kitty-tool-%s-%s", rv, runtime.GOOS, runtime.GOARCH)
|
||||
if !tty.IsTerminal(os.Stdout.Fd()) {
|
||||
fmt.Println("Downloading:", url)
|
||||
err = utils.DownloadToFile(exe, url, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Downloaded to:", exe)
|
||||
} else {
|
||||
err = tui.DownloadFileWithProgress(exe, url, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func EntryPoint(root *cli.Command) {
|
||||
sc := root.AddSubCommand(&cli.Command{
|
||||
Name: "update-self",
|
||||
Usage: "update-self [options ...]",
|
||||
ShortDescription: "Update this kitty-tool binary",
|
||||
HelpText: "Update this kitty-tool binary in place to the latest available version.",
|
||||
Run: func(cmd *cli.Command, args []string) (ret int, err error) {
|
||||
if len(args) != 0 {
|
||||
return 1, fmt.Errorf("No command line arguments are allowed")
|
||||
}
|
||||
opts := &Options{}
|
||||
err = cmd.GetOptionValues(opts)
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
return 0, update_self(opts.FetchVersion)
|
||||
},
|
||||
})
|
||||
sc.Add(cli.OptionSpec{
|
||||
Name: "--fetch-version",
|
||||
Default: "latest",
|
||||
Help: "The version to fetch. The special words :code:`latest` and :code:`nightly` fetch the latest stable and nightly release respectively. Other values can be, for example: 0.27.1.",
|
||||
})
|
||||
}
|
||||
139
tools/tui/download_with_progress.go
Normal file
139
tools/tui/download_with_progress.go
Normal file
@ -0,0 +1,139 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"kitty/tools/tui/loop"
|
||||
"kitty/tools/utils"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type dl_data struct {
|
||||
mutex sync.Mutex
|
||||
canceled_by_user bool
|
||||
error_from_download error
|
||||
done, total uint64
|
||||
download_started bool
|
||||
download_finished bool
|
||||
temp_file_path string
|
||||
}
|
||||
|
||||
func render_progress(done, total uint64, screen_width uint) string {
|
||||
return fmt.Sprintln(1111111, done, total)
|
||||
}
|
||||
|
||||
func DownloadFileWithProgress(destpath, url string, kill_if_signaled bool) (err error) {
|
||||
lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors, loop.NoMouseTracking)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dl_data := dl_data{}
|
||||
|
||||
register_temp_file_path := func(path string) {
|
||||
dl_data.mutex.Lock()
|
||||
dl_data.temp_file_path = path
|
||||
dl_data.mutex.Unlock()
|
||||
}
|
||||
|
||||
report_progress := func(done, total uint64) error {
|
||||
dl_data.mutex.Lock()
|
||||
defer dl_data.mutex.Unlock()
|
||||
dl_data.done = done
|
||||
dl_data.total = total
|
||||
if dl_data.canceled_by_user {
|
||||
return Canceled
|
||||
}
|
||||
lp.WakeupMainThread()
|
||||
return nil
|
||||
}
|
||||
|
||||
do_download := func() {
|
||||
dl_data.mutex.Lock()
|
||||
dl_data.download_started = true
|
||||
dl_data.mutex.Unlock()
|
||||
err := utils.DownloadToFile(destpath, url, report_progress, register_temp_file_path)
|
||||
dl_data.mutex.Lock()
|
||||
defer dl_data.mutex.Unlock()
|
||||
dl_data.download_finished = true
|
||||
if err != Canceled && err != nil {
|
||||
dl_data.error_from_download = err
|
||||
lp.WakeupMainThread()
|
||||
}
|
||||
}
|
||||
|
||||
redraw := func() {
|
||||
lp.QueueWriteString("\r")
|
||||
lp.ClearToEndOfLine()
|
||||
dl_data.mutex.Lock()
|
||||
defer dl_data.mutex.Unlock()
|
||||
if dl_data.done+dl_data.total == 0 {
|
||||
lp.QueueWriteString("Waiting for download to start...")
|
||||
} else {
|
||||
sz, err := lp.ScreenSize()
|
||||
w := sz.WidthCells
|
||||
if err != nil {
|
||||
w = 80
|
||||
}
|
||||
lp.QueueWriteString(render_progress(dl_data.done, dl_data.total, w))
|
||||
}
|
||||
}
|
||||
|
||||
lp.OnInitialize = func() (string, error) {
|
||||
go do_download()
|
||||
lp.QueueWriteString("Downloading: " + url + "\r\n")
|
||||
return "\r\n", nil
|
||||
}
|
||||
|
||||
lp.OnResumeFromStop = func() error {
|
||||
redraw()
|
||||
return nil
|
||||
}
|
||||
lp.OnResize = func(old_size, new_size loop.ScreenSize) error {
|
||||
redraw()
|
||||
return nil
|
||||
}
|
||||
lp.OnWakeup = func() error {
|
||||
lp.DebugPrintln("11111111111111")
|
||||
dl_data.mutex.Lock()
|
||||
defer dl_data.mutex.Unlock()
|
||||
if dl_data.error_from_download != nil {
|
||||
return dl_data.error_from_download
|
||||
}
|
||||
redraw()
|
||||
return nil
|
||||
}
|
||||
lp.OnKeyEvent = func(event *loop.KeyEvent) error {
|
||||
if event.MatchesPressOrRepeat("ctrl+c") || event.MatchesPressOrRepeat("esc") {
|
||||
event.Handled = true
|
||||
dl_data.mutex.Lock()
|
||||
defer dl_data.mutex.Unlock()
|
||||
dl_data.canceled_by_user = true
|
||||
lp.Quit(1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err = lp.Run()
|
||||
dl_data.mutex.Lock()
|
||||
if dl_data.temp_file_path != "" && !dl_data.download_finished {
|
||||
os.Remove(dl_data.temp_file_path)
|
||||
}
|
||||
dl_data.mutex.Unlock()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ds := lp.DeathSignalName()
|
||||
if ds != "" {
|
||||
if kill_if_signaled {
|
||||
lp.KillIfSignalled()
|
||||
return
|
||||
}
|
||||
return &KilledBySignal{Msg: fmt.Sprint("Killed by signal: ", ds), SignalName: ds}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@ -3,6 +3,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -32,27 +33,15 @@ func (self *write_counter) Write(p []byte) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func DownloadFile(destpath, url string, progress_callback ReportFunc) error {
|
||||
func DownloadToWriter(url string, dest io.Writer, progress_callback ReportFunc) error {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
destpath, err = filepath.EvalSymlinks(destpath)
|
||||
if err != nil {
|
||||
return err
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("The server responded with the HTTP error %d (%s)", resp.StatusCode, resp.Status)
|
||||
}
|
||||
dest, err := os.CreateTemp(filepath.Dir(destpath), filepath.Base(destpath)+".partial-download.")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dest_removed := false
|
||||
defer func() {
|
||||
dest.Close()
|
||||
if !dest_removed {
|
||||
os.Remove(dest.Name())
|
||||
}
|
||||
}()
|
||||
wc := write_counter{report: progress_callback}
|
||||
cl, err := strconv.Atoi(resp.Header.Get("Content-Length"))
|
||||
if err == nil {
|
||||
@ -62,7 +51,53 @@ func DownloadFile(destpath, url string, progress_callback ReportFunc) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DownloadAsSlice(url string, progress_callback ReportFunc) (data []byte, err error) {
|
||||
b := bytes.Buffer{}
|
||||
b.Grow(4096)
|
||||
err = DownloadToWriter(url, &b, progress_callback)
|
||||
if err == nil {
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func DownloadToFile(destpath, url string, progress_callback ReportFunc, temp_file_path_callback func(string)) error {
|
||||
destpath, err := filepath.EvalSymlinks(destpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dest, err := os.CreateTemp(filepath.Dir(destpath), filepath.Base(destpath)+".partial-download.")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if temp_file_path_callback != nil {
|
||||
temp_file_path_callback(dest.Name())
|
||||
}
|
||||
dest_removed := false
|
||||
defer func() {
|
||||
dest.Close()
|
||||
if !dest_removed {
|
||||
os.Remove(dest.Name())
|
||||
}
|
||||
}()
|
||||
err = DownloadToWriter(url, dest, progress_callback)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dest.Close()
|
||||
fi, err := os.Stat(destpath)
|
||||
if err == nil {
|
||||
err = os.Chmod(dest.Name(), fi.Mode().Perm())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Rename(dest.Name(), destpath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user