Skip to content

Commit 501a922

Browse files
committed
Dirs passed as arguments
1 parent 883fb5f commit 501a922

File tree

4 files changed

+97
-103
lines changed

4 files changed

+97
-103
lines changed

README.md

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<img src="spread-logo-text.svg" width="40%" >
1+
<img src="spread-logo-text.svg" width="50%" >
2+
3+
A simple command to run scripts/commands in multiple directories in sequence. Spread is designed to do one thing and do it well. It is a simpler alternative to GNU Parallel.
24

3-
A simple command to help running scripts/commands in multiple subdirectories. Spread is designed to do one thing and do it well. It is a simpler alternative to GNU Parallel.
45

56
## Installation
67

@@ -16,12 +17,14 @@ Using go:
1617
$ go get github.com/tfogo/spread
1718
```
1819

20+
1921
## Usage
2022

2123
```
22-
$ spread <commands>
24+
$ spread [command] [directories...]
2325
```
2426

27+
2528
### Example
2629

2730
Say we're in a directory `A` with three subdirectories which all have `README.md` files:
@@ -36,49 +39,55 @@ A
3639
└── README.md
3740
```
3841

39-
To append some text to each `README.md` file at once, run:
42+
To append some text to each `README.md` file, run:
4043

4144
```
4245
$ spread 'echo text >> README.md'
4346
```
4447

45-
The arguments to `spread` are run in each subdirectory.
48+
The first argument to `spread` is the command to run in each subdirectory.
4649

47-
### Running multiple commands
4850

49-
Spread will run commands in sequence on each subdirectory. If any command in the sequence fails, the rest of the commands will not run. Therefore, you can run commands based on the exit code of previous commands. For example, running tests:
51+
### Specifying directories
52+
53+
By default `spread` will run the command in all the subdirectories of the present working directory. You can specify directories in which to run the command by adding further arguments:
5054

5155
```
52-
$ spread 'npm test' 'git push origin master'
56+
$ spread 'echo text >> README.md' dir1 dir2
5357
```
5458

55-
### Excluding subdirectories
5659

57-
To exclude subdirectories, use `-x`:
60+
### Running multiple commands
61+
62+
A common use is chaining multiple commands based on the exit code of previous commands using `&&`. For example, running tests then pushing to a git remote:
5863

5964
```
60-
$ spread -x dir1 'echo text >> README.md'
65+
$ spread 'npm test && git push origin master'
6166
```
6267

63-
`-x` can take a glob such as `dir{1,2}`.
6468

65-
## Reference
69+
### No-ops
70+
71+
If you need to run a no-op, use an empty string, a colon, or `true` as the first argument.
6672

6773
```
68-
NAME:
69-
spread - Run commands in subdirectories
74+
$ spread ''
75+
$ spread :
76+
$ spread true
77+
```
7078

71-
USAGE:
72-
spread [global options] command [command options] [arguments...]
7379

74-
VERSION:
75-
0.0.0
80+
### Help
7681

77-
COMMANDS:
78-
help, h Shows a list of commands or help for one command
82+
To view the help, run `spread` without any arguments or pass the `--help` or `-h` flags.
7983

