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

feat: upgrade the CLI to use the Go API client version 4 #176

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e12580f
chore: update go version and packages
kai687 Jan 29, 2025
5ec691a
feat: add devbox and task
kai687 Jan 29, 2025
178d052
fix: format all go files
kai687 Jan 31, 2025
a41e7f9
chore: Update Dockerfile
kai687 Feb 6, 2025
4f9aaef
feat: add V4 client package
kai687 Jan 29, 2025
73300d9
feat: migrate indices list
kai687 Jan 29, 2025
c97d380
feat: migrate indices copy
kai687 Jan 30, 2025
f03e1f0
feat: migrate indices move command
kai687 Jan 30, 2025
024f3f7
feat: migrate indices clear
kai687 Jan 30, 2025
5f0a47a
feat: initial migration of indices delete (without replicas)
kai687 Jan 30, 2025
ec7d4df
feat: migrate indices export import
kai687 Jan 31, 2025
2b5b243
feat: migrate indices analyze
kai687 Feb 5, 2025
e1eec17
feat: migrate apikeys create
kai687 Jan 31, 2025
884624a
feat: migrate apikeys delete
kai687 Feb 1, 2025
afc7196
feat: migrate search
kai687 Feb 3, 2025
1410222
feat: migrate settings get
kai687 Feb 3, 2025
ceac635
feat: migrate settings set
kai687 Feb 3, 2025
7115bdc
feat: migrate settings import
kai687 Feb 4, 2025
aac8e73
feat: migrate objects browse
kai687 Feb 3, 2025
0efa81a
feat: migrate objects delete
kai687 Feb 3, 2025
d158104
feat: migrate objects import
kai687 Feb 3, 2025
21378e8
feat: migrate objects operations
kai687 Feb 3, 2025
11394c4
feat: migrate objects update
kai687 Feb 5, 2025
63b4924
feat: migrate apikeys list
kai687 Feb 3, 2025
7c2ddb4
feat: migrate apikeys get
kai687 Feb 3, 2025
50d4d57
feat: migrate rules browse
kai687 Feb 3, 2025
96633ef
feat: migrate rules delete
kai687 Feb 4, 2025
cb8e00e
feat: migrate rules import
kai687 Feb 4, 2025
7399ea1
feat: migrate synonyms browse
kai687 Feb 4, 2025
091d81e
feat: migrate synonyms delete
kai687 Feb 4, 2025
2776091
feat: migrate synonyms save
kai687 Feb 4, 2025
6ef6925
feat: migrate synonyms import
kai687 Feb 4, 2025
e54cc83
feat: add dict shortcut
kai687 Feb 4, 2025
b717321
feat: migrate dictionary settings get
kai687 Feb 4, 2025
e9a3d9c
feat: migrate dictionary settings set
kai687 Feb 4, 2025
efd6c44
feat: migrate dictionary entries browse
kai687 Feb 4, 2025
8739b98
feat: migrate dictionary entries clear
kai687 Feb 4, 2025
163084e
feat: migrate dictionary entries delete
kai687 Feb 4, 2025
ebc77af
feat: migrate dictionary entries import
kai687 Feb 4, 2025
3217b0f
feat: remove art command
kai687 Feb 4, 2025
15f9bd3
feat: migrate events tail command
kai687 Feb 5, 2025
3782391
feat: update type in Crawler API to v4
kai687 Feb 5, 2025
6ae5150
fix: remove v3 client from cli
kai687 Feb 5, 2025
135ac55
feat: add --wait flag to all indexing operations
kai687 Feb 7, 2025
31db9f6
chore: pin everything to go 1.19
kai687 Feb 10, 2025
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
9 changes: 9 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# Automatically sets up your devbox environment whenever you cd into this
# directory via our direnv integration:

eval "$(devbox generate direnv --print-envrc)"

# check out https://www.jetpack.io/devbox/docs/ide_configuration/direnv/
# for more details
18 changes: 10 additions & 8 deletions .github/workflows/api-specs-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ name: Scheduled API Specs Pull Request
on:
schedule:
- cron: '0 */12 * * *'

