Skip to content

Commit

Permalink
fix: don't throw error for if trying to upgrade a package and the ass…
Browse files Browse the repository at this point in the history
…et has the same name (#31)
  • Loading branch information
marwanhawari authored Apr 2, 2024
1 parent 94c9ec2 commit 0a27356
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 212 deletions.
11 changes: 2 additions & 9 deletions cmd/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,9 @@ func Browse(cliInput string) {

downloadURL := githubProject.Releases[tagIndex].Assets[assetIndex].DownloadURL
downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
err = stew.DownloadFile(downloadPath, downloadURL)
stew.CatchAndExit(err)

if downloadPathExists {
stew.CatchAndExit(stew.AssetAlreadyDownloadedError{Asset: asset})
} else {
err = stew.DownloadFile(downloadPath, downloadURL)
stew.CatchAndExit(err)
fmt.Printf("✅ Downloaded %v to %v\n", constants.GreenColor(asset), constants.GreenColor(stewPkgPath))
}
fmt.Printf("✅ Downloaded %v to %v\n", constants.GreenColor(asset), constants.GreenColor(stewPkgPath))

binaryName, err := stew.InstallBinary(downloadPath, repo, systemInfo, &lockFile, false)
if err != nil {
Expand Down
11 changes: 2 additions & 9 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,9 @@ func Install(cliInputs []string) {
}

downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
err = stew.DownloadFile(downloadPath, downloadURL)
stew.CatchAndExit(err)
if downloadPathExists {
fmt.Println(stew.AssetAlreadyDownloadedError{Asset: asset})
continue
} else {
err = stew.DownloadFile(downloadPath, downloadURL)
stew.CatchAndExit(err)
fmt.Printf("✅ Downloaded %v to %v\n", constants.GreenColor(asset), constants.GreenColor(stewPkgPath))
}
fmt.Printf("✅ Downloaded %v to %v\n", constants.GreenColor(asset), constants.GreenColor(stewPkgPath))

binaryName, err := stew.InstallBinary(downloadPath, repo, systemInfo, &lockFile, false)
if err != nil {
Expand Down
166 changes: 88 additions & 78 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ import (
)

// Upgrade is executed when you run `stew upgrade`
func Upgrade(cliFlag bool, binaryName string) {
func Upgrade(upgradeAllCliFlag bool, binaryName string) {

userOS, userArch, _, systemInfo, err := stew.Initialize()
stew.CatchAndExit(err)

if cliFlag && binaryName != "" {
if upgradeAllCliFlag && binaryName != "" {
stew.CatchAndExit(stew.CLIFlagAndInputError{})
} else if !cliFlag {
} else if !upgradeAllCliFlag {
err := stew.ValidateCLIInput(binaryName)
stew.CatchAndExit(err)
}

sp := constants.LoadingSpinner

stewPkgPath := systemInfo.StewPkgPath
stewTmpPath := systemInfo.StewTmpPath
stewLockFilePath := systemInfo.StewLockFilePath

Expand All @@ -40,82 +37,95 @@ func Upgrade(cliFlag bool, binaryName string) {
stew.CatchAndExit(stew.NoBinariesInstalledError{})
}

var binaryFound bool
for index, pkg := range lockFile.Packages {
if upgradeAllCliFlag {
upgradeAll(userOS, userArch, lockFile, systemInfo)
} else {
err := upgradeOne(binaryName, userOS, userArch, lockFile, systemInfo)
stew.CatchAndExit(err)
}
}

var upgradeCondition bool
if cliFlag || pkg.Binary == binaryName {
upgradeCondition = true
}
func upgradeOne(binaryName, userOS, userArch string, lockFile stew.LockFile, systemInfo stew.SystemInfo) error {
sp := constants.LoadingSpinner
stewPkgPath := systemInfo.StewPkgPath
stewLockFilePath := systemInfo.StewLockFilePath

indexInLockFile, binaryFoundInLockFile := stew.FindBinaryInLockFile(lockFile, binaryName)
if !binaryFoundInLockFile {
return stew.BinaryNotInstalledError{Binary: binaryName}
}

pkg := lockFile.Packages[indexInLockFile]
fmt.Println(constants.GreenColor(pkg.Binary))
if pkg.Source == "other" {
return stew.InstalledFromURLError{Binary: pkg.Binary}
}
owner := pkg.Owner
repo := pkg.Repo

sp.Start()
githubProject, err := stew.NewGithubProject(owner, repo)
sp.Stop()
if err != nil {
return err
}

if upgradeCondition {
fmt.Println(constants.GreenColor(pkg.Binary))
binaryFound = true

if pkg.Source == "other" {
fmt.Println(stew.InstalledFromURLError{Binary: pkg.Binary})
continue
}
owner := pkg.Owner
repo := pkg.Repo

sp.Start()
githubProject, err := stew.NewGithubProject(owner, repo)
sp.Stop()
stew.CatchAndExit(err)

// This will make sure that there are any tags at all
_, err = stew.GetGithubReleasesTags(githubProject)
stew.CatchAndExit(err)

// Get the latest tag
tagIndex := 0
tag := githubProject.Releases[tagIndex].TagName

if pkg.Tag == tag {
fmt.Println(stew.AlreadyInstalledLatestTagError{Tag: tag})
continue
}

// Make sure there are any assets at all
releaseAssets, err := stew.GetGithubReleasesAssets(githubProject, tag)
stew.CatchAndExit(err)

asset, err := stew.DetectAsset(userOS, userArch, releaseAssets)
stew.CatchAndExit(err)
assetIndex, _ := stew.Contains(releaseAssets, asset)

downloadURL := githubProject.Releases[tagIndex].Assets[assetIndex].DownloadURL

downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
stew.CatchAndExit(err)
if downloadPathExists {
stew.CatchAndExit(stew.AssetAlreadyDownloadedError{Asset: asset})
} else {
err = stew.DownloadFile(downloadPath, downloadURL)
stew.CatchAndExit(err)
fmt.Printf("✅ Downloaded %v to %v\n", constants.GreenColor(asset), constants.GreenColor(stewPkgPath))
}

_, err = stew.InstallBinary(downloadPath, repo, systemInfo, &lockFile, true)
if err != nil {
os.RemoveAll(downloadPath)
stew.CatchAndExit(err)
}

lockFile.Packages[index].Tag = tag
lockFile.Packages[index].Asset = asset
lockFile.Packages[index].URL = downloadURL

err = stew.WriteLockFileJSON(lockFile, stewLockFilePath)
stew.CatchAndExit(err)

fmt.Printf("✨ Successfully upgraded the %v binary from %v to %v\n", constants.GreenColor(pkg.Binary), constants.GreenColor(pkg.Tag), constants.GreenColor(tag))
// This will make sure that there are any tags at all
_, err = stew.GetGithubReleasesTags(githubProject)
if err != nil {
return err
}

// Get the latest tag
tagIndex := 0
tag := githubProject.Releases[tagIndex].TagName

if pkg.Tag == tag {
return stew.AlreadyInstalledLatestTagError{Tag: tag}
}

// Make sure there are any assets at all
releaseAssets, err := stew.GetGithubReleasesAssets(githubProject, tag)
if err != nil {
return err
}

asset, err := stew.DetectAsset(userOS, userArch, releaseAssets)
if err != nil {
return err
}
assetIndex, _ := stew.Contains(releaseAssets, asset)
downloadURL := githubProject.Releases[tagIndex].Assets[assetIndex].DownloadURL
downloadPath := filepath.Join(stewPkgPath, asset)
err = stew.DownloadFile(downloadPath, downloadURL)
if err != nil {
return err
}
fmt.Printf("✅ Downloaded %v to %v\n", constants.GreenColor(asset), constants.GreenColor(stewPkgPath))

_, err = stew.InstallBinary(downloadPath, repo, systemInfo, &lockFile, true)
if err != nil {
if err := os.RemoveAll(downloadPath); err != nil {
return err
}
}
if !cliFlag && !binaryFound {
stew.CatchAndExit(stew.BinaryNotInstalledError{Binary: binaryName})

lockFile.Packages[indexInLockFile].Tag = tag
lockFile.Packages[indexInLockFile].Asset = asset
lockFile.Packages[indexInLockFile].URL = downloadURL
if err := stew.WriteLockFileJSON(lockFile, stewLockFilePath); err != nil {
return err
}

fmt.Printf("✨ Successfully upgraded the %v binary from %v to %v\n", constants.GreenColor(pkg.Binary), constants.GreenColor(pkg.Tag), constants.GreenColor(tag))
return nil
}

func upgradeAll(userOS, userArch string, lockFile stew.LockFile, systemInfo stew.SystemInfo) {
for _, pkg := range lockFile.Packages {
if err := upgradeOne(pkg.Binary, userOS, userArch, lockFile, systemInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
}
}
9 changes: 0 additions & 9 deletions lib/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,6 @@ func (e CLIFlagAndInputError) Error() string {
return fmt.Sprintf("%v Cannot use the --all flag with a positional argument", constants.RedColor("Error:"))
}

// AssetAlreadyDownloadedError occurs if the requested asset has already been downloaded
type AssetAlreadyDownloadedError struct {
Asset string
}

func (e AssetAlreadyDownloadedError) Error() string {
return fmt.Sprintf("%v The %v asset has already been downloaded and installed", constants.RedColor("Error:"), constants.RedColor(e.Asset))
}

// AbortBinaryOverwriteError occurs if the overwrite of a binary is aborted
type AbortBinaryOverwriteError struct {
Binary string
Expand Down
29 changes: 0 additions & 29 deletions lib/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,35 +265,6 @@ func TestCLIFlagAndInputError_Error(t *testing.T) {
}
}

func TestAssetAlreadyDownloadedError_Error(t *testing.T) {
type fields struct {
Asset string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "test1",
fields: fields{
Asset: "testAsset",
},
want: fmt.Sprintf("%v The %v asset has already been downloaded and installed", constants.RedColor("Error:"), constants.RedColor("testAsset")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := AssetAlreadyDownloadedError{
Asset: tt.fields.Asset,
}
if got := e.Error(); got != tt.want {
t.Errorf("AssetAlreadyDownloadedError.Error() = %v, want %v", got, tt.want)
}
})
}
}

func TestAbortBinaryOverwriteError_Error(t *testing.T) {
type fields struct {
Binary string
Expand Down
Loading

0 comments on commit 0a27356

Please sign in to comment.