From 188b2498d23823a9df451d07d7908342e4cba49d Mon Sep 17 00:00:00 2001 From: Ali Najafizadeh Date: Wed, 3 Jan 2024 21:17:49 -0500 Subject: [PATCH] Finalize the working draft --- cmd/selfupdate/main.go | 5 ++-- patcher.go | 36 ++++++++++++++------------ pkg/executil/executil.go | 56 ---------------------------------------- runner.go | 17 ------------ 4 files changed, 21 insertions(+), 93 deletions(-) diff --git a/cmd/selfupdate/main.go b/cmd/selfupdate/main.go index 2cafefa..b414cfd 100644 --- a/cmd/selfupdate/main.go +++ b/cmd/selfupdate/main.go @@ -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) diff --git a/patcher.go b/patcher.go index 259771c..247f070 100644 --- a/patcher.go +++ b/patcher.go @@ -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) }) } @@ -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) +} diff --git a/pkg/executil/executil.go b/pkg/executil/executil.go index 18b7a8b..aca230b 100644 --- a/pkg/executil/executil.go +++ b/pkg/executil/executil.go @@ -1,10 +1,8 @@ package executil import ( - "io" "os" "path/filepath" - "strings" ) func CurrentPath() (string, error) { @@ -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. -> 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). 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) -} diff --git a/runner.go b/runner.go index a8a465c..71abb7c 100644 --- a/runner.go +++ b/runner.go @@ -4,8 +4,6 @@ import ( "context" "os" "os/exec" - - "selfupdate.blockthrough.com/pkg/executil" ) // NewCliRunner rerun the an executable with the same arguments. @@ -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) - }) -}