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

Command Interface #150

Merged
merged 73 commits into from
Oct 22, 2014
Merged

Command Interface #150

merged 73 commits into from
Oct 22, 2014

Conversation

mappum
Copy link
Contributor

@mappum mappum commented Oct 10, 2014

This isn't ready for merge yet, PRing for code review/comments.

@btc btc added the status/in-progress In progress label Oct 10, 2014
@jbenet
Copy link
Member

jbenet commented Oct 10, 2014

Couple style things, setup your editor to automatically run go fmt, golint, and go build. There's nice packages for all editors out there.

type Command struct {
Help string
Options []Option
f func(*Request) (interface{}, error)
Copy link
Member

Choose a reason for hiding this comment

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

if we have a Request object, maybe could return a Response object too.

@jbenet
Copy link
Member

jbenet commented Oct 10, 2014

looks good so far!

@mappum
Copy link
Contributor Author

mappum commented Oct 10, 2014

The recent changes deal with command output. Commands will now be able to output an error or any interface{}, and it can then be easily marshaled based on the command caller's choice of encoding type.

type Error struct {
Message string
Code ErrorType
}
Copy link
Member

Choose a reason for hiding this comment

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

can make this struct implement the error interface by adding:

func (e *Error) Error() string {
  return fmt.Sprintf("%d error: %s", e.Code, e.Message)
}

http://golang.org/pkg/builtin/#error

And then just use it directly in the Response struct

Copy link
Member

Choose a reason for hiding this comment

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

bump on this. it simplifies the Response implementation

Copy link
Member

Choose a reason for hiding this comment

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

addressed

@jbenet
Copy link
Member

jbenet commented Oct 11, 2014

LGTM so far. Not sure why travis failed-- maybe try rebasing on master (not sure if up to date)

@jbenet
Copy link
Member

jbenet commented Oct 14, 2014

@mappum lmk when you want CR / ready to merge in.

@mappum
Copy link
Contributor Author

mappum commented Oct 16, 2014

The commands package is essentially done. LMK if any parts of the API look like they need polishing.

Next up is refactoring the commands, CLI entry point, and RPC client/server to use this. That is a big change, so it could possibly be in its own PR (and this could now be merged).

Options []Option
f func(*Request, *Response)
subcommands map[string]*Command
}
Copy link
Member

Choose a reason for hiding this comment

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

  • f -> function (meaningful var name at object level)
  • make a type for the function
  • comments on exported symbols (golint)
// Function is the type of function that Commands use. 
// It reads from the Request, and writes results to the Response.
type Function func(*Request, *Response)

// Command is a runnable command, with input arguments and options (flags).
// It can also have subcommands, to group units of work into sets.
type Command struct {
    Help        string
    Options     []Option

    function    Function
    subcommands map[string]*Command
}

Copy link
Member

Choose a reason for hiding this comment

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

addressed

@jbenet
Copy link
Member

jbenet commented Oct 21, 2014

@mappum rebased on top of new master.

@jbenet
Copy link
Member

jbenet commented Oct 21, 2014

@mappum: one comment re Parse returning Request. other changes LGTM.

one caveat i have on this is i think that the Register call won't let us define commands statically. It would be good to be able to continue defining things statically, and then have the options checked on dispatch (i.e. either in Run (but this may do work more than once) or when constructing the Request).

@jbenet
Copy link
Member

jbenet commented Oct 21, 2014

actually, hang on. it may not be needed, given that it's cumbersome to define it statically, and we may want functions to help us with making options etc:

// in a function
cmdIpfsPin = &commands.Command{
  Description:     "pin ipfs objects to local storage.",
  Help: `Manages ipfs objects pinned to (kept in) local storage.`,
  Options: []Option{
    Option{[]string{"r", "recursive"}, commands.Bool},
  },
  Run: func(req Request, res Response) { ... },
  Subcommands: map[string]*Command{
    "add": cmdIpfsPinAdd,
    "rm": cmdIpfsPinRm,
  }
}

or

var cmdIpfsPin = &cmd.Command{
  Description:     "pin ipfs objects to local storage.",
  Help: `Manages ipfs objects pinned to (kept in) local storage.`,
  Options: []Option{
    cmd.MakeOption(cmd.Bool, "pin objects recursively", "r", "recursive")
  },
  Run: func(req Request, res Response) { ... },
}
cmdIpfsPin.Register("add", cmdIpfsPinAdd)
cmdIpfsPin.Register("rm", cmdIpfsPinRm)
// I really dont like this o/  it makes it easier to change commands after startup, or 
// to move this Register call elsewhere in the code. 

And, autogenerating the "subcommands" part of the help from the subcommands map won't work well: it won't let us specify order.

@jbenet
Copy link
Member

jbenet commented Oct 22, 2014

