Skip to content

Commit 9ca16b4

Browse files
authored
Merge pull request #72 from vitessio/cross-platform-find
Run `find` in a cross-platform manner
2 parents fb8a75e + 30c5f91 commit 9ca16b4

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

go/pull_request.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -546,14 +546,21 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR(
546546
if docsVersion == "main" {
547547
// We need to replace "main" with whatever the highest version under
548548
// content/en/docs is.
549-
find, err := shell.NewContext(ctx,
550-
"find",
551-
"-sE",
552-
"content/en/docs",
549+
//
550+
// MacOS does not support -regextype flag, but Unix (non-darwin) does
551+
// not support the -E equivalent. Similarly, Unix does not support the
552+
// lexicographic sort opt (-s), so we rely on sort's dictionary sort
553+
// (-d) instead.
554+
//
555+
// Unix version: find content/en/docs -regextype posix-extended -maxdepth 1 -type d -regex ... | sort -d
556+
// MacOS version: find -E content/en/docs -maxdepth 1 -type d -regex ... | sort -d
557+
args := shell.FindRegexpExtended("content/en/docs",
558+
"-maxdepth", "1",
553559
"-type", "d",
554-
"-depth", "1",
555-
"-regex", `.*/[0-9]+.[0-9]+`,
556-
).InDir(website.LocalDir).Output()
560+
"-regex", `.*/[0-9]+.[0-9]+`, "|",
561+
"sort", "-d",
562+
)
563+
find, err := shell.NewContext(ctx, "bash", "-c", strings.Join(args, " ")).InDir(website.LocalDir).Output()
557564
if err != nil {
558565
return nil, errors.Wrapf(err, "Failed to `find` latest docs version to %s for %s", op, pr.GetHTMLURL())
559566
}

go/shell/shell.go

+20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import (
2525

2626
type cmd exec.Cmd
2727

28+
var (
29+
globalRegexpOpt string
30+
regexpTypeOpt []string
31+
)
32+
2833
// New returns a new command can be run.
2934
func New(name string, arg ...string) *cmd {
3035
return NewContext(context.Background(), name, arg...)
@@ -35,6 +40,21 @@ func NewContext(ctx context.Context, name string, arg ...string) *cmd {
3540
return (*cmd)(exec.CommandContext(ctx, name, arg...))
3641
}
3742

43+
// FindRegexpExtended returns a slice of arguments for doing a `find` command
44+
// using extended regular expressions in a cross-platform-friendly manner.
45+
func FindRegexpExtended(path string, expressions ...string) (args []string) {
46+
args = []string{path}
47+
if globalRegexpOpt != "" {
48+
args = []string{globalRegexpOpt, path}
49+
}
50+
51+
if len(regexpTypeOpt) > 0 {
52+
args = append(args, regexpTypeOpt...)
53+
}
54+
55+
return append(args, expressions...)
56+
}
57+
3858
// InDir updates the working directory of the command and returns it.
3959
//
4060
// Must be called before running the command.

go/shell/shell_darwin.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build darwin
2+
3+
package shell
4+
5+
func init() {
6+
globalRegexpOpt = "-E"
7+
}

go/shell/shell_unix.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build unix && !darwin
2+
3+
package shell
4+
5+
func init() {
6+
regexpTypeOpt = []string{
7+
// This is not a typo. It is "regexp" in goland and "regex" in POSIX land. ¯\_(ツ)_/¯
8+
"-regextype",
9+
"posix-extended",
10+
}
11+
}

0 commit comments

Comments
 (0)