-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from owenthereal/upgrade
Add `goup upgrade [version]`
- Loading branch information
Showing
351 changed files
with
65,918 additions
and
2,056 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/kierdavis/ansi" | ||
"github.com/spf13/cobra" | ||
"github.com/tj/go-update" | ||
"github.com/tj/go-update/progress" | ||
"github.com/tj/go-update/stores/github" | ||
) | ||
|
||
func upgradeCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "upgrade [version]", | ||
Short: "Upgrade goup", | ||
Long: "Upgrade goup by providing a version. If no version is provided, upgrade to the latest goup.", | ||
RunE: runUpgrade, | ||
} | ||
} | ||
|
||
func runUpgrade(cmd *cobra.Command, args []string) error { | ||
ansi.HideCursor() | ||
defer ansi.ShowCursor() | ||
|
||
m := &manager{ | ||
Manager: &update.Manager{ | ||
Command: "goup", | ||
Store: &github.Store{ | ||
Owner: "owenthereal", | ||
Repo: "goup", | ||
Version: Version, | ||
}, | ||
}, | ||
} | ||
|
||
var r release | ||
if len(args) > 0 { | ||
rr, err := m.GetRelease(trimVPrefix(args[0])) | ||
if err != nil { | ||
return fmt.Errorf("error fetching release: %s", err) | ||
} | ||
|
||
r = release{rr} | ||
} else { | ||
// fetch the new releases | ||
releases, err := m.LatestReleases() | ||
if err != nil { | ||
log.Fatalf("error fetching releases: %s", err) | ||
} | ||
|
||
// no updates | ||
if len(releases) == 0 { | ||
logger.Println("No upgrades") | ||
return nil | ||
} | ||
|
||
// latest release | ||
r = release{releases[0]} | ||
|
||
} | ||
|
||
// find the tarball for this system | ||
a := r.FindTarballWithVersion(runtime.GOOS, runtime.GOARCH) | ||
if a == nil { | ||
return fmt.Errorf("no upgrade for your system") | ||
} | ||
|
||
bin, err := a.DownloadProxy(progress.Reader) | ||
if err != nil { | ||
return fmt.Errorf("error downloading: %s", err) | ||
} | ||
|
||
logger.Debugf("Downloaded release to %s", bin) | ||
|
||
// install it | ||
if err := m.InstallBin(bin); err != nil { | ||
return fmt.Errorf("error installing: %s", err) | ||
} | ||
|
||
logger.Printf("Upgraded to %s", trimVPrefix(r.Version)) | ||
|
||
return nil | ||
} | ||
|
||
type manager struct { | ||
*update.Manager | ||
} | ||
|
||
func (m *manager) InstallBin(bin string) error { | ||
oldbin, err := exec.LookPath(m.Command) | ||
if err != nil { | ||
return fmt.Errorf("error looking up path of %q: %w", m.Command, err) | ||
} | ||
|
||
dir := filepath.Dir(oldbin) | ||
|
||
if err := os.Chmod(bin, 0755); err != nil { | ||
return fmt.Errorf("error in chmod: %w", err) | ||
} | ||
|
||
dst := filepath.Join(dir, m.Command) | ||
tmp := dst + ".tmp" | ||
|
||
logger.Debugf("Copy %q to %q", bin, tmp) | ||
if err := copyFile(tmp, bin); err != nil { | ||
return fmt.Errorf("error in copying: %w", err) | ||
} | ||
|
||
if runtime.GOOS == "windows" { | ||
old := dst + ".old" | ||
logger.Debugf("Windows workaround renaming %q to %q", dst, old) | ||
if err := os.Rename(dst, old); err != nil { | ||
return fmt.Errorf("error in windows renmaing: %w", err) | ||
} | ||
} | ||
|
||
logger.Debugf("Renaming %q to %q", tmp, dst) | ||
if err := os.Rename(tmp, dst); err != nil { | ||
return fmt.Errorf("error in renaming: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type release struct { | ||
*update.Release | ||
} | ||
|
||
func (r *release) FindTarballWithVersion(os, arch string) *update.Asset { | ||
s := fmt.Sprintf("%s-%s", os, arch) | ||
for _, a := range r.Assets { | ||
if s == a.Name { | ||
return a | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func trimVPrefix(s string) string { | ||
return strings.TrimPrefix(s, "v") | ||
} | ||
|
||
// copyFile copies the contents of the file named src to the file named | ||
// by dst. The file will be created if it does not already exist. If the | ||
// destination file exists, all it's contents will be replaced by the contents | ||
// of the source file. The file mode will be copied from the source and | ||
// the copied data is synced/flushed to stable storage. | ||
func copyFile(dst, src string) (err error) { | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return | ||
} | ||
defer in.Close() | ||
|
||
out, err := os.Create(dst) | ||
if err != nil { | ||
return | ||
} | ||
|
||
defer func() { | ||
if e := out.Close(); e != nil { | ||
err = e | ||
} | ||
}() | ||
|
||
_, err = io.Copy(out, in) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = out.Sync() | ||
if err != nil { | ||
return | ||
} | ||
|
||
si, err := os.Stat(src) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = os.Chmod(dst, si.Mode()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.