@mappum: did you opt for keeping a separate Stream instead of assigning to the same value?

ok, let's merge this and see how the interface has to change when we fit the other commands to it.

jbenet added a commit that referenced this pull request Oct 22, 2014
@jbenet jbenet merged commit c4af76c into master Oct 22, 2014
@jbenet jbenet deleted the commands branch October 22, 2014 01:33
@jbenet
Copy link
Member

jbenet commented Oct 22, 2014

Awesome

@Stebalien Stebalien mentioned this pull request May 26, 2020
77 tasks
ariescodescream pushed a commit to ariescodescream/go-ipfs that referenced this pull request Oct 23, 2021
Add explanation to NewDHT/NewDHTClient doc strings
@aschmahmann aschmahmann mentioned this pull request Dec 1, 2021
80 tasks
laurentsenta pushed a commit to laurentsenta/kubo that referenced this pull request Feb 25, 2022
* remove the EnableRelayHop option in the SwarmConfig

* add an option to disable the limited relay

* make the relay service resources configurable

* refactor: use custom types

This enables us to swap defaults in go-ipfs without touching the config
file generated during `ipfs init`

ipfs/go-ipfs-config#146 (comment)
ipfs/go-ipfs-config#146 (comment)

* use OptionalDuration in RelayService configuration

* fix: *OptionalInteger with omitempty

This removes null values from the config

* fix: Flag does not need to be a pointer

* refactor: flatten RelayService limits

this simplifies consumer code and removes nil footgun

* docs: clarify different relay types

* feat: flag for ForceReachability mode in libp2p (ipfs#150)

adds Internal.Libp2pForceReachability
needed for sharness tests in ipfs#8522

Co-authored-by: Marcin Rataj <lidel@lidel.org>

Co-authored-by: Marcin Rataj <lidel@lidel.org>
laurentsenta pushed a commit to laurentsenta/kubo that referenced this pull request Feb 25, 2022
* remove the EnableRelayHop option in the SwarmConfig

* add an option to disable the limited relay

* make the relay service resources configurable

* refactor: use custom types

This enables us to swap defaults in go-ipfs without touching the config
file generated during `ipfs init`

ipfs/go-ipfs-config#146 (comment)
ipfs/go-ipfs-config#146 (comment)

* use OptionalDuration in RelayService configuration

* fix: *OptionalInteger with omitempty

This removes null values from the config

* fix: Flag does not need to be a pointer

* refactor: flatten RelayService limits

this simplifies consumer code and removes nil footgun

* docs: clarify different relay types

* feat: flag for ForceReachability mode in libp2p (ipfs#150)

adds Internal.Libp2pForceReachability
needed for sharness tests in ipfs#8522

Co-authored-by: Marcin Rataj <lidel@lidel.org>

Co-authored-by: Marcin Rataj <lidel@lidel.org>
laurentsenta pushed a commit to laurentsenta/kubo that referenced this pull request Mar 4, 2022
* remove the EnableRelayHop option in the SwarmConfig

* add an option to disable the limited relay

* make the relay service resources configurable

* refactor: use custom types

This enables us to swap defaults in go-ipfs without touching the config
file generated during `ipfs init`

ipfs/go-ipfs-config#146 (comment)
ipfs/go-ipfs-config#146 (comment)

* use OptionalDuration in RelayService configuration

* fix: *OptionalInteger with omitempty

This removes null values from the config

* fix: Flag does not need to be a pointer

* refactor: flatten RelayService limits

this simplifies consumer code and removes nil footgun

* docs: clarify different relay types

* feat: flag for ForceReachability mode in libp2p (ipfs#150)

adds Internal.Libp2pForceReachability
needed for sharness tests in ipfs#8522

Co-authored-by: Marcin Rataj <lidel@lidel.org>

Co-authored-by: Marcin Rataj <lidel@lidel.org>
laurentsenta pushed a commit to laurentsenta/kubo that referenced this pull request Mar 4, 2022
* remove the EnableRelayHop option in the SwarmConfig

* add an option to disable the limited relay

* make the relay service resources configurable

* refactor: use custom types

This enables us to swap defaults in go-ipfs without touching the config
file generated during `ipfs init`

ipfs/go-ipfs-config#146 (comment)
ipfs/go-ipfs-config#146 (comment)

* use OptionalDuration in RelayService configuration

* fix: *OptionalInteger with omitempty

This removes null values from the config

* fix: Flag does not need to be a pointer

* refactor: flatten RelayService limits

this simplifies consumer code and removes nil footgun

* docs: clarify different relay types

* feat: flag for ForceReachability mode in libp2p (ipfs#150)

adds Internal.Libp2pForceReachability
needed for sharness tests in ipfs#8522

Co-authored-by: Marcin Rataj <lidel@lidel.org>

Co-authored-by: Marcin Rataj <lidel@lidel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants