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

autoversion: Support CSS selectors #394

Merged
merged 2 commits into from
Feb 6, 2024
Merged

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Feb 6, 2024

autoversion currently only supports XPath for searching for versions
in an HTML page.
This works great, but CSS selectors are arguably
more approachable for newcomers, and more widely understood.

This change adds support for CSS selectors to autoversion
using the github.com/andybalholm/cascadia package (BSD2 licensed)
for matching them.

To fit that in, this extracts the search logic into a new abstraction
called htmlMatcher with an implementation for XPath and one for CSS.

Following this change, you can do something like the following:

auto-version {
  html {
    url = "https://example.com"
    css = "div.version"
  }
}

It is invalid to specify both xpath and css
in the same auto-version block.

autoversion currently only supports XPath for searching for versions
in an HTML page.
This works great, but CSS selectors are arguably
more approachable for newcomers, and more widely understood.

This change adds support for CSS selectors to autoversion
using the github.com/andybalholm/cascadia package (BSD2 licensed)
for matching them.

To fit that in, this extracts the search logic into a new abstraction
called htmlMatcher with an implementation for XPath and one for CSS.

Following this change, you can do something like the following:

```hcl
auto-version {
  html {
    url = "https://example.com"
    css = "div.version"
  }
}
```

It is invalid to specify both `xpath` and `css`
in the same auto-version block.
@alecthomas
Copy link
Collaborator

That's awesome! I originally chose XPath because it's much more powerful, allowing selection of regex subgroups, among other things. But I think you're right, CSS is much more approachable and should be fine for most use cases.

If no versions are found, we should return an error instead of panicking.
We just need to add a check for the length of the versions slice
before returning the latest version.
@alecthomas alecthomas merged commit a5b7ea4 into cashapp:master Feb 6, 2024
6 checks passed
@abhinav abhinav deleted the css-matcher branch February 6, 2024 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants