Git Product home page Git Product logo

grift's Introduction

Grift

Grift is a very simple library that allows you to write simple "task" scripts in Go and run them by name without having to write big main type of wrappers. Grift is similar to, and inspired by, Rake.

Why?

Excellent question! When building applications there comes a point where you need different scripts to do different things. For example, you might want a script to seed your database, or perhaps a script to parse some logs, etc...

Grift lets you write these scripts using Go in a really simple and extensible way.

Installation

Installation is really easy using go get.

$ go get github.com/markbates/grift

You can confirm the installation by running:

$ grift jim

Usage/Getting Started

Apart from having the binary installed, the only other requirement is that the package you place your grifts in is called grifts. That's it.

By running the following command:

$ grift init

When you run the init sub-command Grift will generate a new grifts package and create a couple of simple grifts for you.

List available grifts

$ grift list

Say Hello!

$ grift hello

That's it!

That's really it! Grift is meant to be simple. Write your grifts, use the full power of Go to do it.

For more information I would highly recommend checking out the docs.

Examples:

package grifts

import (
	"errors"
	"fmt"
	"os"
	"strings"

	. "github.com/markbates/grift/grift"
)

var _ = Add("boom", func(c *Context) error {
	return errors.New("boom!!!")
})

var _ = Add("hello", func(c *Context) error {
	fmt.Println("Hello World!")
	return nil
})

var _ = Add("hello", func(c *Context) error {
	fmt.Println("Hello World! Again")
	err := Run("db:migrate", c)
	if err != nil {
		return err
	}
	dir, err := os.Getwd()
	if err != nil {
		return err
	}
	fmt.Printf("### dir -> %+v\n", dir)
	return nil
})

var _ = Add("env:print", func(c *Context) error {
	if len(c.Args) >= 1 {
		for _, e := range c.Args {
			fmt.Printf("%s=%s\n", e, os.Getenv(e))
		}
	} else {
		for _, e := range os.Environ() {
			pair := strings.Split(e, "=")
			fmt.Printf("%s=%s\n", pair[0], os.Getenv(pair[0]))
		}
	}

	return nil
})

