Skip to content

Commit

Permalink
Add grifts directory hunting
Browse files Browse the repository at this point in the history
Instead of requiring grifts to run at the top-level directory, it can
now be executed in any subdirectory of the current package and the
grifts directory will be found if it exists.
  • Loading branch information
Ryan Faerman committed Jun 21, 2017
1 parent bf7b900 commit c6826af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 28 additions & 10 deletions cmd/grifter.go
Expand Up @@ -4,9 +4,9 @@ import (
"html/template"
"os"
"path/filepath"
"strings"
"sync"

"github.com/gobuffalo/envy"
"github.com/pkg/errors"
)

Expand All @@ -20,29 +20,47 @@ type grifter struct {
Verbose bool
}

func hasGriftDir(path string) bool {
stat, err := os.Stat(filepath.Join(path, "grifts"))
if err != nil {
if os.IsNotExist(err) {
return false
}
return false
}

if !stat.IsDir() {
return false
}

return true

}

func newGrifter(name string) (*grifter, error) {
g := &grifter{
CommandName: name,
}

pwd, err := os.Getwd()
path, err := os.Getwd()
if err != nil {
return g, errors.WithStack(err)
}

stat, err := os.Stat(filepath.Join(pwd, "grifts"))
if err != nil {
if os.IsNotExist(err) {
return g, errors.Errorf("there is no directory named 'grifts'. Run '%s init' or switch to the appropriate directory", name)
for !strings.HasSuffix(path, "/src") && path != "/" {
if hasGriftDir(path) {
break
}
return g, err

path = filepath.Dir(path)
}

if !stat.IsDir() {
return g, errors.New("there should be a directory named 'grifts', not a file")
p := strings.SplitN(path, "/src/", 2)
if len(p) == 1 {
return g, errors.Errorf("There is no directory named 'grifts'. Run '%s init' or switch to the appropriate directory", name)
}

g.GriftsPackagePath = filepath.ToSlash(filepath.Join(envy.CurrentPackage(), "grifts"))
g.GriftsPackagePath = filepath.ToSlash(filepath.Join(p[1], "grifts"))
return g, nil
}

Expand Down
2 changes: 2 additions & 0 deletions main.go
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"

"github.com/markbates/grift/cmd"
Expand All @@ -9,6 +10,7 @@ import (
func main() {
err := cmd.Run("grift", os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}

0 comments on commit c6826af

Please sign in to comment.