Skip to content

Commit d4562cf

Browse files
committed
chore: Do not link parallel command by default
1 parent 9458477 commit d4562cf

File tree

10 files changed

+71
-22
lines changed

10 files changed

+71
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Go rewrite of [moreutils](http://kitenet.net/~joey/code/moreutils/).
1111
- **[combine](docs/combine.md)**: Combine sets of lines from two files using boolean operations
1212
- **[ifne](docs/ifne.md)**: Run a command if the standard input is not empty
1313
- **[mispipe](docs/mispipe.md)**: Pipe two commands, returning the exit status of the first
14-
- **[parallel](docs/parallel.md)**: Run multiple jobs at once
14+
- **[parallel](docs/moreutils_parallel.md)**: Run multiple jobs at once
1515
- **[pee](docs/pee.md)**: Tee standard input to pipes
1616
- **[sponge](docs/sponge.md)**: Soak up standard input and write to a file
1717
- **[ts](docs/ts.md)**: Timestamp standard input

cmd/cmdutil/subcommands/subcommands.go

+21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package subcommands
33
import (
44
"errors"
55
"fmt"
6+
"iter"
67
"path/filepath"
8+
"slices"
79
"strings"
810

911
"github.com/gabe565/moreutils/cmd/chronic"
@@ -37,6 +39,25 @@ func All(opts ...cmdutil.Option) []*cobra.Command {
3739
}
3840
}
3941

42+
func DefaultExcludes() []string {
43+
return []string{parallel.Name}
44+
}
45+
46+
func Without(excludes []string, opts ...cmdutil.Option) iter.Seq[*cobra.Command] {
47+
if len(excludes) == 0 {
48+
excludes = DefaultExcludes()
49+
}
50+
return func(yield func(*cobra.Command) bool) {
51+
for _, cmd := range All(opts...) {
52+
if !slices.Contains(excludes, cmd.Name()) {
53+
if !yield(cmd) {
54+
return
55+
}
56+
}
57+
}
58+
}
59+
}
60+
4061
var ErrUnknownCommand = errors.New("unknown command")
4162

4263
func Choose(name string, opts ...cmdutil.Option) (*cobra.Command, error) {

cmd/install/install.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
FlagSymbolic = "symbolic"
1515
FlagForce = "force"
1616
FlagRelative = "relative"
17+
FlagExclude = "exclude"
1718
)
1819

1920
func New() *cobra.Command {
@@ -29,6 +30,7 @@ func New() *cobra.Command {
2930
cmd.Flags().BoolP(FlagSymbolic, "s", false, "Create symbolic links instead of hard links")
3031
cmd.Flags().BoolP(FlagForce, "f", false, "Overwrite existing files")
3132
cmd.Flags().BoolP(FlagRelative, "r", false, "Create relative symbolic links")
33+
cmd.Flags().StringSlice(FlagExclude, subcommands.DefaultExcludes(), "Subcommands that will not be linked")
3234

3335
return cmd
3436
}
@@ -82,9 +84,14 @@ func run(cmd *cobra.Command, args []string) error {
8284
src = filepath.Join(relPath, filepath.Base(src))
8385
}
8486

87+
excludes, err := cmd.Flags().GetStringSlice(FlagExclude)
88+
if err != nil {
89+
panic(err)
90+
}
91+
8592
var errs []error
86-
for _, cmd := range subcommands.All() {
87-
dst := path.Join(dst, cmd.Name())
93+
for subCmd := range subcommands.Without(excludes) {
94+
dst := path.Join(dst, subCmd.Name())
8895
if err := link(symbolic, src, dst); err != nil {
8996
if force {
9097
if err := os.Remove(dst); err == nil {

docs/moreutils_install.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ moreutils install dir [flags]
99
### Options
1010

1111
```
12-
-f, --force Overwrite existing files
13-
-h, --help help for install
14-
-r, --relative Create relative symbolic links
15-
-s, --symbolic Create symbolic links instead of hard links
12+
--exclude strings Subcommands that will not be linked (default [parallel])
13+
-f, --force Overwrite existing files
14+
-h, --help help for install
15+
-r, --relative Create relative symbolic links
16+
-s, --symbolic Create symbolic links instead of hard links
1617
```
1718

1819
### SEE ALSO

docs/parallel.md renamed to docs/moreutils_parallel.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
## parallel
1+
## moreutils parallel
22

33
Run multiple jobs at once
44

55
```
6-
parallel command -- arg... [flags]
6+
moreutils parallel command -- arg... [flags]
77
```
88

99
### Options
@@ -13,7 +13,6 @@ parallel command -- arg... [flags]
1313
-j, --jobs string Number of jobs to run in parallel. Can be a number or a percentage of CPU cores. (default "10")
1414
-n, --num-args int Number of arguments to pass to a command at a time. Default is 1. Incompatible with -i (default 1)
1515
-i, --replace Normally the argument is added to the end of the command. With this option, instances of "{}" in the command are replaced with the argument.
16-
-v, --version version for parallel
1716
```
1817

1918
### SEE ALSO

internal/generate/completions/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"os"
55
"path/filepath"
6+
"slices"
67
"time"
78

89
"github.com/gabe565/moreutils/cmd"
@@ -39,12 +40,13 @@ func main() {
3940
panic(err)
4041
}
4142

43+
cmds := append(slices.Collect(subcommands.Without(nil)), cmd.New(cmd.Name))
4244
for _, shell := range []string{shellBash, shellZsh, shellFish} {
4345
basePath := filepath.Join("completions", shell)
4446
if err := os.MkdirAll(basePath, 0o777); err != nil {
4547
panic(err)
4648
}
47-
for _, subCmd := range append(subcommands.All(), cmd.New(cmd.Name)) {
49+
for _, subCmd := range cmds {
4850
var path string
4951
switch shell {
5052
case shellBash:

internal/generate/docs/main.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/gabe565/moreutils/cmd"
1212
"github.com/gabe565/moreutils/cmd/cmdutil"
1313
"github.com/gabe565/moreutils/cmd/cmdutil/subcommands"
14-
"github.com/spf13/cobra"
14+
"github.com/gabe565/moreutils/internal/util"
1515
"github.com/spf13/cobra/doc"
1616
)
1717

@@ -33,10 +33,10 @@ func main() {
3333

3434
opts := []cmdutil.Option{cmd.WithVersion("beta")}
3535
root := cmd.New(cmd.Name, opts...)
36-
cmds := append(subcommands.All(opts...), root)
36+
cmds := append(slices.Collect(subcommands.Without(nil, opts...)), root)
3737
for _, subCmd := range root.Commands() {
3838
// Add any commands which aren't standalone
39-
if !slices.ContainsFunc(cmds, func(cmd *cobra.Command) bool { return cmd.Name() == subCmd.Name() }) {
39+
if !util.CmdsContains(cmds, subCmd) {
4040
cmds = append(cmds, subCmd)
4141
}
4242
}
@@ -92,8 +92,13 @@ func main() {
9292
}
9393

9494
var list []byte
95+
linked := slices.Collect(subcommands.Without(nil))
9596
for _, subCmd := range subcommands.All() {
96-
list = append(list, []byte("- **["+subCmd.Name()+"](docs/"+subCmd.Name()+".md)**: "+subCmd.Short+"\n")...)
97+
docPath := subCmd.Name() + ".md"
98+
if !util.CmdsContains(linked, subCmd) {
99+
docPath = cmd.Name + "_" + docPath
100+
}
101+
list = append(list, []byte("- **["+subCmd.Name()+"](docs/"+docPath+")**: "+subCmd.Short+"\n")...)
97102
}
98103

99104
readmeContents = slices.Concat(beforeApplets, []byte(beforeMarker), list, []byte(afterMarker), afterApplets)

internal/generate/links/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
panic(err)
3232
}
3333

34-
for _, subCmd := range subcommands.All() {
34+
for subCmd := range subcommands.Without(nil) {
3535
path := filepath.Join("links", subCmd.Name())
3636
if err := os.Symlink(cmd.Name, path); err != nil {
3737
panic(err)

internal/generate/manpages/main.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
"github.com/gabe565/moreutils/cmd"
1515
"github.com/gabe565/moreutils/cmd/cmdutil/subcommands"
16-
"github.com/spf13/cobra"
16+
"github.com/gabe565/moreutils/internal/util"
1717
"github.com/spf13/cobra/doc"
1818
flag "github.com/spf13/pflag"
1919
)
@@ -45,14 +45,15 @@ func main() {
4545
}
4646

4747
root := cmd.New(cmd.Name)
48-
cmds := append(subcommands.All(), root)
48+
cmds := append(slices.Collect(subcommands.Without(nil)), root)
4949
for _, subCmd := range root.Commands() {
5050
// Add any commands which aren't standalone
51-
if !slices.ContainsFunc(cmds, func(cmd *cobra.Command) bool { return cmd.Name() == subCmd.Name() }) {
51+
if !util.CmdsContains(cmds, subCmd) {
5252
cmds = append(cmds, subCmd)
5353
}
5454
}
5555

56+
linked := slices.Collect(subcommands.Without(nil))
5657
for _, subCmd := range cmds {
5758
subCmd.DisableAutoGenTag = true
5859
header := doc.GenManHeader{
@@ -64,8 +65,8 @@ func main() {
6465
}
6566

6667
name := subCmd.Name() + ".1.gz"
67-
if subCmd.HasParent() {
68-
name = subCmd.Parent().Name() + "-" + name
68+
if subCmd.Name() != cmd.Name && !util.CmdsContains(linked, subCmd) {
69+
name = cmd.Name + "-" + name
6970
}
7071
path := filepath.Join("manpages", name)
7172
out, err := os.Create(path)
@@ -107,7 +108,7 @@ func (s *seeAlsoWriter) Write(p []byte) (int, error) {
107108
n := len(p)
108109
headerIdx := bytes.Index(p, []byte(seeAlsoLine))
109110
p, temp := p[:headerIdx], p[headerIdx:]
110-
for _, subCmd := range subcommands.All() {
111+
for subCmd := range subcommands.Without(nil) {
111112
temp = bytes.ReplaceAll(temp, []byte(cmd.Name+"-"+subCmd.Name()), []byte(subCmd.Name()))
112113
}
113114
_, err := s.w.Write(append(p, temp...))

internal/util/cobra.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package util
2+
3+
import (
4+
"slices"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func CmdsContains(cmds []*cobra.Command, target *cobra.Command) bool {
10+
return slices.ContainsFunc(cmds, func(cmd *cobra.Command) bool {
11+
return cmd.Name() == target.Name()
12+
})
13+
}

0 commit comments

Comments
 (0)