Skip to content

Commit

Permalink
Merge pull request #72 from vitessio/cross-platform-find
Browse files Browse the repository at this point in the history
Run `find` in a cross-platform manner
  • Loading branch information
frouioui authored Jan 24, 2024
2 parents fb8a75e + 30c5f91 commit 9ca16b4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
21 changes: 14 additions & 7 deletions go/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
20 changes: 20 additions & 0 deletions go/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions go/shell/shell_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build darwin

package shell

func init() {
globalRegexpOpt = "-E"
}
11 changes: 11 additions & 0 deletions go/shell/shell_unix.go
Original file line number Diff line number Diff line change
@@ -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",
}
}

0 comments on commit 9ca16b4

Please sign in to comment.