Skip to content

Commit

Permalink
Allow specifying commands and verbose as flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonkpils committed Oct 11, 2015
1 parent 89b7066 commit b58ac26
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ snag

From a project with a `.snag.yml` file and develop away!

### Quick Use

If you find yourself working on a project that does not contain a snag file and
still want to use snag, you can use flags to specify commands to run.

The `-c` flag allows specifying a command just like the snag file and can
be defined more than once for multiple commands. The order of the commands
depends on the order of the flag.

```sh
snag -c "echo snag world" -c "echo rocks"
```

will output

```sh
|Passed | echo snag world
|Passed | echo rocks
```

The `-v` flag enables verbose output. It will also override the `verbose`
option form the snag file if it is defined to false.

**NOTE**: using the `-c` flag will skip reading a snag file even if it
exists in the current working directory.

## Caveats

* Endless build loops
Expand Down
45 changes: 36 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import (
"gopkg.in/yaml.v2"
)

type cmdSlice []string

func (c *cmdSlice) String() string {
return fmt.Sprintf("%s", *c)
}

func (c *cmdSlice) Set(value string) error {
*c = append(*c, value)
return nil
}

var cliCmds cmdSlice

type config struct {
Script []string `yaml:"script"`
IgnoredItems []string `yaml:"ignore"`
Expand All @@ -21,9 +34,14 @@ const (
VersionOutput = "Snag version " + Version
)

var version bool
var (
version bool
verbose bool
)

func init() {
flag.Var(&cliCmds, "c", "List of commands to execute")
flag.BoolVar(&verbose, "v", false, "Verbose output")
flag.BoolVar(&version, "version", false, "display snag's version")
}

Expand All @@ -34,18 +52,27 @@ func main() {
return
}

in, err := ioutil.ReadFile(".snag.yml")
if err != nil {
log.Fatal("Could not find '.snag.yml' in your current directory")
}

var c config
if err := yaml.Unmarshal(in, &c); err != nil {
log.Fatalf("Could not parse yml file. %s\n", err)
if len(cliCmds) > 0 {
c.Script = cliCmds
} else {
in, err := ioutil.ReadFile(".snag.yml")
if err != nil {
log.Fatal("Could not find '.snag.yml' in your current directory")
}

if err := yaml.Unmarshal(in, &c); err != nil {
log.Fatalf("Could not parse yml file. %s\n", err)
}

}

if len(c.Script) == 0 {
log.Fatal("You must have at least 1 command in your '.snag.yml'")
log.Fatal("You must specify at least 1 command.")
}

if verbose {
c.Verbose = verbose
}

b, err := NewBuilder(c)
Expand Down

0 comments on commit b58ac26

Please sign in to comment.