Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to support downloading and updating restic for Windows. #432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
test
autorestic
data
dist
dist
*.exe
28 changes: 15 additions & 13 deletions internal/bins/bins.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bins

import (
"compress/bzip2"
"encoding/json"
"errors"
"fmt"
Expand All @@ -18,8 +17,6 @@ import (
"github.com/cupcakearmy/autorestic/internal/flags"
)

const INSTALL_PATH = "/usr/local/bin"

type GithubReleaseAsset struct {
Name string `json:"name"`
Link string `json:"browser_download_url"`
Expand All @@ -46,19 +43,19 @@ func dlJSON(url string) (GithubRelease, error) {
}

func Uninstall(restic bool) error {
if err := os.Remove(path.Join(INSTALL_PATH, "autorestic")); err != nil {
if err := os.Remove(path.Join(installPath(), exeName("autorestic"))); err != nil {
return err
}
if restic {
if err := os.Remove(path.Join(INSTALL_PATH, "restic")); err != nil {
if err := os.Remove(path.Join(installPath(), exeName("restic"))); err != nil {
return err
}
}
return nil
}

func downloadAndInstallAsset(body GithubRelease, name string) error {
ending := fmt.Sprintf("_%s_%s.bz2", runtime.GOOS, runtime.GOARCH)
ending := fmt.Sprintf("_%s_%s.%s", runtime.GOOS, runtime.GOARCH, formatName)
for _, asset := range body.Assets {
if strings.HasSuffix(asset.Name, ending) {
// Download archive
Expand All @@ -70,12 +67,15 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
defer resp.Body.Close()

// Uncompress
bz := bzip2.NewReader(resp.Body)
bz, err := decompress(resp)
if err != nil {
return err
}

// Save to tmp file in the same directory as the install directory
// Linux does not support overwriting the file that is currently being running
// But it can be delete the old one and a new one moved in its place.
tmp, err := os.CreateTemp(INSTALL_PATH, "autorestic-")
// Linux does not support overwriting the file that is currently running
// But it can delete the old one and a new one can be moved in its place.
tmp, err := os.CreateTemp(installPath(), "autorestic-")
if err != nil {
return err
}
Expand All @@ -86,8 +86,10 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
if _, err := io.Copy(tmp, bz); err != nil {
return err
}
// On Windows the rename below will fail if the file remains open.
tmp.Close()

to := path.Join(INSTALL_PATH, name)
to := path.Join(installPath(), exeName(name))
defer os.Remove(tmp.Name()) // Cleanup temporary file after thread exits

mode := os.FileMode(0755)
Expand All @@ -109,7 +111,7 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
return err
}

colors.Success.Printf("Successfully installed '%s' under %s\n", name, INSTALL_PATH)
colors.Success.Printf("Successfully installed '%s' under %s\n", name, installPath())
return nil
}
}
Expand All @@ -132,7 +134,7 @@ func InstallRestic() error {

func upgradeRestic() error {
_, _, err := internal.ExecuteCommand(internal.ExecuteOptions{
Command: flags.RESTIC_BIN,
Command: exeName(flags.RESTIC_BIN),
}, "self-update")
return err
}
Expand Down
17 changes: 17 additions & 0 deletions internal/bins/decompress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !windows

package bins

const formatName string = "bz2"

func installPath() string {
return "/usr/local/bin"
}

func decompress(resp *http.Response) (io.ReadCloser, error) {
return bzip2.NewReader(resp.Body)
}

func exeName(f string) string {
return f
}
41 changes: 41 additions & 0 deletions internal/bins/decompress_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build windows

package bins

import (
"archive/zip"
"bytes"
"fmt"
"io"
"net/http"
"os"
)

const formatName string = "zip"

func installPath() string {
return fmt.Sprintf("%s%cSystem32", os.Getenv("SYSTEMROOT"), os.PathSeparator)
}

func decompress(resp *http.Response) (io.ReadCloser, error) {
// Have to copy the response as we need to get an io.ReaderAt for the zip API.
buff := bytes.NewBuffer([]byte{})
size, err := io.Copy(buff, resp.Body)
if err != nil {
return nil, err
}
z, err := zip.NewReader(bytes.NewReader(buff.Bytes()), size)
if err != nil {
return nil, err
}

if len(z.File) != 1 {
return nil, fmt.Errorf("Expecting one file in zip download, got:%d", len(z.File))
}

return z.File[0].Open()
}

func exeName(f string) string {
return fmt.Sprintf("%s/%s.exe", installPath, f)
}