-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mathieu Leduc-Hamel
committed
May 3, 2018
1 parent
99a1b2e
commit d98f0c2
Showing
4 changed files
with
96 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
up: | ||
- go: 1.10.1 | ||
- homebrew: | ||
- curl | ||
- custom: | ||
met?: dep status 2> /dev/null > /dev/null | ||
meet: dep ensure | ||
|
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,60 @@ | ||
package helpers | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pior/dad/pkg/env" | ||
"github.com/pior/dad/pkg/utils" | ||
) | ||
|
||
type Homebrew struct { | ||
prefix string | ||
} | ||
|
||
// NewHomebrew is returning a new Cellar | ||
func NewHomebrew() *Homebrew { | ||
return NewHomebrewWithPrefix("/usr/local") | ||
} | ||
|
||
func NewHomebrewWithPrefix(prefix string) *Homebrew { | ||
if !utils.PathExists(filepath.Join(prefix, "Cellar")) { | ||
|
||
path := strings.Split(env.NewFromOS().Get("PATH"), ":") | ||
|
||
if len(path) > 0 && utils.PathExists(path[0]) { | ||
prefix = filepath.Dir(path[0]) | ||
} | ||
} | ||
|
||
return &Homebrew{prefix: prefix} | ||
} | ||
|
||
func pathToPackage(filename string) string { | ||
results := strings.Split(filename, "/") | ||
pkg := results[len(results)-1] | ||
return strings.TrimSuffix(pkg, filepath.Ext(pkg)) | ||
} | ||
|
||
func (h *Homebrew) PackageIsInCaskroom(pkg string) bool { | ||
path := "/opt/homebrew-cask/Caskroom" | ||
|
||
if !utils.PathExists("/opt/homebrew-cask/Caskroom") { | ||
path = filepath.Join(h.prefix, "Caskroom") | ||
} | ||
|
||
return utils.PathExists(filepath.Join(path, pkg)) | ||
} | ||
|
||
func (h *Homebrew) PackageIsInCellar(pkg string) bool { | ||
path := filepath.Join(h.prefix, "Cellar") | ||
|
||
path = filepath.Join(path, pkg) | ||
return utils.PathExists(path) | ||
} | ||
|
||
// PackageIsInstalled returns true if `pkg` is installed in cellar or in caskroom | ||
func (h *Homebrew) PackageIsInstalled(pkg string) bool { | ||
pkg = pathToPackage(pkg) | ||
return h.PackageIsInCellar(pkg) || h.PackageIsInCaskroom(pkg) | ||
} |
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,24 @@ | ||
package helpers | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPackageIsInCellar(t *testing.T) { | ||
prefix, err := ioutil.TempDir("/tmp", "dad-brew") | ||
require.NoError(t, err, "ioutil.TempDir() failed") | ||
|
||
cellarPath := filepath.Join(prefix, "Cellar") | ||
|
||
os.MkdirAll(filepath.Join(cellarPath, "curl", "1.2.3"), os.ModePerm) | ||
|
||
h := NewHomebrewWithPrefix(prefix) | ||
|
||
require.Truef(t, h.PackageIsInCellar("curl"), "Curl is missing from Cellar %s", cellarPath) | ||
require.Falsef(t, h.PackageIsInCellar("vim"), "Curl is missing from Cellar %s", cellarPath) | ||
} |
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