Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Leduc-Hamel committed May 3, 2018
1 parent 99a1b2e commit d98f0c2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dev.yml
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
Expand Down
60 changes: 60 additions & 0 deletions pkg/helpers/homebrew.go
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)
}
24 changes: 24 additions & 0 deletions pkg/helpers/homebrew_test.go
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)
}
11 changes: 10 additions & 1 deletion pkg/tasks/homebrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tasks
import (
"fmt"
"strings"

"github.com/pior/dad/pkg/helpers"
)

func init() {
Expand Down Expand Up @@ -39,16 +41,23 @@ func (h *Homebrew) header() string {
return strings.Join(h.files, ", ")
}

func (h *Homebrew) perform(ctx *Context) (err error) {
func (h *Homebrew) perform(ctx *Context) error {
packageHelper := helpers.NewHomebrew()

if ctx.env.Os() != "darwin" {
return fmt.Errorf("homebrew is only supported on darwin operating system")
}

for _, file := range h.files {
if packageHelper.PackageIsInstalled(file) {
continue
}

code, err := runCommand(ctx, "brew", "install", file)
if err != nil {
return err
}

if code != 0 {
return fmt.Errorf("Homebrew failed with code %d", code)
}
Expand Down

0 comments on commit d98f0c2

Please sign in to comment.