Skip to content

Commit

Permalink
patch: unzip before doing self-update
Browse files Browse the repository at this point in the history
Unzip before doing self-update
  • Loading branch information
rockavoldy authored Jul 25, 2023
2 parents 91ce192 + 60bf663 commit c1b326e
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cmd

import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"odoo-one-click/config"
Expand Down Expand Up @@ -85,14 +89,41 @@ func CheckNewVersion() (bool, []Asset) {
return false, nil
}

func UncompressZip(src io.Reader, name string) (io.Reader, error) {
buf, err := ioutil.ReadAll(src)
if err != nil {
return nil, fmt.Errorf("failed to create buffer for zip file: %s", err)
}

r := bytes.NewReader(buf)
z, err := zip.NewReader(r, r.Size())
if err != nil {
return nil, fmt.Errorf("failed to uncompress zip file: %s", err)
}

for _, file := range z.File {
if !file.FileInfo().IsDir() {
log.Println("Executable file", file.Name, "was found in zip archive")
return file.Open()
}
}

return nil, fmt.Errorf("file '%s' for the command is not found", name)
}

func DoUpdate(url string) {
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()

err = selfupdate.Apply(resp.Body, selfupdate.Options{})
unzipped, err := UncompressZip(resp.Body, url)
if err != nil {
log.Fatalln(err)
}

err = selfupdate.Apply(unzipped, selfupdate.Options{})
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit c1b326e

Please sign in to comment.