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

Skip to content

Commit d4e1f07

Browse files
committed
Merge pull request #1 from kpurdon/master
Current Post
2 parents d03a8c8 + a644e08 commit d4e1f07

File tree

17 files changed

+891
-0
lines changed

17 files changed

+891
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
python-ruby-golang
2+
----------
3+
4+
A comparison of click (Python), thor (Ruby), and cli.go (Golang) for building a very simple command-line tool.
5+
6+
## QuickStart
7+
8+
See the README.md in each of the subdirectories for more information.
9+
10+
## Blog Post
11+
12+
[Coming Soon!]()

golang/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
gomr: go [m]utli-[r]epository manager
2+
-----
3+
4+
gomr is a golang command-line tool used to execute arbitrary commands on a set of tagged directories.
5+
6+
## Features
7+
8+
* Configuration is stored in the registered directory (allows for version-controlled configuration)
9+
* Any arbitrary command can be run, no ties to version-control specific commands.
10+
11+
12+
## Installation
13+
14+
I'm not distributing binaries yet (TODO) for now checkout and `go install` to build the binary.
15+
16+
## Command Help
17+
18+
For help run:
19+
* `gomr --help`
20+
* `gomr register --help`
21+
* `gomr run --help`

golang/gomr.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"github.com/codegangsta/cli"
5+
"os"
6+
)
7+
8+
func main() {
9+
app := cli.NewApp()
10+
app.Name = "gomr"
11+
app.Author = "Kyle W. Purdon"
12+
app.Version = "0.0.1"
13+
app.Usage = "multi-repository manager in go"
14+
app.Commands = []cli.Command{
15+
{
16+
Name: "register",
17+
Usage: "register a directory",
18+
Action: register,
19+
Flags: []cli.Flag{
20+
cli.StringFlag{
21+
Name: "directory, d",
22+
Value: "./",
23+
Usage: "directory to tag",
24+
},
25+
cli.StringFlag{
26+
Name: "tag, t",
27+
Value: "default",
28+
Usage: "tag to add for directory",
29+
},
30+
cli.BoolFlag{
31+
Name: "append",
32+
Usage: "append the tag to an existing registered directory",
33+
},
34+
},
35+
},
36+
{
37+
Name: "run",
38+
Usage: "run a command",
39+
Action: run,
40+
Flags: []cli.Flag{
41+
cli.StringFlag{
42+
Name: "basepath, b",
43+
Value: "./",
44+
Usage: "path to begin the recursive search",
45+
},
46+
cli.StringFlag{
47+
Name: "tag, t",
48+
Value: "default",
49+
Usage: "tag to add for directory",
50+
},
51+
cli.BoolFlag{
52+
Name: "dryrun, d",
53+
Usage: "print (dont execute) the commands that will be run",
54+
},
55+
},
56+
},
57+
}
58+
59+
app.Run(os.Args)
60+
}

golang/register.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/codegangsta/cli"
6+
"github.com/robfig/config"
7+
"os"
8+
"path"
9+
"strings"
10+
)
11+
12+
func AppendIfMissing(slice []string, i string) []string {
13+
for _, ele := range slice {
14+
if ele == i {
15+
return slice
16+
}
17+
}
18+
return append(slice, i)
19+
}
20+
21+
func register(ctx *cli.Context) {
22+
23+
fn := path.Join(ctx.String("directory"), ".gomr")
24+
25+
newTags := strings.Split(ctx.String("tag"), ",")
26+
27+
if ctx.Bool("append") {
28+
if _, err := os.Stat(fn); err == nil {
29+
cfg, _ := config.ReadDefault(".gomr")
30+
curTags, _ := cfg.String("gomr", "tags")
31+
32+
for _, tag := range strings.Split(curTags, ",") {
33+
newTags = AppendIfMissing(newTags, tag)
34+
}
35+
} else {
36+
err := "append used, existing file not found."
37+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
38+
os.Exit(1)
39+
}
40+
}
41+
42+
outTags := strings.Join(newTags, ",")
43+
44+
outCfg := config.NewDefault()
45+
outCfg.AddSection("gomr")
46+
outCfg.AddOption("gomr", "tags", outTags)
47+
outCfg.WriteFile(fn, 0644, "gomr configuration file")
48+
49+
return
50+
}

golang/run.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/codegangsta/cli"
6+
"github.com/robfig/config"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"strings"
11+
)
12+
13+
func RunGomr(ctx *cli.Context) filepath.WalkFunc {
14+
return func(path string, f os.FileInfo, err error) error {
15+
16+
tag := ctx.String("tag")
17+
dryrun := ctx.Bool("dryrun")
18+
19+
if len(ctx.Args()) == 0 {
20+
panic("no command given")
21+
}
22+
23+
command := ctx.Args()[0]
24+
25+
if strings.Contains(path, ".gomr") {
26+
27+
cfg, _ := config.ReadDefault(path)
28+
gomrTags, _ := cfg.String("gomr", "tags")
29+
30+
if strings.Contains(gomrTags, tag) {
31+
if dryrun {
32+
fmt.Printf("Would run %s in %s\n", command, filepath.Dir(path))
33+
} else {
34+
os.Chdir(filepath.Dir(path))
35+
cmd := exec.Command("sh", "-c", command)
36+
stdout, err := cmd.Output()
37+
38+
if err != nil {
39+
panic(err.Error())
40+
}
41+
42+
println(string(stdout))
43+
}
44+
}
45+
46+
}
47+
return nil
48+
}
49+
}
50+
51+
func run(ctx *cli.Context) {
52+
53+
root := ctx.String("basepath")
54+
filepath.Walk(root, RunGomr(ctx))
55+
56+
return
57+
}

0 commit comments

Comments
 (0)