Skip to content

Commit

Permalink
Finalize the working draft
Browse files Browse the repository at this point in the history
  • Loading branch information
alinz committed Jan 4, 2024
1 parent f61e022 commit 188b249
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 93 deletions.
5 changes: 2 additions & 3 deletions cmd/selfupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ import (
// selfupdate github release --owner blockthrough --repo up-marble --name btctl.sign --version v1.0.0 --sign < ./bin/btctl
// selfupdate github download --owner blockthrough --repo up-marble --name btctl.sign --version v1.0.0 --verify > ./bin/btctl

var Version string = "v0.0.0"
var Version string = ""

func main() {
selfupdate.Exec(
selfupdate.Auto(
context.Background(), // Context
"blockthrough", // Owner Name
"selfupdate.go", // Repo Name
Version, // Current Version
"selfupdate", // Executable Name
".new", // Executable Extension for downloading new version
)

err := commands.Execute(Version)
Expand Down
36 changes: 19 additions & 17 deletions patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,23 @@ import (
"os"
"os/exec"
"runtime"

"selfupdate.blockthrough.com/pkg/executil"
)

func NewPatcher(ext string) Patcher {
func NewPatcher(outfile string) Patcher {
return PatcherFunc(func(ctx context.Context, patch io.Reader) error {
execPath, err := executil.CurrentPath()
err := writeToFile(outfile, patch)
if err != nil {
return err
}

target := execPath + ext

err = writeToFile(target, patch)
if err != nil {
return err
}

if rc, ok := patch.(io.ReadCloser); ok {
rc.Close()
}

// On Darwin, use the 'chmod' command to make the binary executable
if runtime.GOOS == "darwin" {
cmd := exec.Command("chmod", "+x", target)
cmd := exec.Command("chmod", "+x", outfile)
return cmd.Run()
}

// For other platforms, use the 'os.Chmod' function
return os.Chmod(target, 0755)
return os.Chmod(outfile, 0755)
})
}

Expand All @@ -46,6 +33,21 @@ func writeToFile(filename string, r io.Reader) error {
}
defer out.Close()

if rc, ok := r.(io.ReadCloser); ok {
defer rc.Close()
}

_, err = io.Copy(out, r)

return err
}

func copyFile(dst, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()

return writeToFile(dst, in)
}
56 changes: 0 additions & 56 deletions pkg/executil/executil.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package executil

import (
"io"
"os"
"path/filepath"
"strings"
)

func CurrentPath() (string, error) {
Expand All @@ -16,57 +14,3 @@ func CurrentPath() (string, error) {
// Clean up the path to get the absolute path without symbolic links
return filepath.EvalSymlinks(exePath)
}

// Copy the content of current executable to a new file.
// file.<ext> -> file
func Copy(ext string) (newPath string, err error) {
src, err := CurrentPath()
if err != nil {
return
}

if !strings.HasSuffix(src, ext) {
return "", nil
}

newPath = strings.TrimSuffix(src, ext)

out, err := os.Create(newPath)
if err != nil {
return
}
defer out.Close()

in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()

_, err = io.Copy(out, in)
return
}

// Cleanup tries to remove the file that is not currently running, but has the given extension.
// Need to run this at the begining of the program to make sure the old file is removed
// Note: this function returns nil if the currently running file has the given extension
// Basically it only does the following: remove (current file).<ext> if it exists
func Cleanup(ext string) error {
src, err := CurrentPath()
if err != nil {
return err
}

if strings.HasSuffix(src, ext) {
return nil
}

target := src + ext

_, err = os.Stat(target)
if err != nil {
return nil
}

return os.Remove(target)
}
17 changes: 0 additions & 17 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"os"
"os/exec"

"selfupdate.blockthrough.com/pkg/executil"
)

// NewCliRunner rerun the an executable with the same arguments.
Expand All @@ -24,18 +22,3 @@ func NewCliRunner(path string, args ...string) Runner {
return cmd.Wait()
})
}

func NewAutoCliRunner(ext string) Runner {
return RunnerFunc(func(ctx context.Context) error {
target, err := executil.Copy(ext)
if err != nil {
return err
}

if target == "" {
return nil
}

return NewCliRunner(target, os.Args[1:]...).Run(ctx)
})
}

0 comments on commit 188b249

Please sign in to comment.