jobs:
api-specs-pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.19
- run: |
git config --global user.name "algolia-ci"
git config --global user.email "[email protected]"
- run: make api-specs-pr
- name: Install Go-task
uses: arduino/setup-task@v2
with:
version: v3.x
repo-token: ${{ secrets.GH_TOKEN }}
- name: Update Search API spec
run: task api-specs-pr
env:
GH_TOKEN: ${{ secrets.GH_SECRET }}
GH_TOKEN: ${{ secrets.GH_SECRET }}
9 changes: 2 additions & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
name: Go

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.19

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
30 changes: 5 additions & 25 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,11 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: 1.18
- uses: actions/checkout@v3
go-version: 1.19
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.47.3

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
version: v1.63.4
16 changes: 6 additions & 10 deletions .github/workflows/releases.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
name: goreleaser

on:
push:
tags:
- "v*"

permissions:
contents: write # publishing releases

contents: write # publishing releases
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Code checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.19
- name: Install chocolatey
Expand All @@ -30,15 +27,15 @@ jobs:
env:
CHOCOLATEY_VERSION: 1.2.0
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.8.0
uses: goreleaser/goreleaser-action@v6
with:
version: "~1.13.1"
version: "~> v2"
args: release
env:
GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
CHOCOLATEY_API_KEY: ${{ secrets.CHOCOLATEY_API_KEY }}
- name: Docs checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: algolia/doc
path: docs
Expand All @@ -53,4 +50,3 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
run: |
make docs-pr
make VARIATON=new docs-pr
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
vendor/

# local build
algolia
algolia
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters:
enable:
- gosec
- gofumpt
- stylecheck
17 changes: 13 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Build the binary
FROM golang:1.23-alpine AS builder
WORKDIR /app
COPY . .
ARG VERSION=docker
RUN apk update && apk add --no-cache curl
RUN go mod download
RUN go install github.com/go-task/task/v3/cmd/task@latest
RUN task download-spec-file && VERSION=${VERSION} task build

