Skip to content

Commit

Permalink
Merge pull request #253 from tigrisdata/main
Browse files Browse the repository at this point in the history
Beta Release
  • Loading branch information
efirs committed May 12, 2023
2 parents 6dae2d3 + a0f0d8b commit e51b605
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
run:
timeout: 3m
linters:
enable-all: true
disable:
Expand Down
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ builds:
- amd64
- arm64
binary: tigris
flags:
- -tags=release,tigris_grpc,tigris_http
ldflags:
- -w -extldflags '-static'
- -X 'github.com/tigrisdata/tigris-cli/util.Version={{.Version}}'
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ${BIN}: ${GO_SRC} go.sum
CGO_ENABLED=0 go build ${BUILD_PARAM} .

lint:
golangci-lint run --timeout=3m --fix
golangci-lint run --fix
shellcheck tests/*.sh
cd pkg/npm && TIGRIS_SKIP_VERIFY=1 npm i; npx eslint install.js
git checkout pkg/npm/bin/tigris # lints above overwrites placeholder with real binary
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

[![Build Status](https://github.com/tigrisdata/tigrisdb/workflows/go-lint/badge.svg)]()
[![Build Status](https://github.com/tigrisdata/tigrisdb/workflows/go-test/badge.svg)]()
[![Go Report Card](https://goreportcard.com/badge/github.com/tigrisdata/tigris-cli)](https://goreportcard.com/report/github.com/tigrisdata/tigris-cli)

[![NPM](https://img.shields.io/npm/v/@tigrisdata/tigris-cli)](https://www.npmjs.com/package/@tigrisdata/tigris-cli)
[![tigris](https://snapcraft.io/tigris/badge.svg)](https://snapcraft.io/tigris)

Create databases and collections, read and write data, perform transactions,
stream events and setup Tigris locally, all from the command line.

# Documentation

- [Quickstart](https://docs.tigrisdata.com/quickstart/with-cli)
- [Working Locally](https://docs.tigrisdata.com/cli/working-locally)
- [Command Reference](https://docs.tigrisdata.com/cli)

Expand Down Expand Up @@ -72,6 +75,7 @@ Available Commands:
help Help about any command
import Import documents into collection
insert Inserts document(s)
invitation Invitation management commands
list Lists projects, collections or namespaces
login Authenticate on the Tigris instance
logout Logout from Tigris instance
Expand Down
10 changes: 5 additions & 5 deletions cmd/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var createCollectionCmd = &cobra.Command{
Long: "Creates collections with provided schema.",
Example: fmt.Sprintf(`
# Pass the schema as a string
%[1]s create collection --project=testdb '{
%[1]s create collection --project=myproj '{
"title": "users",
"description": "Collection of documents with details of users",
"properties": {
Expand Down Expand Up @@ -150,11 +150,11 @@ var createCollectionCmd = &cobra.Command{
# "id"
# ]
# }
%[1]s create collection --project=testdb </home/alice/users.json
%[1]s create collection --project=myproj </home/alice/users.json
# Create collection with schema passed through stdin
cat /home/alice/users.json | %[1]s create collection testdb -
%[1]s describe collection --project=testdb users | jq .schema | %[1]s create collection testdb -
cat /home/alice/users.json | %[1]s create collection myproj -
%[1]s describe collection --project=myproj users | jq .schema | %[1]s create collection myproj -
`, rootCmd.Root().Name()),
Run: func(cmd *cobra.Command, args []string) {
login.Ensure(cmd.Context(), func(ctx context.Context) error {
Expand Down Expand Up @@ -201,7 +201,7 @@ var alterCollectionCmd = &cobra.Command{
Long: "Updates collection schema.",
Example: fmt.Sprintf(`
# Pass the schema as a string
%[1]s alter collection --project=testdb '{
%[1]s alter collection --project=myproj '{
"title": "users",
"description": "Collection of documents with details of users",
"properties": {
Expand Down
4 changes: 2 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ var deleteCmd = &cobra.Command{
Long: "Deletes documents according to the provided filter.",
Example: fmt.Sprintf(`
# Delete a user where the value of the id field is 2
%[1]s delete --project=testdb users '{"id": 2}'
%[1]s delete --project=myproj users '{"id": 2}'
# Delete users where the value of id field is 1 or 3
%[1]s delete --project=testdb users '{"$or": [{"id": 1}, {"id": 3}]}'
%[1]s delete --project=myproj users '{"$or": [{"id": 1}, {"id": 3}]}'
`, rootCmd.Root().Name()),
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
22 changes: 21 additions & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package cmd

import (
"fmt"
"path"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/tigrisdata/tigris-cli/util"
Expand All @@ -25,7 +29,23 @@ var docsCmd = &cobra.Command{
Short: "Generates CLI documentation in Markdown format",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := doc.GenMarkdownTree(rootCmd, args[0])
err := doc.GenMarkdownTreeCustom(rootCmd, args[0], func(name string) string {
name = path.Base(name)
name = strings.TrimSuffix(name, path.Ext(name))
name = strings.TrimPrefix(name, "tigris_")
name = strings.ReplaceAll(name, "_", "-")
title := strings.ReplaceAll(name, "-", " ")
title = strings.Title(title) //nolint:staticcheck

return fmt.Sprintf(`---
id: %s
title: %s
slug: /sdkstools/cli/%s
---
`, name, title, name)
}, func(link string) string {
return link
})
util.Fatal(err, "generating Markdown documentation")
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Automatically:
* Evolve the schema as soon as it's backward compatible
`,
Example: fmt.Sprintf(`
%[1]s import --project=testdb users --primary-key=id \
%[1]s import --project=myproj users --primary-key=id \
'[
{"id": 20, "name": "Jania McGrory"},
{"id": 21, "name": "Bunny Instone"}
Expand Down
6 changes: 3 additions & 3 deletions cmd/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ var insertCmd = &cobra.Command{
Long: "Inserts one or more documents into a collection.",
Example: fmt.Sprintf(`
# Insert a single document into the users collection
%[1]s insert --project=testdb users '{"id": 1, "name": "Alice Alan"}'
%[1]s insert --project=myproj users '{"id": 1, "name": "Alice Alan"}'
# Insert multiple documents into the users collection
%[1]s insert --project=testdb users \
%[1]s insert --project=myproj users \
'[
{"id": 20, "name": "Jania McGrory"},
{"id": 21, "name": "Bunny Instone"}
Expand All @@ -49,7 +49,7 @@ var insertCmd = &cobra.Command{
# {"id": 20, "name": "Jania McGrory"},
# {"id": 21, "name": "Bunny Instone"}
# ]
cat /home/alice/user_records.json | %[1]s insert --project=testdb users -
cat /home/alice/user_records.json | %[1]s insert --project=myproj users -
`, rootCmd.Root().Name()),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ all the fields of the documents are selected.`,
# Read a user document where id is 20
# The output would be
# {"id": 20, "name": "Jania McGrory"}
%[1]s read --project=testdb users '{"id": 20}'
%[1]s read --project=myproj users '{"id": 20}'
# Read user documents where id is 2 or 4
# The output would be
# {"id": 2, "name": "Alice Wong"}
# {"id": 4, "name": "Jigar Joshi"}
%[1]s read --project=testdb users '{"$or": [{"id": 2}, {"id": 4}]}'
%[1]s read --project=myproj users '{"$or": [{"id": 2}, {"id": 4}]}'
# Read all documents in the user collection
# The output would be
# {"id": 2, "name": "Alice Wong"}
# {"id": 4, "name": "Jigar Joshi"}
# {"id": 20, "name": "Jania McGrory"}
# {"id": 21, "name": "Bunny Instone"}
%[1]s read --project=testdb users
%[1]s read --project=myproj users
`, rootCmd.Root().Name()),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ var replaceCmd = &cobra.Command{
Long: "Inserts new documents or replaces existing documents.",
Example: fmt.Sprintf(`
# Insert new documents
%[1]s replace --project=testdb users '{"id": 1, "name": "John Wong"}'
%[1]s replace --project=myproj users '{"id": 1, "name": "John Wong"}'
# Replace existing document
# Existing document with the following data will get replaced
# {"id": 20, "name": "Jania McGrory"}
%[1]s replace --project=testdb users '{"id": 20, "name": "Alice Alan"}'
%[1]s replace --project=myproj users '{"id": 20, "name": "Alice Alan"}'
# Insert or replace multiple documents
# Existing document with the following data will get replaced
# {"id": 20, "name": "Alice Alan"}
# {"id": 21, "name": "Bunny Instone"}
# While the new document {"id": 19, "name": "New User"} will get inserted
%[1]s replace --project=testdb users \
%[1]s replace --project=myproj users \
'[
{"id": 19, "name": "New User"},
{"id": 20, "name": "Replaced Alice Alan"},
Expand Down
2 changes: 1 addition & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var dbSearchCmd = &cobra.Command{
# Find users with last name exactly matching "Wong"
%[1]s %[2]s --filter '{"lastName": "Wong"}'
`, rootCmd.Root().Name(), "search --project=testdb users"),
`, rootCmd.Root().Name(), "search --project=myproj users"),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
login.Ensure(cmd.Context(), func(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/search/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var importCmd = &cobra.Command{
Input is a stream or array of JSON documents to import.
`,
Example: fmt.Sprintf(`
%[1]s search import --project=testdb users --create-index \
%[1]s search import --project=myproj users --create-index \
'[
{"id": 20, "name": "Jania McGrory"},
{"id": 21, "name": "Bunny Instone"}
Expand Down
8 changes: 4 additions & 4 deletions cmd/search/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ var createIndexCmd = &cobra.Command{
Long: "Creates indexes with provided schema.",
Example: fmt.Sprintf(`
# Pass the schema as a string
%[1]s create index --project=testdb '{
%[1]s create index --project=myproj '{
"title": "users",
"description": "Index of documents with details of users",
"properties": {
Expand Down Expand Up @@ -140,11 +140,11 @@ var createIndexCmd = &cobra.Command{
# }
# }
# }
%[1]s create index --project=testdb </home/alice/users.json
%[1]s create index --project=myproj </home/alice/users.json
# Create index with schema passed through stdin
cat /home/alice/users.json | %[1]s create index testdb -
%[1]s describe index --project=testdb users | jq .schema | %[1]s create index testdb -
cat /home/alice/users.json | %[1]s create index myproj -
%[1]s describe index --project=myproj users | jq .schema | %[1]s create index myproj -
`, "tigris search"),
Run: func(cmd *cobra.Command, args []string) {
login.Ensure(cmd.Context(), func(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/transact.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var transactCmd = &cobra.Command{
All the read, write and schema operations are supported.`,
Example: fmt.Sprintf(`
# Perform a transaction that inserts and updates in three collections
%[1]s tigris transact testdb \
%[1]s tigris transact myproj \
'[
{
"insert": {
Expand Down
2 changes: 1 addition & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var updateCmd = &cobra.Command{
Long: "Updates the field values in documents according to provided filter.",
Example: fmt.Sprintf(`
# Update the field "name" of user where the value of the id field is 2
%[1]s update --project=testdb users '{"id": 19}' '{"$set": {"name": "Updated New User"}}'
%[1]s update --project=myproj users '{"id": 19}' '{"$set": {"name": "Updated New User"}}'
`, rootCmd.Root().Name()),
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
6 changes: 3 additions & 3 deletions login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ var (
instances = map[string]instance{
"api.dev.tigrisdata.cloud": {
clientID: "zXKltgV3JhGwUqOCUWNmtU7aX5TytKGx",
authHost: "https://tigrisdata-dev.us.auth0.com/",
authHost: "https://login-dev.tigrisdata.cloud/",
audience: "https://tigris-api-dev",
},
"api.perf.tigrisdata.cloud": {
clientID: "zXKltgV3JhGwUqOCUWNmtU7aX5TytKGx",
authHost: "https://tigrisdata-dev.us.auth0.com/",
authHost: "https://login-dev.tigrisdata.cloud/",
audience: "https://tigris-api-perf",
},
"api.preview.tigrisdata.cloud": {
clientID: "GS8PrHA1aYblUR73yitqomc40ZYZ81jF",
authHost: "https://tigrisdata.us.auth0.com/",
authHost: "https://auth.tigrisdata.cloud/",
audience: "https://tigris-api-preview",
},
}
Expand Down
67 changes: 45 additions & 22 deletions pkg/npm/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

/* eslint-disable no-console */

