diff --git a/go/pull_request.go b/go/pull_request.go index 33801c4..6199c48 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -546,14 +546,21 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( if docsVersion == "main" { // We need to replace "main" with whatever the highest version under // content/en/docs is. - find, err := shell.NewContext(ctx, - "find", - "-sE", - "content/en/docs", + // + // MacOS does not support -regextype flag, but Unix (non-darwin) does + // not support the -E equivalent. Similarly, Unix does not support the + // lexicographic sort opt (-s), so we rely on sort's dictionary sort + // (-d) instead. + // + // Unix version: find content/en/docs -regextype posix-extended -maxdepth 1 -type d -regex ... | sort -d + // MacOS version: find -E content/en/docs -maxdepth 1 -type d -regex ... | sort -d + args := shell.FindRegexpExtended("content/en/docs", + "-maxdepth", "1", "-type", "d", - "-depth", "1", - "-regex", `.*/[0-9]+.[0-9]+`, - ).InDir(website.LocalDir).Output() + "-regex", `.*/[0-9]+.[0-9]+`, "|", + "sort", "-d", + ) + find, err := shell.NewContext(ctx, "bash", "-c", strings.Join(args, " ")).InDir(website.LocalDir).Output() if err != nil { return nil, errors.Wrapf(err, "Failed to `find` latest docs version to %s for %s", op, pr.GetHTMLURL()) } diff --git a/go/shell/shell.go b/go/shell/shell.go index 2a96d71..d304cb6 100644 --- a/go/shell/shell.go +++ b/go/shell/shell.go @@ -25,6 +25,11 @@ import ( type cmd exec.Cmd +var ( + globalRegexpOpt string + regexpTypeOpt []string +) + // New returns a new command can be run. func New(name string, arg ...string) *cmd { return NewContext(context.Background(), name, arg...) @@ -35,6 +40,21 @@ func NewContext(ctx context.Context, name string, arg ...string) *cmd { return (*cmd)(exec.CommandContext(ctx, name, arg...)) } +// FindRegexpExtended returns a slice of arguments for doing a `find` command +// using extended regular expressions in a cross-platform-friendly manner. +func FindRegexpExtended(path string, expressions ...string) (args []string) { + args = []string{path} + if globalRegexpOpt != "" { + args = []string{globalRegexpOpt, path} + } + + if len(regexpTypeOpt) > 0 { + args = append(args, regexpTypeOpt...) + } + + return append(args, expressions...) +} + // InDir updates the working directory of the command and returns it. // // Must be called before running the command. diff --git a/go/shell/shell_darwin.go b/go/shell/shell_darwin.go new file mode 100644 index 0000000..769a9b8 --- /dev/null +++ b/go/shell/shell_darwin.go @@ -0,0 +1,7 @@ +//go:build darwin + +package shell + +func init() { + globalRegexpOpt = "-E" +} diff --git a/go/shell/shell_unix.go b/go/shell/shell_unix.go new file mode 100644 index 0000000..924a719 --- /dev/null +++ b/go/shell/shell_unix.go @@ -0,0 +1,11 @@ +//go:build unix && !darwin + +package shell + +func init() { + regexpTypeOpt = []string{ + // This is not a typo. It is "regexp" in goland and "regex" in POSIX land. ¯\_(ツ)_/¯ + "-regextype", + "posix-extended", + } +}