var _ = Namespace("db", func() {
    Desc("migrate", "Migrates the databases")
    Set("migrate", func(c *Context) error {
            fmt.Println("db:migrate")
            fmt.Printf("### args -> %+v\n", c.Args)
            return nil
    })
}

grift's People

Contributors

alexcarol avatar g4z avatar jcbwlkr avatar markbates avatar mdouchement avatar ntakouris avatar rrrkren avatar ryanfaerman avatar spankie avatar turnkey-commerce avatar u007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grift's Issues

grift fails to find "grifts"-directory

Error

Running

# version 0..4.0
grift

in directory, results in error message:

There is no directory named 'grifts'. Run 'grift init' or switch to the appropriate directory
$ pwd
# => ~/tmp/blub.2.2

Expected result

The grift is found and run also in projects which are not in GOPATH.

More information

I use grift in non-go-projects. With version 0.4.0 this is not possible anymore.

calling grift hello causes sql: Register called twice for driver postgres

hi

i tried run in coke new project, and it works.
but for my project, none task works

$ grift hello
panic: sql: Register called twice for driver postgres

goroutine 1 [running]:
database/sql.Register(0x4cae8a4, 0x8, 0x5360080, 0x53e6700)
	/usr/local/Cellar/go/1.9.3/libexec/src/database/sql/sql.go:50 +0x1ad
project/vendor/github.com/lib/pq.init.0()
	/Users/james/www/gowork/src/project/vendor/github.com/lib/pq/conn.go:49 +0x60
project/vendor/github.com/lib/pq.init()
	<autogenerated>:1 +0x782
project/vendor/github.com/markbates/pop.init()
	<autogenerated>:1 +0x8c
project/db.init()
	<autogenerated>:1 +0x49
project/grifts.init()
	<autogenerated>:1 +0x82
main.init()
	<autogenerated>:1 +0x44

Using grift on windows can produce invalid import statements

I have built a project using IntelliJ as my base on a Windows 10 machine, using the following version of Go
go version go1.8 windows/amd64

I have been trying to use the following command to run some grifts with buffalo, but it has not been successful.

C:\Users\Joshua\Projects\PufferPanel\PufferPanel\src\github.com\pufferpanel\pufferpanel>buffalo task db:seed
Buffalo version 0.8.2

package main:
grift_runner.go:4:22: unknown escape sequence
grift_runner.go:4:34: unknown escape sequence
grift_runner.go:4:46: unknown escape sequence
Usage:
  buffalo task [flags]

Aliases:
  task, t, tasks

Flags:
  -h, --help   help for task

Error: exit status 1

After digging into the file, I noticed that it was creating import paths using my local machine's path separator, which appeared to cause the issue. When I change the path to use the more known Golang version, I am able to use go run grift_runner.go without issue.

I narrowed down the actual offending call to be this one: https://github.com/markbates/grift/blob/master/cmd/grifter.go#L45

It uses the filepath joining method to generate the path, which works on Linux, but does not work well on Windows, or at least, on my own environment.

As per the spec, implementations can \ as an invalid character, and using the Golang.org version of 1.8 for Windows does not permit \ in the import paths.
grift_runner.go:4:22: unknown escape sequence
https://golang.org/ref/spec#Import_declarations

The lexical rules say it's valid, but the windows binary of golang does not.

This being the offending line ->

import _ "github.com\pufferpanel\pufferpanel\grifts"

Since grift wants to always override the file, I cannot use any of the provided methods to actually run it, as it will override any changes to the file, and even making it readonly will not let me run the tasks.

I cannot tell if this is just a "me" issue, but it's effectively been killing me trying to work with it, and seems odd that I'd be the first to really notice it.

Run should say something if task doesn't exist

When using grift.Run inside another grift with a task that doesn't exist Grift doesn't say anything and doesn't do anything, it should at least say that the invoked grift doesn't exist.

Running Grift outside the GOPATH fails

When running grift with a modules app outside the GOPATH it fails saying:

build github.com/paganotoni/myapp/.grifter: cannot find module for path myapp/grifts

Result of buffalo info:

buffalo info
### Buffalo Version
v0.13.2

### App Information
Pwd=/Users/paganotoni/code/myapp
Root=/Users/paganotoni/code/myapp
GoPath=/Users/paganotoni/go
Name=myapp
Bin=bin/myapp
PackagePkg=../../code/myapp
ActionsPkg=../../code/myapp/actions
ModelsPkg=../../code/myapp/models
GriftsPkg=../../code/myapp/grifts
VCS=git
WithPop=true
WithSQLite=false
WithDep=false
WithWebpack=true
WithYarn=false
WithDocker=true
WithGrifts=true
WithModules=false

### Go Version
go version go1.11.1 darwin/amd64

### Go Env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/paganotoni/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/paganotoni/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/paganotoni/code/myapp/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zx/9gj414m52nj3l_zrlr6903sm0000gn/T/go-build325846444=/tmp/go-build -gno-record-gcc-switches -fno-common"

### Node Version
v11.0.0

### NPM Version
6.4.1

### Yarn Version
1.12.1

### PostgreSQL Version
pg_ctl (PostgreSQL) 9.6.10

### MySQL Version
MySQL Not Found

### SQLite Version
3.24.0 2018-06-04 14:10:15 95fbac39baaab1c3a84fdfc82ccb7f42398b2e92f18a2a57bce1d4a713cbaapl

### Dep Version
could not find a Gopkg.toml file

### Dep Status
could not find a Gopkg.toml file

### go.mod
module github.com/paganotoni/myapp

require (
        github.com/Masterminds/sprig v2.16.0+incompatible // indirect
        github.com/aokoli/goutils v1.0.1 // indirect
        github.com/badoux/checkmail v0.0.0-20180430153108-0755fe2dc241
        github.com/bsphere/le_go v0.0.0-20170215134836-7a984a84b549
        github.com/corpix/uarand v0.0.0-20170903190822-2b8494104d86 // indirect
        github.com/dustin/go-humanize v1.0.0
        github.com/fatih/structs v1.1.0
        github.com/go-playground/form v3.1.3+incompatible
        github.com/gobuffalo/buffalo v0.13.2
        github.com/gobuffalo/buffalo-pop v1.1.8
        github.com/gobuffalo/envy v1.6.7
        github.com/gobuffalo/mw-csrf v0.0.0-20180802151833-446ff26e108b
        github.com/gobuffalo/mw-i18n v0.0.0-20181027200759-09e0c99be4d3
        github.com/gobuffalo/mw-paramlogger v0.0.0-20181005191442-d6ee392ec72e
        github.com/gobuffalo/packr v1.16.0
        github.com/gobuffalo/plush v3.7.21+incompatible
        github.com/gobuffalo/pop v4.8.7+incompatible
        github.com/gobuffalo/suite v2.2.0+incompatible
        github.com/gobuffalo/tags v2.0.11+incompatible
        github.com/gobuffalo/uuid v2.0.5+incompatible
        github.com/gobuffalo/validate v2.0.3+incompatible
        github.com/google/uuid v1.0.0 // indirect
        github.com/huandu/xstrings v1.2.0 // indirect
        github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428
        github.com/imdario/mergo v0.3.6 // indirect
        github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 // indirect
        github.com/leekchan/accounting v0.0.0-20180703100437-18a1925d6514
        github.com/markbates/going v1.0.2
        github.com/markbates/grift v1.0.5
        github.com/markbates/inflect v1.0.4
        github.com/matcornic/hermes v1.2.0
        github.com/mattn/go-runewidth v0.0.3 // indirect
        github.com/monoculum/formam v0.0.0-20180901015400-4e68be1d79ba
        github.com/newrelic/go-agent v2.1.0+incompatible
        github.com/olekukonko/tablewriter v0.0.1 // indirect
        github.com/pkg/errors v0.8.0
        github.com/sendgrid/rest v2.4.1+incompatible // indirect
        github.com/sendgrid/sendgrid-go v3.4.1+incompatible
        github.com/sirupsen/logrus v1.1.1
        github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
        github.com/stanislas-m/mocksmtp v1.0.0
        github.com/stretchr/testify v1.2.2
        golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16
        gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
)

Context Logger

When running periodic tasks in production usually there is a need to follow the details of what the task is doing, it would be great if the grift Context could have a Logger() method (as buffalo does) that returns a logger one could set in the grift init.

This would allow to add custom loggers and have it handy while the task is running. Another cool addition would be to call c.Logger().Error(err) if the task returns an error.

flag issue

hi,

ive encounter issue when i tried to run and pass in parameter with - numbers.
where by -1 is the limit of records to unlimited. it does works if we dont use any - hypen.
example:

grift mytask -1

and also grift does not support flags like --limit or -limit right?

thanks

Cannot bootstrap grifts directory

When I follow the documentation, to get started I use grift init. I don't get any error message, just an exit code of 255.

If I modify the source to have it print the error, the error is no grifts directory exists. Once I make the grifts directory and run grifts init again, I get an error message grift_runner.go:4:8: no buildable Go source files in ....

Using grifts jim doesn't work either.

Then, I went ahead and copied the example grift file from the readme into a grifts folder. Now grift list works, but grift init and grift jim don't work. Both the failed commands give exit status 1 and a message No task named '...' defined.

EDIT:

I've done a bit more testing... and even things like grift --help, grift --version, or just grift have issues.

Grift not using vendor as a default path.

I m using golang dep for dependancy management.
**grift routes** /go/src/grift.build/8c375bea22/grifts/migrate.go:12:2: cannot find package "github.com/emirpasic/gods/sets/hashset" in any of: /usr/local/go/src/github.com/emirpasic/gods/sets/hashset (from $GOROOT) /go/src/github.com/emirpasic/gods/sets/hashset (from $GOPATH) /go/src/grift.build/8c375bea22/grifts/migrate.go:13:2: cannot find package "github.com/jinzhu/gorm" in any of: /usr/local/go/src/github.com/jinzhu/gorm (from $GOROOT) /go/src/github.com/jinzhu/gorm (from $GOPATH)

But Grift still searching only in GOPATH & GOROOT

build fails in go module

Steps to reproduce:

mkdir testgrift && cd "$_"  #(outside of $GOPATH)
git init
git remote add origin [email protected]:rrrkren/testgrift.git
go mod init
grift init
grift hello

gives error:

build github.com/rrrkren/testgrift/.grifter: cannot find module for path testgrift/grifts
exit status 1

when I do this process without the git part everything work fine

grifter/main.go:4:8 cannot find package

I tried to integrate grift to my buffalo project.

After installing grift by go get github.com/markbates/grift

Still it's showing me -bash: grift: command not found . I have created GOPATH correctly. buffalo command is working fine.

And also if I run grift inside my buffalo project, it shows me this error.

.grifter/main.go:4:8: cannot find package "Users/dilumnavanjana/Documents/Singapore/big-api/grifts" in any of:
	/usr/local/go/src/Users/dilumnavanjana/Documents/project (from $GOROOT)
	/Users/dilumnavanjana/go/src/Users/dilumnavanjana/Documents/project/grifts (from $GOPATH)

The GOPATH I configured is /Users/dilumnavanjana/go, here it's showing a wrong GOPATH.

What is the reason for all these??

grifter/main.go:4:8: cannot find package

grifter/main.go:4:8: cannot find package "fixme_backend/grifts" in any of: /usr/local/Cellar/go/1.11.5/libexec/src/fixme_backend/grifts (from $GOROOT) /Users/biboswanroy/Go/src/fixme_backend/grifts (from $GOPATH)

Current directory: /Users/biboswanroy/Go/src/github.com/ossn/fixme_backend/
GOPATH=/Users/biboswanroy/Go
I have grift command working.

I see same issue before as well but it didn't help me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.