80-
GLOBAL OPTIONS:
81-
--exclude value, -x value Glob of directories to exclude
82-
--help, -h show help
83-
--version, -v print the version
84-
```
84+
85+
## License (MIT)
86+
87+
Copyright 2018 Tim Fogarty
88+
89+
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:
90+
91+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
92+
93+
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.

spread-logo-small.svg

Lines changed: 0 additions & 36 deletions
This file was deleted.

spread-logo-text.svg

Lines changed: 14 additions & 13 deletions
Loading

spread.go

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
func main() {
1414
app := cli.NewApp()
1515
app.Name = "spread"
16-
app.Usage = "Run scripts and commands in multiple directories at once. \n\nWEBSITE: \n\n github.com/tfogo/spread\n\nDESCRIPTION:\n\n Spread is designed to do one thing and do it well. It is a simpler alternative to GNU Parallel. Spread will run commands in sequence on each subdirectory. If any command in the sequence fails, the rest of the commands will not run. Therefore, you can run commands based on the exit code of previous commands. \n\nEXAMPLES: \n\n spread 'npm test' 'git push origin master'"
17-
app.Version = "v1.0.1"
18-
app.UsageText = "spread [global options] [your commands]"
16+
app.Usage = "Run scripts and commands in multiple directories. \n\nWEBSITE: \n\n github.com/tfogo/spread\n\nDESCRIPTION: \n\n The first argument to spread is the command that will be run. By default, spread will run the command in all the subdirectories of the present working directory. Add a list of files as arguments after the command to specify the directories in which the command should run.\n\nEXAMPLES: \n\n spread 'npm test && git push origin master'\n spread ls dir1 dir2 dir3"
17+
app.Version = "v2.0.0"
18+
app.UsageText = "spread [your command] [directories ...]"
1919
app.HideHelp = true
2020

2121
app.Authors = []cli.Author{
@@ -26,11 +26,6 @@ func main() {
2626
}
2727

2828
app.Flags = []cli.Flag {
29-
cli.StringFlag {
30-
Name: "exclude, x",
31-
Value: "",
32-
Usage: "glob of directories to exclude",
33-
},
3429
cli.BoolFlag {
3530
Name: "help, h",
3631
Usage: "show help",
@@ -48,29 +43,55 @@ func action(c *cli.Context) error {
4843
return nil
4944
}
5045

51-
commands := c.Args()
52-
exclude := c.String("exclude")
53-
54-
files, err := ioutil.ReadDir(".")
55-
if err != nil {
56-
log.Fatal(err)
46+
if len(c.Args()) == 0 {
47+
cli.ShowAppHelp(c)
48+
return nil
5749
}
5850

59-
errors := make([]error, 0, len(files))
51+
command, fileArgs := c.Args()[0], c.Args()[1:]
52+
files := make([]string, 0, len(fileArgs))
6053

61-
for _, file := range files {
54+
if len(fileArgs) == 0 {
55+
dirs, err := ioutil.ReadDir(".")
56+
if err != nil {
57+
log.Fatal(err)
58+
}
59+
for _, fileInfo := range dirs {
60+
file, err := filepath.Abs(fileInfo.Name())
61+
if err != nil {
62+
log.Fatal(err)
63+
}
6264

63-
excluded, _ := filepath.Match(exclude, file.Name())
65+
if fileInfo.IsDir() {
66+
files = append(files, file)
67+
}
68+
}
69+
} else {
70+
for _, fileString := range fileArgs {
71+
file, err := filepath.Abs(fileString)
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
fileInfo, err := os.Stat(file)
76+
if err != nil {
77+
log.Fatal(err)
78+
}
6479

65-
if file.IsDir() && !excluded {
66-
// fmt.Println(file.Name())
67-
for _, command := range commands {
68-
err := runCmd(command, file)
69-
if err != nil {
70-
errors = append(errors, err)
71-
break
72-
}
80+
if fileInfo.IsDir() {
81+
files = append(files, file)
82+
} else {
83+
log.Fatal(file, " is not a directory")
7384
}
85+
86+
}
87+
}
88+
89+
errors := make([]error, 0, len(files))
90+
91+
for _, file := range files {
92+
err := runCmd(command, file)
93+
if err != nil {
94+
errors = append(errors, err)
7495
}
7596
}
7697

@@ -79,13 +100,12 @@ func action(c *cli.Context) error {
79100
} else {
80101
return nil
81102
}
82-
83103
}
84104

85-
func runCmd(command string, file os.FileInfo) error {
105+
func runCmd(command string, file string) error {
86106

87107
cmd := exec.Command("sh", "-c", command)
88-
cmd.Dir = file.Name()
108+
cmd.Dir = file
89109
cmd.Stdout = os.Stdout
90110
cmd.Stderr = os.Stderr
91111

0 commit comments

Comments
 (0)