FROM alpine
RUN apk update && apk upgrade && \
apk add --no-cache ca-certificates
COPY algolia /bin/algolia
ENTRYPOINT ["/bin/algolia"]
RUN apk update && apk add --no-cache ca-certificates
COPY --from=builder /app/algolia /bin/algolia
ENTRYPOINT ["/bin/algolia"]
113 changes: 113 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
version: 3
output: prefixed
vars:
# The URL to the "old" Algolia docs repo
docs_remote: https://github.com/algolia/doc.git
# The temporary folder for the generated YML files
docs_local: docs
cli_ref_path: app_data/cli/commands
yml_folder: tmp
tasks:
build:
desc: Build the binary
deps: [generate]
cmd: go build -ldflags "-s -w -X=github.com/algolia/cli/pkg/version.Version={{ .VERSION }}" -o algolia cmd/algolia/main.go
vars:
VERSION: '{{ .VERSION | default "main" }}'
test:
desc: Run unit tests
run: always
cmd: go test ./...
lint:
desc: Lint code
cmd: golangci-lint run
format:
desc: Format code
cmds:
- gofumpt -w pkg cmd test internal api
- golines -w pkg cmd test internal api
ci:
desc: Test, lint, and format
aliases:
- default
deps:
- build
- test
- lint
api-specs-pr:
desc: Update the flags for search and settings from the latest Search API spec
summary: |
This task downloads the latest Search API OpenAPI spec from the api-clients-automation repo,
generates the flags, and makes a new PR to the CLI GitHub repo.
deps: [download-spec-file, generate]
preconditions:
- git status --porcelain
cmds:
- |
original="$(git branch --show-current)"
git checkout -B {{ .branch }}
git add .
git commit --message "chore: update search api spec"
git push --force --set-upstream origin {{ .branch }}
gh pr list --base main --head {{ .branch }} | grep -q . || gh pr create --title '{{ .pr-title }}' --description '{{ .pr-description }}'
git switch "${original}"
vars:
branch: feat/api-specs
pr-title: "chore: Update Search API spec"
pr-description: "Update Search API spec"
env:
GIT_COMMITTER_NAME: algolia-ci
GIT_AUTHOR_NAME: algolia-ci
GIT_COMMITTER_EMAIL: [email protected]
GIT_AUTHOR_EMAIL: [email protected]
download-spec-file:
desc: Download the latest Search API spec from GitHub
cmd: curl -fsSL -o {{ .destination }} {{ .source }}
vars:
source: https://raw.githubusercontent.com/algolia/api-clients-automation/main/specs/bundled/search.yml
destination: ./api/specs/search.yml
generate:
desc: Generate command flags
internal: true
cmds:
- go generate ./...
update-docs:
desc: Update the CLI command reference in the Algolia docs
deps:
- clone-docs
- generate-command-reference
cmds:
- task: update-command-reference
- task: cleanup
clone-docs:
desc: Clone the Algolia docs
internal: true
cmd: git clone --depth=1 {{ .docs_remote }} {{ .docs_local }}
generate-command-reference:
desc: Generate updated YML files for the CLI command reference
internal: true
cmd: go run ./cmd/docs --app_data-path {{ .yml_folder }}
update-command-reference:
desc: Add the updated YML files to the docs
summary: |
This task clones the Algolia docs repo,
adds the updated CLI reference yml files to it,
and pushes a new PR to the GitHub repo.
internal: true
cmds:
- |
git -C {{ .docs_local }} checkout -B chore/cli-$(git rev-parse --short HEAD)
git -C {{ .docs_local }} rm "{{ .cli_ref_path }}/*.yml"
mkdir -p {{ .docs_local }}/{{ .cli_ref_path }}
mv {{ .yml_folder }}/*.yml {{ .docs_local }}/{{ .cli_ref_path }}/
git -C {{ .docs_local }} add "{{ .cli_ref_path }}/*.yml"
git -C {{ .docs_local }} commit --message 'chore: Update CLI command reference'
env:
GIT_COMMITTER_NAME: algolia-ci
GIT_AUTHOR_NAME: algolia-ci
GIT_COMMITTER_EMAIL: [email protected]
GIT_AUTHOR_EMAIL: [email protected]
cleanup:
desc: Cleanup the docs files
internal: true
cmd: rm -rf {{ .docs_local }} {{ .yml_folder }} || true
23 changes: 18 additions & 5 deletions api/crawler/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
)

Expand Down Expand Up @@ -43,7 +42,13 @@ func NewClientWithHTTPClient(userID, apiKey string, client *http.Client) *Client

// Request sends an HTTP request and returns an HTTP response.
// It unmarshals the response body to the given interface.
func (c *Client) request(res interface{}, method string, path string, body interface{}, urlParams map[string]string) error {
func (c *Client) request(
res interface{},
method string,
path string,
body interface{},
urlParams map[string]string,
) error {
r, err := c.buildRequest(method, path, body, urlParams)
if err != nil {
return err
Expand Down Expand Up @@ -99,14 +104,18 @@ func (c *Client) buildRequestWithBody(method, url string, body interface{}) (*ht
return nil, err
}

r = ioutil.NopCloser(bytes.NewReader(b))
r = io.NopCloser(bytes.NewReader(b))
}

return http.NewRequest(method, url, r)
}

// buildRequest builds an HTTP request.
func (c *Client) buildRequest(method, path string, body interface{}, urlParams map[string]string) (req *http.Request, err error) {
func (c *Client) buildRequest(
method, path string,
body interface{},
urlParams map[string]string,
) (req *http.Request, err error) {
url := DefaultBaseURL + path

if body == nil {
Expand Down Expand Up @@ -275,7 +284,11 @@ func (c *Client) Stats(crawlerID string) (*StatsResponse, error) {

// CrawlURLs crawls the specified URLs on the specified Crawler.
// It returns the Task ID if successful.
func (c *Client) CrawlURLs(crawlerID string, URLs []string, save, saveSpecified bool) (string, error) {
func (c *Client) CrawlURLs(
crawlerID string,
URLs []string,
save, saveSpecified bool,
) (string, error) {
var res TaskIDResponse
path := fmt.Sprintf("crawlers/%s/urls/crawl", crawlerID)

Expand Down
6 changes: 3 additions & 3 deletions api/crawler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package crawler
import (
"time"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)

// ErrResponse is a Crawler API error response.
Expand Down Expand Up @@ -67,8 +67,8 @@ type Config struct {
IgnoreNoFollowTo bool `json:"ignoreNoFollowTo,omitempty"`
IgnoreCanonicalTo bool `json:"ignoreCanonicalTo,omitempty"`

SaveBackup bool `json:"saveBackup,omitempty"`
InitialIndexSettings map[string]*search.Settings `json:"initialIndexSettings,omitempty"`
SaveBackup bool `json:"saveBackup,omitempty"`
InitialIndexSettings map[string]*search.IndexSettings `json:"initialIndexSettings,omitempty"`

Actions []*Action `json:"actions,omitempty"`
}
Expand Down
Loading
Loading