Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run find in a cross-platform manner #72

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
}
}