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

refactor: CLI client interface #1839

Merged
merged 124 commits into from
Oct 2, 2023

Conversation

nasdf
Copy link
Member

@nasdf nasdf commented Aug 31, 2023

Relevant issue(s)

Closes #1472
Closes #1507
Closes #1860

Description

This is a follow up to #1776

This PR adds a CLI implementation that implements the client.DB interface and runs through the existing integration test suite.

Renamed Commands:

  • peerid to client peer info
  • client p2pcollection to client p2p collection
  • client replicator to client p2p replicator
  • client schema list to client collection describe

Removed Commands:

  • block get
  • ping
  • rpc

Added Commands:

  • client collection create
  • client collection delete
  • client collection get
  • client collection keys
  • client collection update
  • client tx create
  • client tx discard
  • client tx commit
  • client schema migration up
  • client schema migration down
  • client schema migration reload

Notes for reviewers:

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

make test

Specify the platform(s) on which this was tested:

  • MacOS

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm only part way through my review, but I thought I'd submit the comments I have so far. Overall it looks really good, and it is a fantastic amount of code you've been able to remove, whilst gaining features :)

RunE: func(cmd *cobra.Command, args []string) (err error) {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
store := cmd.Context().Value(storeContextKey).(client.Store)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: You do cmd.Context().Value(storeContextKey).(client.Store) quite a lot, it might be worth hiding the const and the cast behind a func (perhaps located next to the func that sets it).

"github.com/sourcenetwork/defradb/config"
)

func MakeBlocksGetCommand(cfg *config.Config) *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: It might be handy for reviewers (and yourself) to list the commands that have been totally removed without replacement, such as this one. It would make it easier to spot and then talk about them.

I am pretty happy losing functionality in this PR, but medium-long term we may want some of them back via the client interfaces.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a list of the new, renamed, and removed commands to the description.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers Keenan!

"github.com/sourcenetwork/defradb/client"
)

func MakeP2PReplicatorGetallCommand() *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: The a in all should really be capitalised.

@@ -0,0 +1,36 @@
// Copyright 2022 Democratized Data Foundation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: I hate that we have to do this, and am personally happy with being a bit sloppy here, but I know that others are not - this (and any others) should be 2023.

)

func MakeP2PCollectionGetallCommand(cfg *config.Config) *cobra.Command {
func MakeP2PCollectionGetallCommand() *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: The a in all should really be capitalised.

var collection string
var file string
var cmd = &cobra.Command{
Use: "create --collection <collection> <document>",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: <collection> is ambiguous here and it is not clear (outside of Long) whether this means collection ID, collection name, or a collection json blob. Suggest changing to <collectionName>, if this behaviour is kept (read other comments RE structure first maybe).

Long: `Create a new document.

Example: create document
defradb client document create --collection User '{ "name": "Bob" }'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Here the cli now differs in structure to the client API. Here document is a sibling to collection, despite internally using collection commands.

I think it would reduce the cognitive load on the user if all clients followed the same structure, and I thought this was one of the goals of this block of work.

You are also limiting the user to the default schema version here, if you fix the structure you should get more complex collection selection for free. This also is breaking from the client API.

It might be worth creating a discord thread to discuss this, will leave that up to you though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reorganized this so it better matches the client API.

return err
}
return col.Create(cmd.Context(), doc)
case []any:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: This is quite a lot of code not covered by tests, and the PR description says your only testing was via the make test command, is the PR description incorrect, or have you manually tested these blocks?

suggestion: Write some tests :)

The same question/suggestion applies to quite a few commands in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding more integration tests would be the best option. I will put together a list of areas that are missing coverage.

}
return col.CreateMany(cmd.Context(), docs)
default:
return fmt.Errorf("invalid document")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: This, and any other error string should be in an errors.go file. Happy to expand on why I think so if you need (I cant remember how much you know about this 'standard' :)).

cli/wrapper.go Outdated

var _ client.DB = (*Wrapper)(nil)

type Wrapper struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why is this production code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to move this and the http wrapper to the integration tests package. I think there was something blocking me from doing that, but I will give it another look.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers Keenan - please give me a shout here once you've pushed changes/decided against moving and I'll review them fully.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I was able to move them to a new tests/clients package.

if docKey.Err != nil {
results.Error = docKey.Err.Error()
}
writeJSON(cmd, results) //nolint:errcheck
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Please document why this is okay

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to return the error here instead.

Short: "Delete a replicator. It will stop synchronizing",
Long: `Delete a replicator. It will stop synchronizing.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: This entire command appears to be untested, please test it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like for this to be covered in an integration test so we can get coverage on other implementations as well. I'll follow up shortly with a fix.

func MakeP2PReplicatorSetCommand() *cobra.Command {
var collections []string
var cmd = &cobra.Command{
Use: "set [-c, --collection] <peer>",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: [-c, --collection] appears to be inconsistent with the rest of the CLI, I think it should be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to the naming or that it's a slice type?

Copy link
Contributor

@AndrewSisley AndrewSisley Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot that this was a slice type, but I was referring to the existence of the shorthand. However the way you have moved the document stuff into collection has removed the inconsistency (as there is no longer a --collection flag there).

This comment is now no longer relevant :)

"github.com/sourcenetwork/defradb/errors"
)

func MakePeerIDCommand(cfg *config.Config) *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Is this now provided by p2p_info?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as a follow up I will be adding the multiaddresses as a return value there as well.

var schemaVersionID string
var cmd = &cobra.Command{
Use: "down --version <version> <src>",
Short: "Reverse a migration on the specified schema version.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Please explain to the user what <src> is :)

suggestion: on is somewhat ambiguous here I think, from (or to, I cant remember which way round) might be better.

Same goes for the up command.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to describe it in more detail. I believe it is the list of documents to apply / reverse the migration to / from.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cheers Keenan - looks good

Use: "collection [--name <name> --schema <schemaID> --version <versionID>]",
Short: "Interact with a collection.",
Long: `Create, read, update, and delete documents within a collection.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
Copy link
Contributor

@AndrewSisley AndrewSisley Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet :) I was hoping you'd be able to find a way to do it like this when I was commenting earlier - forces consistency and we get all the features :)

`,
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
col, ok := cmd.Context().Value(colContextKey).(client.Collection)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Similar to store, I'd suggest refactoring out cmd.Context().Value(colContextKey).(client.Collection) into a private func, located next to where it is set (collection.go).

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and looking forward to having it in :) Would suggest a quick manual sanity check of a couple of CLI commands before merge, just in case :)

Good job Keenan :)