const fetch = require('node-fetch'); const path = require('path');
const tar = require('tar'); const mkdirp = require('mkdirp');
const fs = require('fs'); const { execSync } = require('child_process');
const fetch = require('node-fetch');
const path = require('path');
const tar = require('tar');
const AdmZip = require('adm-zip'); // windows unzip
const mkdirp = require('mkdirp');
const fs = require('fs');
const { execSync } = require('child_process');
const crypto = require('crypto');
const process = require('node:process');

Expand All @@ -21,6 +25,8 @@ const PLATFORM_MAPPING = {
win32: 'windows',
};

const IS_WINDOWS = process.platform === 'win32';

function getInstallationPath(callback) {
const out = execSync('npm root');

Expand Down Expand Up @@ -132,7 +138,7 @@ function parsePackageJson() {
};

// Binary name on Windows has .exe suffix
if (process.platform === 'win32') {
if (IS_WINDOWS) {
opts.binName += '.exe';
opts.ext = 'zip';
}
Expand Down Expand Up @@ -165,27 +171,44 @@ function install(callback) {
mkdirp.sync(opts.binPath);

console.log(`Downloading from URL: ${opts.url}`);

fetch(opts.url).then((res) => {
const hash = crypto.createHash('sha256').setEncoding('hex');
res.body.pipe(hash).on('end', () => { hash.end(); });
res.body
.pipe(
tar.x(
{
C: opts.binPath,
},
['tigris'],
),
)
.on('end', () => {
verifyAndPlaceBinary(
opts.binName,
opts.binPath,
hash.read(),
opts.checksums,
callback,
);
});

const handleExtractComplete = () => {
verifyAndPlaceBinary(
opts.binName,
opts.binPath,
hash.read(),
opts.checksums,
callback,
);
};

if (IS_WINDOWS) {
// Numerous unzip Node stream supporting solutions were tried.
// However, none seem to unzip the .exe in a usable format.
// Therefore, saving to disk and then using a trusted unzipper
// that doesn't support streams to extract.
const tmpZip = path.join(opts.binPath, 'tigris-tmp.zip');
res.body
.pipe(fs.createWriteStream(tmpZip))
.on('finish', () => {
const zip = new AdmZip(tmpZip);
zip.extractAllTo(opts.binPath, true);
fs.unlinkSync(tmpZip);

handleExtractComplete();
});
} else {
res.body.pipe(tar.x(
{
C: opts.binPath,
},
['tigris'],
)).on('end', handleExtractComplete);
}
});

return null;
Expand Down
1 change: 1 addition & 0 deletions pkg/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
],
"homepage": "https://www.tigrisdata.com",
"dependencies": {
"adm-zip": "^0.5.10",
"mkdirp": "^1.0.4",
"node-fetch": "^2.6.7",
"tar": "^6.1.12"
Expand Down

0 comments on commit e51b605

Please sign in to comment.