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

Add catalog CLI functions #3204

Merged
merged 22 commits into from
Jul 14, 2017
Merged

Add catalog CLI functions #3204

merged 22 commits into from
Jul 14, 2017

Conversation

sethvargo
Copy link
Contributor

@sethvargo sethvargo commented Jun 29, 2017

I am restricting the first pass to "readonly" (no register or deregister functionality).

/cc @hashicorp/consul


List all datacenters:

$ consul catalog datacenters
Copy link
Contributor Author

Choose a reason for hiding this comment

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

❓ should these all be "actions" instead? Like list-datacenters and list-nodes? In the consul kv CLI, everything is an action (verb), but list-datacenters seems blah to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think just datacenters is fine since the only datacenter-related thing you can do is list them right now, if we added other actions later it could become consul catalog datacenters list.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I kind of like the verbs (like https://www.consul.io/docs/commands/operator/area.html), we could start out that way so we get help with the top-level command showing help and we've got a spot for more things later like deregister:

consul catalog datacenters list
consul catalog nodes list
consul catalog services list

If we start out with consul catalog datacenters running list, there's no where to park the help for the datacenters subcommands later, for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, that was my fear. I just also fear that we are going too deep (queue Inception gif). I find CLIs challenging to reason about beyond the third level, which is why I like the kv CLI

consul kv [action]

That's why I suggested

consul catalog list-datacenters

since then list is a verb. Otherwise, we have inconsistency where some CLI commands are "namespace verb" and others are "namespace noun verb".

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm - I can live with that - I do like verbs and flattening the verbs into one level of consul catalog <verb> seems like a good compromise.

"github.com/mitchellh/cli"
)

var _ cli.Command = (*CatalogDatacentersCommand)(nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kyhavlov I found this on the Internets as a better way to test is something implements an interface. We used to do this in test, but this will fail at build time if the interfaces don't match.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good find, that's a lot better than having to run a test

return strings.TrimSpace(helpText)
}

func (c *CatalogNodesCommand) Run(args []string) int {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still a WIP, and I wanted to solicit feedback on the API/UX here.

I can see a few possible ways this could go:

  • consul catalog nodes returns a list of node ids (no other data), and then -detailed returns a more tabular like (like consul kv get -detailed)
    • should it be node ids or node names?
    • should it be node names falling back to node ids?
  • consul catalog nodes returns everything in tabular format except tagged addrs, meta, and create/modify indexes

Thoughts? I think I like the first one, since it allows for better composition into other resources.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having it show tabular format similar to members (option 2) could still feed into cut or some other utility, but I don't really have a preference here, as long as there's a -detailed option.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was kind of expecting consul members-like table output for this, too, since there's a lot of info vs consul kv get -detailed. Maybe something like:

  1. consul catalog node list maybe a table of: node name, node id, address
  2. consul catalog node info -node=<name or id> does the kv -detailed kind of thing

Still kind of thinking about this one...

func prettyNode(w io.Writer, node *api.Node) error {
tw := tabwriter.NewWriter(w, 0, 2, 6, ' ', 0)
fmt.Fprintf(tw, "ID\t%s\n", node.ID)
if node.Node != "" {
Copy link
Contributor Author

@sethvargo sethvargo Jun 29, 2017

Choose a reason for hiding this comment

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

Can this ever be empty?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you try to start a node with a blank node name, it'll default to the hostname, so I don't think this should ever be empty.

Copy link
Contributor

@slackpad slackpad left a comment

Choose a reason for hiding this comment

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

Looking good - added some thoughts!


List all datacenters:

$ consul catalog datacenters
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I kind of like the verbs (like https://www.consul.io/docs/commands/operator/area.html), we could start out that way so we get help with the top-level command showing help and we've got a spot for more things later like deregister:

consul catalog datacenters list
consul catalog nodes list
consul catalog services list

If we start out with consul catalog datacenters running list, there's no where to park the help for the datacenters subcommands later, for example.


func (c *CatalogNodesCommand) Help() string {
helpText := `
Usage: consul catalog datacenters [options]
Copy link
Contributor

Choose a reason for hiding this comment

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

Stale.

return strings.TrimSpace(helpText)
}

func (c *CatalogNodesCommand) Run(args []string) int {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was kind of expecting consul members-like table output for this, too, since there's a lot of info vs consul kv get -detailed. Maybe something like:

  1. consul catalog node list maybe a table of: node name, node id, address
  2. consul catalog node info -node=<name or id> does the kv -detailed kind of thing

Still kind of thinking about this one...

@sethvargo
Copy link
Contributor Author

sethvargo commented Jul 10, 2017

$ consul catalog list-datacenters
dc1
dc2
$ consul catalog list-nodes
Node       ID        Address    DC
bacon-mac  d4e68c58  127.0.0.1  dc1
$ consul catalog list-nodes -service=consul
Node       ID        Address    DC
bacon-mac  d4e68c58  127.0.0.1  dc1
$ consul catalog list-services
consul
redis
$ consul catalog list-services -tags
consul
redis       primary,v1

@slackpad @kyhavlov @magiconair @preetapan I think this is ready for real review now! 😄

@sethvargo
Copy link
Contributor Author

Also, the more I'm typing and working with these, I think list is the wrong prefix 😦 . It's really painful to type each time, especially given there's no show- or update-, etc.

$ consul catalog datacenters
$ consul catalog nodes
$ consul catalog servies

Feels a lot better than

$ consul catalog list-datacenters
$ consul catalog list-nodes
$ consul catalog list-services

I've been using this pretty regularly for about a week, and I don't think list- is provide value or future-proofing ourselves. Thoughts?

@slackpad
Copy link
Contributor

@sethvargo that's fair - i'd be ok to drop the "list-" prefix as it does look ugly. We could put verbs for future actions and mix them, which seems workable:

$ consul catalog deregister -node=foo -service=bar

@sethvargo
Copy link
Contributor Author

Okay - updated

This actually calls up to the servers, so want to not include "agent" here.
@slackpad slackpad merged commit afd83a9 into master Jul 14, 2017
@slackpad slackpad deleted the sethvargo/catalog_cli branch July 14, 2017 19:45
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.

None yet

3 participants