Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #168 from tomlazar/master
Browse files Browse the repository at this point in the history
return more helpful errors for link and unlink
  • Loading branch information
Matthew Fisher authored Jul 28, 2020
2 parents 3c11947 + f30cc8d commit bb012a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/gofish/install.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -62,7 +63,13 @@ func newInstallCmd() *cobra.Command {
ohai.Ohaif("Installing %s...\n", fishFood)
start := time.Now()
if err := food.Install(); err != nil {
return err
if errors.Is(err, gofish.ErrCouldNotUnlink{}) {
return fmt.Errorf("%s could not be 'unlinked' try running 'gofish unlink %s': %s", fishFood, fishFood, err.Error())
} else if errors.Is(err, gofish.ErrCouldNotLink{}) {
return fmt.Errorf("%s could not be 'linked' try running 'gofish link %s': %s", fishFood, fishFood, err.Error())
} else {
return err
}
}
t := time.Now()
ohai.Successf("%s %s: installed in %s\n", food.Name, food.Version, t.Sub(start).String())
Expand Down
26 changes: 24 additions & 2 deletions food.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ type Resource struct {
Executable bool
}

// ErrCouldNotUnlink is returned when the 'unlink' operation does not succeed
type ErrCouldNotUnlink struct {
Err error
}

func (e ErrCouldNotUnlink) Error() string {
return fmt.Sprintf("could not unlink: %s", e.Err.Error())
}

// ErrCouldNotLink is returned when the 'link' operation does not succeed
type ErrCouldNotLink struct {
Err error
}

func (e ErrCouldNotLink) Error() string {
return fmt.Sprintf("could not link: %s", e.Err.Error())
}

// Install attempts to install the package, returning errors if it fails.
func (f *Food) Install() error {
barrelDir := filepath.Join(home.Barrel(), f.Name, f.Version)
Expand Down Expand Up @@ -104,7 +122,11 @@ func (f *Food) Install() error {
unarchiveOrCopy(cachedFilePath, barrelDir, u.Path)

// This is just a safety check to make sure that there's nothing there when we link the package.
f.Unlink(pkg)
err = f.Unlink(pkg)
if err != nil {
return ErrCouldNotUnlink{err}
}

// special case: gofish is replacing itself on windows
// https://github.com/fishworks/gofish/issues/46
if runtime.GOOS == "windows" && f.Name == "gofish" {
Expand All @@ -120,7 +142,7 @@ func (f *Food) Install() error {
}
}
if err := f.Link(pkg); err != nil {
return err
return ErrCouldNotLink{err}
}

if f.PostInstallScript != "" {
Expand Down

0 comments on commit bb012a4

Please sign in to comment.