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

Refactoring Commands #196

Closed
wants to merge 142 commits into from
Closed

Refactoring Commands #196

wants to merge 142 commits into from

Conversation

mappum
Copy link
Contributor

@mappum mappum commented Oct 24, 2014

Now that we have the commands package set up, it's time to move everything over to it. This PR is currently not ready for merge, just setting it up for CR.

The basic scope of this PR is as follows:

  • Set up transports based on commands package (HTTP RPC server/client, CLI command handling)
  • Refactor existing commands for the new API (won't be a 1:1 mapping, commands will change)
  • Make changes to commands package when necessary

@btc btc added the status/in-progress In progress label Oct 24, 2014
@mappum
Copy link
Contributor Author

mappum commented Oct 24, 2014

At the current HEAD, the following is done:

  • HTTP RPC server/client (at commands/http)
  • CLI entry point

You can test out the HTTP by doing ipfs daemon, then visiting localhost:8080/api/v0/beep in your browser. While the daemon is running, you can also do ipfs beep and it will send the command to the daemon. For a demonstration of the encoding functionality, visit localhost:8080/api/v0/beep?enc=xml.

@jbenet
Copy link
Member

jbenet commented Oct 24, 2014

@mappum good progress, this cleans up a lot! 👍

@jbenet
Copy link
Member

jbenet commented Oct 24, 2014

I guess for now let's just keep outputting CLI-friendly Help-- we can solve that over time once we generate HTTP API docs.

@jbenet
Copy link
Member

jbenet commented Oct 26, 2014

@mappum heads up -- i rebased over master.

@jbenet jbenet mentioned this pull request Oct 27, 2014
@mappum mappum force-pushed the commands-refactor branch 3 times, most recently from e4bcae3 to ef32b68 Compare October 28, 2014 02:02
@whyrusleeping whyrusleeping added this to the α milestone Oct 28, 2014
@btc
Copy link
Contributor

btc commented Oct 29, 2014

@mappum Is it intended for the calling Command to be available to the Function? For example, the root Ipfs command prints out it's help message. Is the intent for this root cli command to have an empty Run Function and print out the help message some other way?

@mappum
Copy link
Contributor Author

mappum commented Oct 29, 2014

@maybebtc Commands have a Help string field, and it's up to the calling code to decide how/when to display it, not the command itself. For example, the CLI entry point prints the help text if there is an error when trying to call the Run Function, but the HTTP server does not.

@btc
Copy link
Contributor

btc commented Oct 29, 2014

@mappum cool. as you requested, force-pushed the side-by-side version to the branch under review.

"net/http"

cmds "github.com/jbenet/go-ipfs/commands"
commands "github.com/jbenet/go-ipfs/core/commands2"
Copy link
Contributor

Choose a reason for hiding this comment

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

@mappum could make it harder to vendor this if we import ipfs-specific packages in the commands lib. Perhaps this root command can be passed in?

func NewHandler(r commands.Command) Handler {
  return Handler{Root: r}
}

Copy link
Member

Choose a reason for hiding this comment

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

yeah pass it in. will be good to pull this out and give it to the community as the sweetest command package yet.

@btc
Copy link
Contributor

btc commented Oct 29, 2014

@jbenet
@mappum
@whyrusleeping

@mappum brought me up to speed on the status of the commands refactoring. I'm relaying this information for the group so we may chart a path to the completion of the overhaul.

There are a couple sample commands in place. cat, daemon, and init have been implemented using the new framework! 🎉

And there are just a couple remaining framework concerns. These are briefly outlined below.

To ease integration, may I suggest a course of action?

Merge into master once:

1. @mappum is satisfied with the outstanding framework concerns (outlined below)
2. framework stuff is CR'd
3. existing, newly-implemented commands are CR'd

After the merge, remaining commands can be implemented individually.

🐶 🐕 Dogfooding! Also, I bet it would be really helpful if we'd implement a command (end-to-end) and provide @mappum with feedback. Exercising the various edge-cases will help to iron out the framework and reveal any lingering unknown unknowns.


Framework Concerns

  • file arguments in CLI (blocks cli/add.go)

... when to read args as files and stream the data, vs sending an arg string. e.g. ipfs add foo.txt - @mappum

  • formatting

I just added a Format field to Command, it's a function that takes in a command response and outputs human-readable plaintext. i set it up assuming it would be able to read in the struct response values, which works when running commands locally, but when you run it with unmarshalled output (e.g. from a HTTP response), we have a map[string]interface{} instead of a struct... - @mappum

CLI Concerns

  • add.go
  • block.go
  • bootstrap.go
  • cat.go
  • commands.go
  • config.go
  • daemon.go
  • diag.go
  • init.go
  • ipfs.go
  • log.go
  • ls.go
  • mount_unix.go
  • mount_windows.go
  • name.go
  • objects.go
  • pin.go
  • publish.go
  • refs.go
  • resolve.go
  • run.go
  • serve.go
  • tour.go
  • update.go
  • version.go

@mappum
Copy link
Contributor Author

mappum commented Oct 29, 2014

In the concerns checklist:

  • formatting

I took care of that, the beep command is an example of how it works.

@jbenet
Copy link
Member

jbenet commented Oct 29, 2014

@mappum @maybebtc bravo! this clarity on status makes me feel great. LMK which command you'd like me to tackle.

Thoughts file args in cli concern

I think we could either

  1. create an commands.Argument abstraction, like commands.Option that specifies the type. This would help parse things (yay), and cli could make an io.Reader if arg type is "InputFile" or somethig.
  2. provide an optional arg parsing function to commands, which can manipulate the args as needed. simpler, but prob less useful.

@mappum
Copy link
Contributor Author

mappum commented Oct 29, 2014

About the file args in CLI:

Something I've noticed is that args are almost always just strings, which is why I didn't already make static type definitions for them (on the rare occasion that it is an int or something, the command can convert it itself). Also, adding argument specifications can take away some features, such as variadic args (e.g. ipfs cat can take any number of args).

cli could make an io.Reader if arg type is "InputFile"

That sounds pretty clean.

@jbenet
Copy link
Member

jbenet commented Oct 29, 2014

on the rare occasion that it is an int or something, the command can convert it itself

but might be nice if this was done for you. could get rid of lots of implementation bugs if type checking happens on parse

Also, adding argument specifications can take away some features, such as variadic args

Maybe, not necessarily, you could have something like:

cmd := &Command{
  Args: []Arg{
    Arg{Type: Integer}, // may not be there.
    Arg{Type: String, Required: true}, // must have it or fail
    Arg{Type: InputFile, Required: true, Variadic: true}, // required = 1 or more
  }
  Args: []Arg(String)
}

And Args slice is considered invalid unless there is exactly one Variadic arg, at the end.

And this arg slice descriptor would error out if any of the InputFiles don't exist :)


If we want to do anything more complicated, can always just get variadic strings and deal with it yourself. (so have full power, but the lib does some lifting for you).

@jbenet
Copy link
Member

jbenet commented Oct 29, 2014

@mappum @maybebtc lmk which commands you'd like me to try to implement tonight

@btc
Copy link
Contributor

btc commented Oct 30, 2014

@jbenet refs is a good place to start.

@btc
Copy link
Contributor

btc commented Oct 31, 2014

  • block get
  • block put

Heads up @mappum, I'm porting the above to get acquainted.

@mappum
Copy link
Contributor Author

mappum commented Nov 4, 2014

@jbenet @maybebtc
Now all the comments have been addressed. Care to give it another look?

This was referenced Nov 4, 2014
@jbenet
Copy link
Member

jbenet commented Nov 4, 2014

We're splitting this into #262 and #263. Closing this for posterity.

@jbenet jbenet closed this Nov 4, 2014
@jbenet jbenet removed the status/in-progress In progress label Nov 4, 2014
@btc btc deleted the commands-refactor branch January 21, 2015 01:02
@Stebalien Stebalien mentioned this pull request May 26, 2020
77 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants