diff --git a/.gitignore b/.gitignore index 5cc40ea..49ca1f4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ doc tmp pkg -*.gem *.pid coverage coverage.data @@ -14,8 +13,6 @@ build/* profile .console_history .sass-cache/* -.rake_tasks~ -*.log.lck solr/ .jhw-cache/ jhw.* @@ -26,4 +23,5 @@ generated/ .vendor/ bin/* gin-bin -grifts/ +.idea/ +.vscode diff --git a/.goreleaser.yml.plush b/.goreleaser.yml.plush new file mode 100644 index 0000000..9a953d6 --- /dev/null +++ b/.goreleaser.yml.plush @@ -0,0 +1,30 @@ +builds: +- + goos: + - darwin + - linux + - windows + env: + - CGO_ENABLED=0 + main: main.go + +checksum: + name_template: 'checksums.txt' + +snapshot: + name_template: "{{ .Tag }}-next" + +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' +<%= if (brew) { %> +brews: +- + name: homebrew-tap + github: + owner: markbates + name: homebrew-tap +<% } %> diff --git a/LICENSE b/LICENSE index 4d8cbbb..649efd4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,22 +1,21 @@ -Copyright (c) 2016 Mark Bates +The MIT License (MIT) -MIT License +Copyright (c) 2019 Mark Bates -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile index 0694fbf..d17f984 100644 --- a/Makefile +++ b/Makefile @@ -1,38 +1,66 @@ -TAGS ?= "sqlite" -GO_BIN ?= go +TAGS ?= "" +GO_BIN ?= "go" install: - $(GO_BIN) install -v ./grift + $(GO_BIN) install -tags ${TAGS} -v ./. + make tidy -deps: - $(GO_BIN) get github.com/gobuffalo/release - $(GO_BIN) get -tags ${TAGS} -t ./... +tidy: ifeq ($(GO111MODULE),on) $(GO_BIN) mod tidy +else + echo skipping go mod tidy endif +deps: + $(GO_BIN) get -tags ${TAGS} -t ./... + make tidy + build: $(GO_BIN) build -v . + make tidy test: - $(GO_BIN) test -tags ${TAGS} ./... + $(GO_BIN) test -cover -tags ${TAGS} ./... + make tidy + +ci-deps: + $(GO_BIN) get -tags ${TAGS} -t ./... ci-test: $(GO_BIN) test -tags ${TAGS} -race ./... +cov: + $(GO_BIN) test -coverprofile cover.out -tags ${TAGS} ./... + go tool cover -html cover.out + make tidy + +lint: + go get github.com/golangci/golangci-lint/cmd/golangci-lint + golangci-lint run --enable-all + make tidy + update: - $(GO_BIN) get -u -tags ${TAGS} ifeq ($(GO111MODULE),on) + rm go.* + $(GO_BIN) mod init $(GO_BIN) mod tidy +else + $(GO_BIN) get -u -tags ${TAGS} endif make test make install -ifeq ($(GO111MODULE),on) - $(GO_BIN) mod tidy -endif + make tidy release-test: $(GO_BIN) test -tags ${TAGS} -race ./... + make tidy release: - release -y -f version.go + $(GO_BIN) get github.com/gobuffalo/release + make tidy + release -y -f ./cli/version.go --skip-packr + make tidy + + + diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 0000000..c91e865 --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,66 @@ +variables: + GOPROXY: "https://proxy.golang.org" + GOBIN: "$(GOPATH)/bin" # Go binaries path + GOPATH: "$(system.defaultWorkingDirectory)/gopath" # Go workspace path + modulePath: "$(GOPATH)/src/github.com/$(build.repository.name)" # Path to the module"s code + +jobs: +- job: Windows + pool: + vmImage: "vs2017-win2016" + strategy: + matrix: + go 1.12 (on): + go_version: "1.12.13" + GO111MODULE: "on" + go 1.12 (off): + go_version: "1.12.13" + GO111MODULE: "off" + go 1.13.4 (on): + go_version: "1.13.4" + GO111MODULE: "on" + go 1.13.4 (off): + go_version: "1.13.4" + GO111MODULE: "off" + steps: + - template: azure-tests.yml + +- job: macOS + pool: + vmImage: "macOS-10.13" + strategy: + matrix: + go 1.12 (on): + go_version: "1.12.13" + GO111MODULE: "on" + go 1.12 (off): + go_version: "1.12.13" + GO111MODULE: "off" + go 1.13.4 (on): + go_version: "1.13.4" + GO111MODULE: "on" + go 1.13.4 (off): + go_version: "1.13.4" + GO111MODULE: "off" + steps: + - template: azure-tests.yml + +- job: Linux + pool: + vmImage: "ubuntu-16.04" + strategy: + matrix: + go 1.12 (on): + go_version: "1.12.13" + GO111MODULE: "on" + go 1.12 (off): + go_version: "1.12.13" + GO111MODULE: "off" + go 1.13.4 (on): + go_version: "1.13.4" + GO111MODULE: "on" + go 1.13.4 (off): + go_version: "1.13.4" + GO111MODULE: "off" + steps: + - template: azure-tests.yml diff --git a/azure-tests.yml b/azure-tests.yml new file mode 100644 index 0000000..eea5822 --- /dev/null +++ b/azure-tests.yml @@ -0,0 +1,19 @@ +steps: + - task: GoTool@0 + inputs: + version: $(go_version) + - task: Bash@3 + inputs: + targetType: inline + script: | + mkdir -p "$(GOBIN)" + mkdir -p "$(GOPATH)/pkg" + mkdir -p "$(modulePath)" + shopt -s extglob + mv !(gopath) "$(modulePath)" + displayName: "Setup Go Workspace" + - script: | + go get -t -v ./... + go test -race ./... + workingDirectory: "$(modulePath)" + displayName: "Tests" diff --git a/cli/help.go b/cli/help.go new file mode 100644 index 0000000..9e4cd7f --- /dev/null +++ b/cli/help.go @@ -0,0 +1,12 @@ +package cli + +import ( + "context" + "fmt" +) + +func Help(ctx context.Context, args []string) error { + stderr := Stderr(ctx) + fmt.Fprintln(stderr, "grift [task arguments]") + return nil +} diff --git a/cli/help_test.go b/cli/help_test.go new file mode 100644 index 0000000..032eecf --- /dev/null +++ b/cli/help_test.go @@ -0,0 +1,20 @@ +package cli + +import ( + "bytes" + "context" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_Help(t *testing.T) { + r := require.New(t) + + bb := &bytes.Buffer{} + + ctx := WithStderr(bb, context.Background()) + r.NoError(Help(ctx, []string{})) + + r.Contains(bb.String(), "grift [task arguments]") +} diff --git a/cmd/templates.go b/cli/init.go similarity index 67% rename from cmd/templates.go rename to cli/init.go index 74ccb38..4b1ea3b 100644 --- a/cmd/templates.go +++ b/cli/init.go @@ -1,26 +1,30 @@ -package cmd +package cli -var modTmpl = `module grifter` - -var mainTmpl = ` -package main +import ( + "context" + "io/ioutil" + "log" + "os" + "path/filepath" +) -import _ "{{.GriftsPackagePath}}" -import "os" -import "log" -import "github.com/markbates/grift/grift" -import "path/filepath" +func Init(ctx context.Context, args []string) error { + pwd, err := os.Getwd() + if err != nil { + return err + } -func main() { - grift.CommandName = "{{.CommandName}}" - if err := os.Chdir(filepath.Dir("{{.GriftsAbsolutePath}}")); err != nil { - log.Fatal(err) + dir := filepath.Join(pwd, "grifts") + if err := os.MkdirAll(dir, 0766); err != nil { + return err } - err := grift.Exec(os.Args[1:], false) + + err = ioutil.WriteFile(filepath.Join(dir, "example.go"), []byte(initTmpl), 0644) if err != nil { log.Fatal(err) } -}` + return nil +} var initTmpl = ` package grifts diff --git a/cli/io.go b/cli/io.go new file mode 100644 index 0000000..301fd96 --- /dev/null +++ b/cli/io.go @@ -0,0 +1,46 @@ +package cli + +import ( + "context" + "io" + "os" +) + +const ( + stdin = "stdin" + stdout = "stdout" + stderr = "stderr" +) + +func WithStdin(r io.Reader, ctx context.Context) context.Context { + return context.WithValue(ctx, stdin, r) +} + +func Stdin(ctx context.Context) io.Reader { + if r, ok := ctx.Value(stdin).(io.Reader); ok { + return r + } + return os.Stdin +} + +func Stdout(ctx context.Context) io.Writer { + if w, ok := ctx.Value(stdout).(io.Writer); ok { + return w + } + return os.Stdout +} + +func Stderr(ctx context.Context) io.Writer { + if w, ok := ctx.Value(stderr).(io.Writer); ok { + return w + } + return os.Stderr +} + +func WithStdout(w io.Writer, ctx context.Context) context.Context { + return context.WithValue(ctx, stdout, w) +} + +func WithStderr(w io.Writer, ctx context.Context) context.Context { + return context.WithValue(ctx, stderr, w) +} diff --git a/cmd/jim.go b/cli/jim.go similarity index 96% rename from cmd/jim.go rename to cli/jim.go index 37c7578..3bdd8ec 100644 --- a/cmd/jim.go +++ b/cli/jim.go @@ -1,11 +1,15 @@ -package cmd +package cli -import "fmt" +import ( + "context" + "fmt" +) -func jimTribute() { - fmt.Println(jim) +func Jim(ctx context.Context, args []string) error { + stdout := Stdout(ctx) + fmt.Fprintln(stdout, jim) - fmt.Println(` + fmt.Fprintln(stdout, ` Grift was inspired by the Ruby program, Rake. (http://rake.rubyforge.org) Rake was written by one of the kindest and generous people any tech community @@ -14,6 +18,8 @@ was lucky to have, Jim Weirich. We miss you Jim. https://en.wikipedia.org/wiki/Jim_Weirich`) + + return nil } var jim = ` diff --git a/cli/main.go b/cli/main.go new file mode 100644 index 0000000..728e339 --- /dev/null +++ b/cli/main.go @@ -0,0 +1,47 @@ +package cli + +import ( + "context" + "flag" + "fmt" +) + +func Main(ctx context.Context, args []string) error { + if len(args) == 0 { + return Help(ctx, args) + } + + opts := struct { + version bool + help bool + }{} + + flags := flag.NewFlagSet("", flag.ContinueOnError) + flags.BoolVar(&opts.version, "v", false, "display version") + flags.BoolVar(&opts.help, "h", false, "display help") + + if err := flags.Parse(args); err != nil { + return err + } + + args = flags.Args() + + if opts.help { + return Help(ctx, args) + } + + if opts.version { + stdout := Stdout(ctx) + fmt.Fprintln(stdout, Version) + return nil + } + + switch args[0] { + case "init": + return Init(ctx, args) + case "jim": + return Jim(ctx, args) + } + + return Run(ctx, args) +} diff --git a/cli/main_test.go b/cli/main_test.go new file mode 100644 index 0000000..681457a --- /dev/null +++ b/cli/main_test.go @@ -0,0 +1,59 @@ +package cli + +import ( + "bytes" + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_Main_Help(t *testing.T) { + table := [][]string{ + {}, + {"-h"}, + } + + for _, tt := range table { + t.Run(strings.Join(tt, " "), func(st *testing.T) { + r := require.New(st) + bb := &bytes.Buffer{} + ctx := WithStderr(bb, context.Background()) + r.NoError(Main(ctx, tt)) + + r.Contains(bb.String(), "grift [task arguments]") + }) + } +} + +func Test_Main_Version(t *testing.T) { + r := require.New(t) + + bb := &bytes.Buffer{} + ctx := WithStdout(bb, context.Background()) + r.NoError(Main(ctx, []string{"-v"})) + + r.Equal(Version, strings.TrimSpace(bb.String())) +} + +func Test_Main_Task(t *testing.T) { + r := require.New(t) + + pwd, err := os.Getwd() + r.NoError(err) + defer os.Chdir(pwd) + os.Chdir(filepath.Dir(pwd)) + + bb := &bytes.Buffer{} + ctx := WithStdout(bb, context.Background()) + + args := []string{"db:seed", "1", "2", "3"} + r.NoError(Main(ctx, args)) + + act := strings.TrimSpace(bb.String()) + + r.Equal("Seeding DB [1 2 3]", act) +} diff --git a/cli/run.go b/cli/run.go new file mode 100644 index 0000000..8d0a232 --- /dev/null +++ b/cli/run.go @@ -0,0 +1,74 @@ +package cli + +import ( + "context" + "html/template" + "os" + "os/exec" + "path" + "path/filepath" + + "github.com/gobuffalo/here" +) + +func Run(ctx context.Context, args []string) error { + h := here.New() + + info, err := h.Current() + if err != nil { + return err + } + + k := struct { + Pkg string + Command string + Dir string + }{ + Pkg: path.Join(info.ImportPath, "grifts"), + Command: "grift", + Dir: info.Dir, + } + + if s, ok := ctx.Value("command").(string); ok { + k.Command = s + } + + od := filepath.Join(info.Dir, ".grifter") + out := filepath.Join(od, "main.go") + + os.MkdirAll(od, 0755) + defer os.RemoveAll(od) + defer func() { + if err := recover(); err != nil { + os.RemoveAll(od) + } + }() + + f, err := os.Create(out) + if err != nil { + return err + } + defer f.Close() + + mpl, err := template.New("main.go").Parse(tmpl) + if err != nil { + return err + } + + if err := mpl.Execute(f, k); err != nil { + return err + } + + cargs := []string{"run", "-tags", "sqlite", out} + cargs = append(cargs, args...) + + c := exec.CommandContext(ctx, "go", cargs...) + c.Stdin = Stdin(ctx) + c.Stdout = Stdout(ctx) + c.Stderr = Stderr(ctx) + + if err := c.Run(); err != nil { + return err + } + return nil +} diff --git a/cli/run_test.go b/cli/run_test.go new file mode 100644 index 0000000..a5ec07e --- /dev/null +++ b/cli/run_test.go @@ -0,0 +1,35 @@ +package cli + +import ( + "bytes" + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_Run(t *testing.T) { + r := require.New(t) + + bb := &bytes.Buffer{} + + ctx := context.Background() + ctx = WithStdout(bb, ctx) + + args := []string{"db:seed", "1", "2", "3"} + + pwd, err := os.Getwd() + r.NoError(err) + defer os.Chdir(pwd) + os.Chdir(filepath.Dir(pwd)) + + err = Run(ctx, args) + r.NoError(err) + + act := strings.TrimSpace(bb.String()) + + r.Equal("Seeding DB [1 2 3]", act) +} diff --git a/cli/tmpl.go b/cli/tmpl.go new file mode 100644 index 0000000..2914011 --- /dev/null +++ b/cli/tmpl.go @@ -0,0 +1,21 @@ +package cli + +const tmpl = ` +package main + +import ( + "log" + "os" + + "github.com/markbates/grift/grift" + _ "{{.Pkg}}" +) + +func main() { + grift.CommandName = "{{.Command}}" + err := grift.Exec(os.Args[1:], false) + if err != nil { + log.Fatal(err) + } +} +` diff --git a/cli/version.go b/cli/version.go new file mode 100644 index 0000000..9d10118 --- /dev/null +++ b/cli/version.go @@ -0,0 +1,3 @@ +package cli + +const Version = "v1.2.0" diff --git a/cmd/gen.go b/cmd/gen.go deleted file mode 100644 index 66ccbb2..0000000 --- a/cmd/gen.go +++ /dev/null @@ -1,19 +0,0 @@ -package cmd - -import ( - "io/ioutil" - "log" - "os" - "path" -) - -func generateInit() { - err := os.MkdirAll("./grifts", 0766) - if err != nil { - log.Fatal(err) - } - err = ioutil.WriteFile(path.Join("./grifts", "example.go"), []byte(initTmpl), 0644) - if err != nil { - log.Fatal(err) - } -} diff --git a/cmd/grifter.go b/cmd/grifter.go deleted file mode 100644 index afe2ef7..0000000 --- a/cmd/grifter.go +++ /dev/null @@ -1,133 +0,0 @@ -package cmd - -import ( - "fmt" - "html/template" - "io/ioutil" - "os" - "path" - "path/filepath" - "strings" - "sync" - - "github.com/gobuffalo/envy" - "github.com/rogpeppe/go-internal/modfile" -) - -const exePath = ".grifter/main.go" - -var once = &sync.Once{} - -type grifter struct { - GriftsPackagePath string - CommandName string - Verbose bool - GriftsAbsolutePath string -} - -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, - } - - currentPath, err := os.Getwd() - if err != nil { - return g, err - } - - if envy.InGoPath() { - // pwd is inside a GOPATH - for !strings.HasSuffix(currentPath, "/src") && currentPath != "/" { - if hasGriftDir(currentPath) { - break - } - - currentPath = filepath.Dir(currentPath) - } - p := strings.SplitN(currentPath, filepath.FromSlash("/src/"), 2) - if len(p) == 1 { - return g, fmt.Errorf("There is no directory named 'grifts'. Run '%s init' or switch to the appropriate directory", name) - } - g.GriftsAbsolutePath = filepath.ToSlash(filepath.Join(currentPath, "grifts")) - g.GriftsPackagePath = filepath.ToSlash(filepath.Join(p[1], "grifts")) - } else { - // is outside of gopath, dont loop to parent - if !hasGriftDir(currentPath) { - return g, fmt.Errorf("There is no directory named 'grifts'. Run '%s init' or switch to the appropriate directory", name) - } - g.GriftsAbsolutePath = filepath.ToSlash(filepath.Join(currentPath, "grifts")) - - // check for go module to see if we can get go.mod - if envy.Mods() { - moddata, err := ioutil.ReadFile("go.mod") - if err != nil { - return g, fmt.Errorf("go.mod cannot be read or does not exist while go module is enabled.") - } - packagePath := modfile.ModulePath(moddata) - if packagePath == "" { - return g, fmt.Errorf("go.mod is malformed.") - } - g.GriftsPackagePath = fmt.Sprintf("%s/grifts", packagePath) - } else { - // no go module, infer package path from current directory - g.GriftsPackagePath = filepath.ToSlash(filepath.Join(path.Base(currentPath), "grifts")) - } - - } - - return g, nil -} - -func (g *grifter) Setup() error { - - root := filepath.Dir(exePath) - err := os.MkdirAll(root, 0755) - if err != nil { - return err - } - - tmpls := map[string]string{} - tmpls[exePath] = mainTmpl - if envy.Mods() { - tmpls[filepath.Join(root, "go.mod")] = modTmpl - } - for k, v := range tmpls { - t, err := template.New(k).Parse(v) - if err != nil { - return err - } - - f, err := os.Create(k) - if err != nil { - return err - } - - err = t.Execute(f, g) - if err != nil { - return err - } - } - - return nil -} - -func (g *grifter) TearDown() error { - return os.RemoveAll(filepath.Dir(exePath)) -} diff --git a/cmd/run.go b/cmd/run.go index ed6788c..1a6252c 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -1,84 +1,13 @@ package cmd import ( - "bytes" - "fmt" - "io/ioutil" - "os/exec" + "context" - "github.com/markbates/grift/grift" + "github.com/markbates/grift/cli" ) -var currentGrift *grifter - func Run(name string, args []string) error { - defer func() { - currentGrift.TearDown() - }() - if len(args) == 1 { - switch args[0] { - case "jim": - jimTribute() - return nil - case "init": - generateInit() - return nil - case "--version", "-v": - fmt.Println(Version) - return nil - case "--help", "-h": - fmt.Println("grift [task arguments]") - return nil - } - } - - err := setup(name) - if err != nil { - return err - } - - err = run(args) - if err != nil { - return err - } - - return nil -} - -func run(args []string) error { - rargs := []string{"run"} - // Test for special cases requiring sqlite build tag - b, err := ioutil.ReadFile("database.yml") - if err != nil { - b, err = ioutil.ReadFile("config/database.yml") - } - if err == nil { - if bytes.Contains(b, []byte("sqlite")) { - rargs = append(rargs, "-tags", "sqlite") - } - } - rargs = append(rargs, exePath) - rargs = append(rargs, args...) - if err := grift.RunSource(exec.Command("go", rargs...)); err != nil { - return err - } - return nil -} - -func list() error { - rargs := []string{"run", exePath, "list"} - return grift.RunSource(exec.Command("go", rargs...)) -} - -func setup(name string) error { - var err error - currentGrift, err = newGrifter(name) - if err != nil { - return err - } - err = currentGrift.Setup() - if err != nil { - return err - } - return nil + ctx := context.Background() + ctx = context.WithValue(ctx, "command", name) + return cli.Run(ctx, args) } diff --git a/cmd/version.go b/cmd/version.go deleted file mode 100644 index 5455631..0000000 --- a/cmd/version.go +++ /dev/null @@ -1,3 +0,0 @@ -package cmd - -var Version = "v1.0.4" diff --git a/go.mod b/go.mod index 6fa80e3..2659113 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,8 @@ module github.com/markbates/grift -go 1.12 +go 1.13 require ( - github.com/gobuffalo/envy v1.7.0 - github.com/rogpeppe/go-internal v1.3.0 - github.com/stretchr/testify v1.3.0 + github.com/gobuffalo/here v0.4.0 + github.com/stretchr/testify v1.4.0 ) diff --git a/go.sum b/go.sum index fae370f..1e036e2 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,21 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gobuffalo/envy v1.7.0 h1:GlXgaiBkmrYMHco6t4j7SacKO4XUjvh5pwXh0f4uxXU= -github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= -github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= -github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/gobuffalo/here v0.4.0 h1:TD+FD+jQkp5iNPYEAxT6E+4O8u/pOQJyDjmZNC0+/80= +github.com/gobuffalo/here v0.4.0/go.mod h1:bTNk/uKZgycuB358iR0D32dI9kHBClBGpXjW2HVHkNo= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk= -github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/grifts/example.go b/grifts/example.go new file mode 100644 index 0000000..dafaca7 --- /dev/null +++ b/grifts/example.go @@ -0,0 +1,20 @@ +package grifts + +import ( + "fmt" + + . "github.com/markbates/grift/grift" +) + +var _ = Desc("hello", "Say Hello!") +var _ = Add("hello", func(c *Context) error { + fmt.Println("Hello World!", c.Args) + return nil +}) + +var _ = Namespace("db", func() { + Add("seed", func(c *Context) error { + fmt.Println("Seeding DB", c.Args) + return nil + }) +}) diff --git a/main.go b/main.go index 6260d3e..fe80923 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,51 @@ package main import ( - "fmt" + "context" + "log" "os" + "os/exec" + "os/signal" + "path/filepath" - "github.com/markbates/grift/cmd" + "github.com/markbates/grift/cli" ) func main() { - err := cmd.Run("grift", os.Args[1:]) - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(-1) + defer func() { + c := exec.Command("go", "mod", "tidy") + c.Run() + }() + pwd, _ := os.Getwd() + dir := filepath.Join(pwd, ".grifter") + defer os.RemoveAll(dir) + defer func() { + if err := recover(); err != nil { + os.RemoveAll(dir) + log.Fatal(err) + } + }() + + ctx := context.Background() + + // trap Ctrl+C and call cancel on the context + ctx, cancel := context.WithCancel(ctx) + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + defer func() { + signal.Stop(c) + cancel() + }() + + go func() { + select { + case <-c: + cancel() + case <-ctx.Done(): + } + }() + + if err := cli.Main(ctx, os.Args[1:]); err != nil { + log.Fatal(err) } } diff --git a/version.go b/version.go deleted file mode 100644 index 170365e..0000000 --- a/version.go +++ /dev/null @@ -1,3 +0,0 @@ -package main - -const Version = "v1.1.0"