Skip to content

Commit d495ff2

Browse files
authored
Merge pull request #2 from serpapi/go-rewrite
Rewrite CLI in Go (v0.2.0)
2 parents 570395c + 48157d5 commit d495ff2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2003
-3928
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,38 @@
1-
name: CI/CD Pipeline
1+
name: CI
22

33
on:
44
push:
55
branches: [main, develop]
6-
tags: ["v*"]
76
pull_request:
87
branches: [main]
98

10-
env:
11-
CARGO_TERM_COLOR: always
9+
permissions: {}
1210

1311
jobs:
14-
lint-and-test:
15-
name: Lint + Unit Test
12+
test:
1613
runs-on: ubuntu-latest
17-
1814
steps:
19-
- uses: actions/checkout@v4
20-
21-
- name: Install Rust stable
22-
uses: actions-rust-lang/setup-rust-toolchain@v1
15+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
16+
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
2317
with:
24-
toolchain: stable
25-
components: rustfmt, clippy
26-
27-
- name: Cache cargo registry
28-
uses: actions/cache@v4
29-
with:
30-
path: |
31-
~/.cargo/registry
32-
~/.cargo/git
33-
target
34-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
35-
restore-keys: |
36-
${{ runner.os }}-cargo-
37-
38-
- name: Check formatting
39-
run: cargo fmt -- --check
40-
41-
- name: Run clippy
42-
run: cargo clippy -- -D warnings
43-
44-
- name: Run unit tests
45-
run: cargo test --all-targets
18+
go-version-file: go.mod
19+
- run: go vet ./...
20+
- run: go build ./cmd/serpapi
21+
- run: go test ./pkg/...
4622

47-
e2e-test:
48-
name: E2E Test
23+
e2e:
4924
runs-on: ubuntu-latest
50-
needs: lint-and-test
51-
25+
needs: test
5226
steps:
53-
- uses: actions/checkout@v4
54-
55-
- name: Install Rust stable
56-
uses: actions-rust-lang/setup-rust-toolchain@v1
57-
with:
58-
toolchain: stable
59-
60-
- name: Cache cargo registry
61-
uses: actions/cache@v4
27+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
28+
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
6229
with:
63-
path: |
64-
~/.cargo/registry
65-
~/.cargo/git
66-
target
67-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
68-
restore-keys: |
69-
${{ runner.os }}-cargo-
70-
30+
go-version-file: go.mod
7131
- name: Run E2E tests
7232
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
7333
env:
7434
SERPAPI_KEY: ${{ secrets.SERPAPI_KEY }}
75-
run: cargo test -- --ignored
76-
77-
- name: Skip E2E tests (fork PR)
35+
run: go test ./tests/...
36+
- name: Skip E2E (fork PR)
7837
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
7938
run: echo "Skipping E2E tests - SERPAPI_KEY unavailable in fork PRs"
80-

.github/workflows/release.yml

Lines changed: 14 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -3,137 +3,24 @@ name: Release
33
on:
44
push:
55
tags:
6-
- 'v[0-9]+.[0-9]+.[0-9]+*'
7-
8-
permissions:
9-
contents: write
6+
- "v*"
107

