Skip to content

Commit

Permalink
dbid => namespace, remove nep413 authr (#1292)
Browse files Browse the repository at this point in the history
* dbid => namespace, remove nep413 authr

* taskfile

* missing golang.org/x/term require

* kwil-cli
  • Loading branch information
jchappelow authored Jan 27, 2025
1 parent c440943 commit 1adedce
Show file tree
Hide file tree
Showing 35 changed files with 134 additions and 570 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ linters:
- durationcheck
- canonicalheader
- copyloopvar
- exptostd
- fatcontext
- gocheckcompilerdirectives
- gochecksumtype
Expand Down
8 changes: 5 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ tasks:

tools:
aliases: [install:deps]
deps:
- task: linter
desc: Install tools required to build this app
cmds:
- go install "golang.org/x/tools/cmd/goimports@v0.27.0"
- go install "golang.org/x/tools/cmd/goimports@v0.29.0"

fmt:
desc: Format the code
Expand All @@ -36,7 +38,7 @@ tasks:
- |
(cd core; go mod tidy)
go mod tidy
# (cd test; go mod tidy)
(cd test; go mod tidy)
# (cd core/client/example; go mod tidy)
# (cd core/gatewayclient/example; go mod tidy)

Expand All @@ -49,7 +51,7 @@ tasks:
linter:
desc: Install the linter # not for CI which has an action for this
cmds:
- go install "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.2"
- go install "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.63.4"

build:
desc: Build kwil-cli and kwild
Expand Down
4 changes: 2 additions & 2 deletions app/shared/display/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func getExampleTxQueryResponse() *types.TxQueryResponse {
}

rawPayload := types.ActionExecution{
DBID: "xf617af1ca774ebbd6d23e8fe12c56d41d25a22d81e88f67c6c6ee0d4",
Action: "create_user",
Namespace: "xf617af1ca774ebbd6d23e8fe12c56d41d25a22d81e88f67c6c6ee0d4",
Action: "create_user",
Arguments: [][]*types.EncodedValue{
{
{
Expand Down
24 changes: 11 additions & 13 deletions cmd/kwil-cli/cmds/database/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,23 @@ var (
var (
batchLong = `Batch executes an action or procedure on a database using inputs from a CSV file.
To map a CSV column name to a procedure input, use the ` + "`" + `--map-inputs` + "`" + ` flag.
The format is ` + "`" + `--map-inputs "<csv_column_1>:<procedure_input_1>,<csv_column_2>:<procedure_input_2>"` + "`" + `. If the ` + "`" + `--map-inputs` + "`" + ` flag is not passed,
the CSV column name will be used as the procedure input name.
To map a CSV column name to a procedure input, use the ` + "`--map-inputs`" + ` flag.
The format is ` + "`--map-inputs <csv_column_1>:<procedure_input_1>,<csv_column_2>:<procedure_input_2>`." +
` If the ` + "`--map-inputs`" + ` flag is not passed, the CSV column name will be used as the procedure input name.
You can also specify the input values directly using the ` + "`" + `--values` + "`" + ` flag, delimited by a colon.
These values will apply to all inserted rows, and will override the CSV column mappings.
You can either specify the database to execute this against with the ` + "`" + `--name` + "`" + ` and ` + "`" + `--owner` + "`" + `
flags, or you can specify the database by passing the database id with the ` + "`" + `--dbid` + "`" + ` flag. If a ` + "`" + `--name` + "`" + `
flag is passed and no ` + "`" + `--owner` + "`" + ` flag is passed, the owner will be inferred from your configured wallet.`
You can specify the namespace with the ` + "`--namespace`" + ` flag.`

batchExample = `# Given a CSV file with the following contents:
# id,name,age
# 1,john,25
# 2,jane,30
# 3,jack,35
# Executing the ` + "`" + `create_user($user_id, $username, $user_age, $created_at)` + "`" + ` action on the "mydb" database
kwil-cli database batch create_user --path ./users.csv --name mydb --owner 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64 --map-inputs "id:user_id,name:username,age:user_age" --values created_at:$(date +%s)`
# Executing the ` + "`" + `create_user($user_id, $username, $user_age, $created_at)` + "`" + ` action in the "mydb" database namespace
kwil-cli database batch create_user --path ./users.csv --namespace mydb --map-inputs "id:user_id,name:username,age:user_age" --values created_at:$(date +%s)`
)

// batch is used for batch operations on databases
Expand All @@ -59,9 +57,9 @@ func batchCmd() *cobra.Command {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return client.DialClient(cmd.Context(), cmd, 0, func(ctx context.Context, cl clientType.Client, conf *config.KwilCliConfig) error {
dbid, _, err := getSelectedNamespace(cmd)
namespace, _, err := getSelectedNamespace(cmd)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting selected dbid from CLI flags: %w", err))
return display.PrintErr(cmd, fmt.Errorf("error getting selected namespace from CLI flags: %w", err))
}

action, _, err := getSelectedAction(cmd, args)
Expand All @@ -88,12 +86,12 @@ func batchCmd() *cobra.Command {
return display.PrintErr(cmd, fmt.Errorf("error building inputs: %w", err))
}

tuples, err := buildExecutionInputs(ctx, cl, dbid, action, inputs)
tuples, err := buildExecutionInputs(ctx, cl, namespace, action, inputs)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error creating action inputs: %w", err))
}

txHash, err := cl.Execute(ctx, dbid, strings.ToLower(action), tuples,
txHash, err := cl.Execute(ctx, namespace, strings.ToLower(action), tuples,
clientType.WithNonce(nonceOverride), clientType.WithSyncBroadcast(syncBcast))
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error executing action: %w", err))
Expand All @@ -112,7 +110,7 @@ func batchCmd() *cobra.Command {
},
}

bindFlagsTargetingAction(cmd)
bindFlagsTargetingAction(cmd) // --namespace/-n , --action/-a
cmd.Flags().StringSliceVarP(&csvColumnMappings, "map-inputs", "m", []string{}, "csv column to action parameter mappings (e.g. csv_id:user_id, csv_name:user_name)")
cmd.Flags().StringSliceVarP(&inputValueMappings, "values", "v", []string{}, "action parameter mappings applied to all executions (e.g. id:123, name:john)")
cmd.Flags().StringVarP(&filePath, "path", "p", "", "path to the CSV file to use")
Expand Down
36 changes: 17 additions & 19 deletions cmd/kwil-cli/cmds/database/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@ import (
)

var (
callLong = `Call a ` + "`" + `view` + "`" + ` procedure or action, returning the result.
callLong = `Call a ` + "`view`" + ` procedure or action, returning the result.
` + "`" + `view` + "`" + ` procedure are read-only procedure that do not require gas to execute. They are
the primary way to query the state of a database. The ` + "`" + `call` + "`" + ` command is used to call
a ` + "`" + `view` + "`" + ` procedure on a database. It takes the procedure name as the first positional
` + "`view`" + ` procedure are read-only procedure that do not require gas to execute. They are
the primary way to query the state of a database. The ` + "`call`" + ` command is used to call
a ` + "`view`" + ` procedure on a database. It takes the procedure name as the first positional
argument, and the procedure inputs as all subsequent arguments.
To specify a procedure input, you first need to specify the input name, then the input value, delimited by a colon.
For example, for procedure ` + "`" + `get_user($username)` + "`" + `, you would specify the procedure as follows:
For example, for procedure ` + "`get_user($username)`" + `, you would specify the procedure as follows:
` + "`" + `call get_user username:satoshi` + "`" + `
` + "`call get_user username:satoshi`" + `
You can either specify the database to execute this against with the ` + "`" + `--name` + "`" + ` and ` + "`" + `--owner` + "`" + `
flags, or you can specify the database by passing the database id with the ` + "`" + `--dbid` + "`" + ` flag. If a ` + "`" + `--name` + "`" + `
flag is passed and no ` + "`" + `--owner` + "`" + ` flag is passed, the owner will be inferred from your configured wallet.
You specify the database namespace in which to execute this with the ` + "`--namespace` flag. " + `
If you are interacting with a Kwil gateway, you can also pass the ` + "`" + `--authenticate` + "`" + ` flag to authenticate the call with your private key.`
If you are interacting with a Kwil gateway, you can also pass the ` + "`--authenticate`" + ` flag to authenticate the call with your private key.`

callExample = `# Calling the ` + "`" + `get_user($username)` + "`" + ` procedure on the "mydb" database
kwil-cli database call get_user --name mydb --owner 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64 username:satoshi
callExample = `# Calling the ` + "`get_user($username)`" + ` procedure on the "somedb" namespace
kwil-cli database call get_user --namespace somedb username:satoshi
# Calling the ` + "`" + `get_user($username)` + "`" + ` procedure on a database using a dbid, authenticating with a private key
kwil-cli database call get_user --dbid 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64 username:satoshi --authenticate`
# Calling the ` + "`get_user($username)`" + ` procedure on a database using a namespace, authenticating with a private key
kwil-cli database call get_user --namespace somedb username:satoshi --authenticate`
)

func callCmd() *cobra.Command {
Expand All @@ -63,9 +61,9 @@ func callCmd() *cobra.Command {
}

return client.DialClient(cmd.Context(), cmd, dialFlags, func(ctx context.Context, clnt clientType.Client, conf *config.KwilCliConfig) error {
dbid, _, err := getSelectedNamespace(cmd)
namespace, _, err := getSelectedNamespace(cmd)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting selected dbid from CLI flags: %w", err))
return display.PrintErr(cmd, fmt.Errorf("error getting selected namespace from CLI flags: %w", err))
}

action, args, err := getSelectedAction(cmd, args)
Expand All @@ -78,7 +76,7 @@ func callCmd() *cobra.Command {
return display.PrintErr(cmd, fmt.Errorf("error getting inputs: %w", err))
}

tuples, err := buildExecutionInputs(ctx, clnt, dbid, action, []map[string]string{inputs})
tuples, err := buildExecutionInputs(ctx, clnt, namespace, action, []map[string]string{inputs})
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error creating action/procedure inputs: %w", err))
}
Expand All @@ -90,7 +88,7 @@ func callCmd() *cobra.Command {
return display.PrintErr(cmd, errors.New("only one set of inputs can be provided to call"))
}

data, err := clnt.Call(ctx, dbid, action, tuples[0])
data, err := clnt.Call(ctx, namespace, action, tuples[0])
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error calling action/procedure: %w", err))
}
Expand All @@ -107,7 +105,7 @@ func callCmd() *cobra.Command {
},
}

bindFlagsTargetingAction(cmd)
bindFlagsTargetingAction(cmd) // --namespace/-n , --action/-a
cmd.Flags().BoolVar(&gwAuth, "authenticate", false, "authenticate signals that the call is being made to a gateway and should be authenticated with the private key")
cmd.Flags().BoolVar(&logs, "logs", false, "result will include logs from notices raised during the call")
return cmd
Expand Down
4 changes: 2 additions & 2 deletions cmd/kwil-cli/cmds/database/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var (
dbCmd = &cobra.Command{
Use: "database",
Aliases: []string{"db"},
Short: "The database command is a parent command containing subcommands for interacting with databases.",
Long: "The database command is a parent command containing subcommands for interacting with databases.",
Short: "The database command is a parent command containing subcommands for interacting with the database.",
Long: "The database command is a parent command containing subcommands for interacting with the database.",
}

nonceOverride int64
Expand Down
8 changes: 4 additions & 4 deletions cmd/kwil-cli/cmds/database/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ statement "SELECT * FROM users WHERE age > 25", you would specify the following:
To specify an action to execute, you can pass the action name as the first positional argument, or as the --action flag.
The action name is specified as the first positional argument, and the action parameters as all subsequent arguments.`

executeExample = `# Executing a CREATE TABLE statement on the "mydb" database
executeExample = `# Executing a CREATE TABLE statement on the "somedb" database namespace
kwil-cli database execute --sql "CREATE TABLE users (id UUID, name TEXT, age INT8);" --namespace mydb
# Executing the ` + "`" + `create_user($username, $age)` + "`" + ` procedure on the "mydb" database
kwil-cli database execute --action create_user username:satoshi age:32 --namespace mydb
# Executing the ` + "`" + `create_user($username, $age)` + "`" + ` procedure on the "somedb" database
kwil-cli database execute --action create_user username:satoshi age:32 --namespace somedb
`
)

Expand Down Expand Up @@ -167,7 +167,7 @@ func executeCmd() *cobra.Command {
},
}

bindFlagsTargetingAction(cmd)
bindFlagsTargetingAction(cmd) // --namespace/-n , --action/-a
cmd.Flags().StringVarP(&sqlStmt, "sql", "s", "", "the SQL statement to execute")
cmd.Flags().StringVarP(&sqlFilepath, "sql-file", "f", "", "the file containing the SQL statement to execute")
return cmd
Expand Down
2 changes: 1 addition & 1 deletion cmd/kwil-cli/cmds/database/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (d *respDBList) MarshalText() ([]byte, error) {
msg.WriteString(fmt.Sprintf("Databases belonging to '%x':\n", d.owner))
}
for i, db := range d.Info {
msg.WriteString(fmt.Sprintf(" DBID: %s\n", db.DBID))
msg.WriteString(fmt.Sprintf(" Namespace: %s\n", db.Namespace))
msg.WriteString(fmt.Sprintf(" Name: %s\n", db.Name))
msg.WriteString(fmt.Sprintf(" Owner: %x", db.Owner))
if i != len(d.Info)-1 {
Expand Down
55 changes: 20 additions & 35 deletions cmd/kwil-cli/cmds/database/message_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package database

import (
"encoding/hex"

"github.com/kwilteam/kwil-db/app/shared/display"
"github.com/kwilteam/kwil-db/core/types"
)

func Example_respDBlist_text_0() {
display.Print(
&respDBList{Info: []*types.DatasetIdentifier{}, owner: mustDecodeHex("6f776e6572")},
&respDBList{Info: []*types.DatasetIdentifier{}, owner: testOwner},
nil, "text")
// Output:
// No databases found for '6f776e6572'.
Expand All @@ -19,57 +17,44 @@ func Example_respDBlist_text() {
display.Print(
&respDBList{Info: []*types.DatasetIdentifier{
{
Name: "db_a",
Owner: mustDecodeHex("6f776e6572"),
DBID: "xabc",
Name: "db_a",
Owner: testOwner,
Namespace: "one",
},
{
Name: "db_b",
Owner: mustDecodeHex("6f776e6572"),
DBID: "xdef",
Name: "db_b",
Owner: testOwner,
Namespace: "two",
},
},
owner: mustDecodeHex("6f776e6572")},
owner: testOwner},
nil, "text")
// Output:
// Databases belonging to '6f776e6572':
// DBID: xabc
// Namespace: one
// Name: db_a
// Owner: 6f776e6572
// DBID: xdef
// Namespace: two
// Name: db_b
// Owner: 6f776e6572
}

func mustDecodeHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}

// func mustDecodeBase64(s string) []byte {
// b, err := base64.StdEncoding.DecodeString(s)
// if err != nil {
// panic(err)
// }
// return b
// }
// hex.DecodeString("6f776e6572")
var testOwner = []byte{0x6f, 0x77, 0x6e, 0x65, 0x72}

func Example_respDBlist_json() {

display.Print(
&respDBList{Info: []*types.DatasetIdentifier{
{
Name: "db_a",
Owner: mustDecodeHex("6f776e6572"),
DBID: "xabc",
Name: "db_a",
Owner: testOwner,
Namespace: "one",
},
{
Name: "db_b",
Owner: mustDecodeHex("6f776e6572"),
DBID: "xdef",
Name: "db_b",
Owner: testOwner,
Namespace: "two",
},
}},
nil, "json")
Expand All @@ -80,12 +65,12 @@ func Example_respDBlist_json() {
// {
// "name": "db_a",
// "owner": "6f776e6572",
// "dbid": "xabc"
// "namespace": "one"
// },
// {
// "name": "db_b",
// "owner": "6f776e6572",
// "dbid": "xdef"
// "namespace": "two"
// }
// ],
// "error": ""
Expand Down
8 changes: 3 additions & 5 deletions cmd/kwil-cli/cmds/database/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ var (
Requires a SQL SELECT statement as an argument.
You can either specify the database to execute this against with the ` + "`" + `--name` + "`" + ` and ` + "`" + `--owner` + "`" + `
flags, or you can specify the database by passing the database id with the ` + "`" + `--dbid` + "`" + ` flag. If a ` + "`" + `--name` + "`" + `
flag is passed and no ` + "`" + `--owner` + "`" + ` flag is passed, the owner will be inferred from your configured wallet.
You specify the database namespace to execute this against with the ` + "`--namespace` flag." + `
Note that ad-hoc queries will be rejected on RPC servers that are operating with
authenticated call requests enabled.`

queryExample = `# Querying the "users" table in the "mydb" database
kwil-cli database query "SELECT * FROM users WHERE age > 25" --name mydb --owner 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64`
queryExample = `# Querying the "users" table in the "somedb" database namespace
kwil-cli database query "SELECT * FROM users WHERE age > 25" --namespace somedb`
)

func queryCmd() *cobra.Command {
Expand Down
8 changes: 4 additions & 4 deletions cmd/kwil-cli/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
"github.com/spf13/cobra"
)

var longDesc = `Command line interface client for using %s.
var longDesc = `Command line interface client for using ` + "`%s`." + `
` + "`" + `%s` + "`" + ` is a command line interface for interacting with %s. It can be used to deploy, update, and query databases.
` + "`%s`" + ` is a command line interface for interacting with %s. It can be used to deploy, update, and query databases.
` + "`" + `%s` + "`" + ` can be configured with a persistent configuration file. This file can be configured with the '%s configure' command.
` + "`" + `%s` + "`" + ` will look for a configuration file at ` + "`" + `$HOME/.kwil-cli/config.json` + "`" + `.`
` + "`%s`" + ` can be configured with a persistent configuration file. This file can be configured with the '%s configure' command.
` + "`%s`" + ` will look for a configuration file at ` + "`$HOME/.kwil-cli/config.json`."

func NewRootCmd() *cobra.Command {
// The basis for ActiveConfig starts with defaults defined in DefaultKwilCliPersistedConfig.
Expand Down
Loading

0 comments on commit 1adedce

Please sign in to comment.