-
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
62dddb1
commit 7e105c5
Showing
6 changed files
with
174 additions
and
0 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tasks | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/pior/dad/pkg/helpers" | ||
) | ||
|
||
func init() { | ||
allTasks["homebrew"] = newHomebrew | ||
} | ||
|
||
type Homebrew struct { | ||
files []string | ||
} | ||
|
||
func newHomebrew(config *taskConfig) (Task, error) { | ||
task := &Homebrew{} | ||
|
||
for _, value := range config.payload.([]interface{}) { | ||
if v, ok := value.(string); ok { | ||
task.files = append(task.files, v) | ||
} else { | ||
return nil, fmt.Errorf("invalid homebrew packages") | ||
} | ||
} | ||
|
||
if len(task.files) == 0 { | ||
return nil, fmt.Errorf("no homebrew packages specified") | ||
} | ||
|
||
return task, nil | ||
} | ||
|
||
func (h *Homebrew) name() string { | ||
return "Homebrew" | ||
} | ||
|
||
func (h *Homebrew) header() string { | ||
return strings.Join(h.files, ", ") | ||
} | ||
|
||
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) | ||
} | ||
} | ||
return nil | ||
} |
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,16 @@ | ||
package tasks | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHomebrew(t *testing.T) { | ||
task := ensureLoadTestTask(t, ` | ||
homebrew: | ||
- file1 | ||
- file2 | ||
`) | ||
require.Equal(t, task.(*Homebrew).files, []string{"file1", "file2"}) | ||
} |