118
jobs:
12-
create-release:
13-
name: Create release
9+
goreleaser:
1410
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
1513
steps:
16-
- uses: actions/checkout@v6
17-
- name: Verify tag matches Cargo.toml version
18-
run: |
19-
TAG="${GITHUB_REF_NAME#v}"
20-
CARGO_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)"/\1/')
21-
if [ "$TAG" != "$CARGO_VERSION" ]; then
22-
echo "::error::Tag $GITHUB_REF_NAME does not match Cargo.toml version $CARGO_VERSION"
23-
exit 1
24-
fi
25-
- name: Create draft release
26-
env:
27-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28-
run: |
29-
gh release create "$GITHUB_REF_NAME" \
30-
--draft \
31-
--title "$GITHUB_REF_NAME" \
32-
--generate-notes
33-
34-
build-release:
35-
name: Build (${{ matrix.target }})
36-
needs: create-release
37-
runs-on: ${{ matrix.os }}
38-
strategy:
39-
fail-fast: true
40-
matrix:
41-
include:
42-
- target: aarch64-apple-darwin
43-
os: macos-latest
44-
- target: x86_64-apple-darwin
45-
os: macos-latest
46-
- target: aarch64-unknown-linux-gnu
47-
os: ubuntu-latest
48-
use_cross: true
49-
- target: x86_64-unknown-linux-gnu
50-
os: ubuntu-latest
51-
- target: x86_64-unknown-linux-musl
52-
os: ubuntu-latest
53-
use_cross: true
54-
- target: x86_64-pc-windows-msvc
55-
os: windows-latest
56-
steps:
57-
- uses: actions/checkout@v6
58-
59-
- name: Install Rust toolchain
60-
uses: actions-rust-lang/setup-rust-toolchain@v1
14+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
6115
with:
62-
target: ${{ matrix.target }}
63-
64-
- name: Prepend Strawberry Perl to PATH (Windows)
65-
if: runner.os == 'Windows'
66-
run: echo "C:\Strawberry\perl\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
67-
68-
- name: Install cross
69-
if: matrix.use_cross
70-
uses: taiki-e/install-action@v2
16+
fetch-depth: 0
17+
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
7118
with:
72-
tool: cross
73-
74-
- name: Build
75-
if: "!matrix.use_cross"
76-
run: cargo build --release --target ${{ matrix.target }}
77-
78-
- name: Build (cross)
79-
if: matrix.use_cross
80-
shell: bash
81-
run: cross build --release --target ${{ matrix.target }}
82-
83-
- name: Package and checksum
84-
id: package
85-
shell: bash
86-
run: |
87-
BIN="serpapi"
88-
TARGET="${{ matrix.target }}"
89-
VERSION="${GITHUB_REF_NAME#v}"
90-
PKG="${BIN}-${VERSION}-${TARGET}"
91-
92-
mkdir "$PKG"
93-
cp README.md LICENSE "$PKG/"
94-
95-
if [[ "$TARGET" == *"windows"* ]]; then
96-
cp "target/${TARGET}/release/${BIN}.exe" "$PKG/"
97-
7z a "${PKG}.zip" "$PKG"
98-
ARCHIVE="${PKG}.zip"
99-
else
100-
cp "target/${TARGET}/release/${BIN}" "$PKG/"
101-
tar czf "${PKG}.tar.gz" "$PKG"
102-
ARCHIVE="${PKG}.tar.gz"
103-
fi
104-
105-
sha256sum "$ARCHIVE" > "${ARCHIVE}.sha256"
106-
echo "archive=$ARCHIVE" >> "$GITHUB_OUTPUT"
107-
echo "sha256=${ARCHIVE}.sha256" >> "$GITHUB_OUTPUT"
108-
109-
- name: Upload to release
110-
shell: bash
111-
env:
112-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
113-
run: |
114-
gh release upload "$GITHUB_REF_NAME" \
115-
"${{ steps.package.outputs.archive }}" \
116-
"${{ steps.package.outputs.sha256 }}"
117-
118-
publish-release:
119-
name: Publish release
120-
needs: build-release
121-
runs-on: ubuntu-latest
122-
steps:
123-
- name: Publish release
124-
env:
125-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
126-
run: gh release edit "$GITHUB_REF_NAME" --draft=false --repo "$GITHUB_REPOSITORY"
127-
128-
publish-crates:
129-
name: Publish to crates.io
130-
needs: publish-release
131-
runs-on: ubuntu-latest
132-
steps:
133-
- uses: actions/checkout@v6
134-
- name: Install Rust toolchain
135-
run: rustup toolchain install stable --profile minimal
136-
- name: Publish
137-
run: cargo publish --locked
19+
go-version-file: go.mod
20+
- uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf # v6.2.1
21+
with:
22+
version: ~> v2
23+
args: release --clean
13824
env:
139-
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
HOMEBREW_TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
/target
1+
# Go binary
2+
/serpapi
3+
/serpapi-test-bin
4+
5+
# IDE
6+
.idea/
7+
.vscode/
8+
tmp/

.goreleaser.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
version: 2
2+
3+
project_name: serpapi
4+
5+
before:
6+
hooks:
7+
- go mod download
8+
9+
builds:
10+
- id: serpapi
11+
main: ./cmd/serpapi
12+
binary: serpapi
13+
env:
14+
- CGO_ENABLED=0
15+
ldflags:
16+
- -s -w -X github.com/serpapi/serpapi-cli/pkg/version.Version={{.Version}}
17+
goos:
18+
- darwin
19+
- linux
20+
- windows
21+
goarch:
22+
- amd64
23+
- arm64
24+
ignore:
25+
- goos: windows
26+
goarch: arm64
27+
28+
archives:
29+
- id: default
30+
name_template: "serpapi_{{.Version}}_{{.Os}}_{{.Arch}}"
31+
files:
32+
- none*
33+
34+
checksum:
35+
name_template: "serpapi_{{.Version}}_checksums.txt"
36+
37+
snapshot:
38+
version_template: "{{.Version}}-next"
39+
40+
changelog:
41+
sort: asc
42+
filters:
43+
exclude:
44+
- "^docs:"
45+
- "^test:"
46+
47+
brews:
48+
- name: serpapi-cli
49+
repository:
50+
owner: serpapi
51+
name: homebrew-tap
52+
token: "{{ .Env.HOMEBREW_TAP_TOKEN }}"
53+
folder: Formula
54+
homepage: https://serpapi.com
55+
description: "HTTP client for structured web search data via SerpApi"
56+
license: MIT
57+
install: |
58+
bin.install "serpapi"
59+
test: |
60+
assert_match version.to_s, shell_output("#{bin}/serpapi --version")
61+
assert_match "search", shell_output("#{bin}/serpapi --help")
62+
assert_match "No API key", shell_output("#{bin}/serpapi account 2>&1", 2)
63+
assert_match "No API key", shell_output("#{bin}/serpapi archive abc123 2>&1", 2)
64+
assert_match "Invalid archive ID", shell_output("#{bin}/serpapi --api-key x archive ../bad 2>&1", 2)

0 commit comments

Comments
 (0)