Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Current Post #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
python-ruby-golang
----------

A comparison of click (Python), thor (Ruby), and cli.go (Golang) for building a very simple command-line tool.

## QuickStart

See the README.md in each of the subdirectories for more information.

## Blog Post

[Coming Soon!]()
21 changes: 21 additions & 0 deletions golang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
gomr: go [m]utli-[r]epository manager
-----

gomr is a golang command-line tool used to execute arbitrary commands on a set of tagged directories.

## Features

* Configuration is stored in the registered directory (allows for version-controlled configuration)
* Any arbitrary command can be run, no ties to version-control specific commands.


## Installation

I'm not distributing binaries yet (TODO) for now checkout and `go install` to build the binary.

## Command Help

For help run:
* `gomr --help`
* `gomr register --help`
* `gomr run --help`
60 changes: 60 additions & 0 deletions golang/gomr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"github.com/codegangsta/cli"
"os"
)

func main() {
app := cli.NewApp()
app.Name = "gomr"
app.Author = "Kyle W. Purdon"
app.Version = "0.0.1"
app.Usage = "multi-repository manager in go"
app.Commands = []cli.Command{
{
Name: "register",
Usage: "register a directory",
Action: register,
Flags: []cli.Flag{
cli.StringFlag{
Name: "directory, d",
Value: "./",
Usage: "directory to tag",
},
cli.StringFlag{
Name: "tag, t",
Value: "default",
Usage: "tag to add for directory",
},
cli.BoolFlag{
Name: "append",
Usage: "append the tag to an existing registered directory",
},
},
},
{
Name: "run",
Usage: "run a command",
Action: run,
Flags: []cli.Flag{
cli.StringFlag{
Name: "basepath, b",
Value: "./",
Usage: "path to begin the recursive search",
},
cli.StringFlag{
Name: "tag, t",
Value: "default",
Usage: "tag to add for directory",
},
cli.BoolFlag{
Name: "dryrun, d",
Usage: "print (dont execute) the commands that will be run",
},
},
},
}

app.Run(os.Args)
}
50 changes: 50 additions & 0 deletions golang/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"github.com/codegangsta/cli"
"github.com/robfig/config"
"os"
"path"
"strings"
)

func AppendIfMissing(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}

func register(ctx *cli.Context) {

fn := path.Join(ctx.String("directory"), ".gomr")

newTags := strings.Split(ctx.String("tag"), ",")

if ctx.Bool("append") {
if _, err := os.Stat(fn); err == nil {
cfg, _ := config.ReadDefault(".gomr")
curTags, _ := cfg.String("gomr", "tags")

for _, tag := range strings.Split(curTags, ",") {
newTags = AppendIfMissing(newTags, tag)
}
} else {
err := "append used, existing file not found."
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

outTags := strings.Join(newTags, ",")

outCfg := config.NewDefault()
outCfg.AddSection("gomr")
outCfg.AddOption("gomr", "tags", outTags)
outCfg.WriteFile(fn, 0644, "gomr configuration file")

return
}
57 changes: 57 additions & 0 deletions golang/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"github.com/codegangsta/cli"
"github.com/robfig/config"
"os"
"os/exec"
"path/filepath"
"strings"
)

func RunGomr(ctx *cli.Context) filepath.WalkFunc {
return func(path string, f os.FileInfo, err error) error {

tag := ctx.String("tag")
dryrun := ctx.Bool("dryrun")

if len(ctx.Args()) == 0 {
panic("no command given")
}

command := ctx.Args()[0]

if strings.Contains(path, ".gomr") {

cfg, _ := config.ReadDefault(path)
gomrTags, _ := cfg.String("gomr", "tags")

if strings.Contains(gomrTags, tag) {
if dryrun {
fmt.Printf("Would run %s in %s\n", command, filepath.Dir(path))
} else {
os.Chdir(filepath.Dir(path))
cmd := exec.Command("sh", "-c", command)
stdout, err := cmd.Output()

if err != nil {
panic(err.Error())
}

println(string(stdout))
}
}

}
return nil
}
}

func run(ctx *cli.Context) {

root := ctx.String("basepath")
filepath.Walk(root, RunGomr(ctx))

return
}
Loading