Copy link
Member

@shahzadlone shahzadlone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers! Just posted one small nitpick.

We can coordinate merging this as would need to turn off the required checks right before merge.

@nasdf nasdf merged commit 4c3df48 into sourcenetwork:develop Oct 2, 2023
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
## Relevant issue(s)

## Description

This PR is split into two to make reviewing easier: sourcenetwork#1839

This PR adds an HTTP client and server implementation that implements
the `client.DB` interface and runs through the existing integration test
suite.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

`make test`

Specify the platform(s) on which this was tested:
- MacOS
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
## Relevant issue(s)

Closes sourcenetwork#1472
Closes sourcenetwork#1507
Closes sourcenetwork#1860 

## Description

This is a follow up to sourcenetwork#1776

This PR adds a CLI implementation that implements the client.DB
interface and runs through the existing integration test suite.

- [x] Merge existing server config code
- [x] Refactor CLI to use new HTTP client
- [x] Remove `net/api` package
- [x] Remove `api/http` package
- [x] Lens tests are timing out in CI: fixed sourcenetwork#1862
- [x] Code coverage is incorrectly reporting: fixed sourcenetwork#1861
- [x] Flaky test causing failures: fixed sourcenetwork#1912

Renamed Commands:
- `peerid` to `client peer info`
- `client p2pcollection` to `client p2p collection`
- `client replicator` to `client p2p replicator`
- `client schema list` to `client collection describe`

Removed Commands:
- `block get`
- `ping`
- `rpc`

Added Commands:
- `client collection create`
- `client collection delete`
- `client collection get`
- `client collection keys`
- `client collection update`
- `client tx create`
- `client tx discard`
- `client tx commit`
- `client schema migration up`
- `client schema migration down`
- `client schema migration reload`

**Notes for reviewers**:
- `.github` changes are merged from sourcenetwork#1871
- `Makefile` most of these changes are also from sourcenetwork#1871
- `docs/cli` ignore these changes, it will be updated next release
- sorry for all of the merge commits, I am working on learning rebase
flow

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

`make test`

Specify the platform(s) on which this was tested:
- MacOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api Related to the external API component area/cli Related to the CLI binary area/testing Related to any test or testing suite
Projects
None yet
3 participants