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

Use cobra to improve CLI #38

Merged
merged 8 commits into from Dec 8, 2017
Merged

Use cobra to improve CLI #38

merged 8 commits into from Dec 8, 2017

Conversation

chadell
Copy link
Collaborator

@chadell chadell commented Nov 25, 2017

@dcaba I had to change "host" flag to "target", because "-h" is assigned to "help"

@chadell chadell mentioned this pull request Nov 25, 2017
@chadell chadell requested a review from dcaba November 25, 2017 16:12
README.md Outdated
-s, --sleep int Time you want to sleep between connections, in ms (default 10)
-t, --target string [Required] Target host you want to open tcp connections against
Copy link
Collaborator

Choose a reason for hiding this comment

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

do you really like target? For me a host is more concise; target is some kind of "generic", and someone may enter into confusion when specifying the format (host:port as the target value?). I see, in any case, the problem with the abbreviation of host (-h -> -help). Maybe we can just mimic the behaviour of nc or telnet; "destination port" could be the last mandatory argument (without a flag)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack, see this commit 72fd4b1

README.md Outdated
tcpgoon tests concurrent connections towards a server listening on a TCP port

Usage:
tcpgoon <host> <port> [flags]
Copy link
Collaborator

Choose a reason for hiding this comment

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

flags after looks super strange! Can we move to tcpgoon [flags] ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will have a look, but it doesn't look to weird to me :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've changed in usage, but in the end it has no effect, you can put flags before or after arguments.

@dcaba
Copy link
Collaborator

dcaba commented Dec 3, 2017

I did a go get ./..., but go build was failing in the root directory (for me):

 % go build
# github.com/spf13/cobra
../../spf13/cobra/command.go:452:12: fs.ShorthandLookup undefined (type *pflag.FlagSet has no field or method ShorthandLookup)
../../spf13/cobra/command.go:1287:10: c.lflags.SortFlags undefined (type *pflag.FlagSet has no field or method SortFlags)
../../spf13/cobra/command.go:1287:32: c.Flags().SortFlags undefined (type *pflag.FlagSet has no field or method SortFlags)
../../spf13/cobra/command.go:1465:18: c.parentsPflags.SortFlags undefined (type *pflag.FlagSet has no field or method SortFlags)

I had to run go get -u github.com/spf13/pflag, as pointed by spf13/cobra#450 . Is it normal?

cmd/root.go Outdated
@@ -0,0 +1,83 @@
package cmd
Copy link
Collaborator

Choose a reason for hiding this comment

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

why root.go and not main.go? What are you using as a reference for this directory/file structure?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's the one recommended in Cobra readme ("Using the Cobra Library"), for the root command ("tcpgoon"). If we go for a subcommands mode, we will create specific files for each one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've rename root.go to tcpgoon.go as the main command. If we add more modes/subcommands later we will creates files for each one.

cmd/root.go Outdated
return nil
}

func enableDebugging(params tcpgoonParams) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

its strange to have a function that wraps another one that looks to do the same. To make the differentiation obvious, I'd rename to enableDebuggingIfFlagSet

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

@@ -0,0 +1,24 @@
package cmd
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that we have this cmd package, not sure if having a separate cmdutil makes sense... what do you think?

Copy link
Collaborator Author

@chadell chadell Dec 3, 2017

Choose a reason for hiding this comment

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

mmm, this cmd package is focused on cobra commands/subcommands, and cmdutils are utils used there. We could merge them, but also keep separated.
As you prefer, we could merge them if it's clearer to you, I also think that reduce the number of packages makes it simpler.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, I would like to have only "command" files in package cmd, to make it easier to understand.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No problem then... i have some doubts the cmdutil is actually a good name (i don't want this to become a wildcard with no sense), but its true is quite isolated from the main() logic

cmd/tcpgoon.go Outdated
"github.com/dachad/tcpgoon/tcpclient"
)

func run(params tcpgoonParams) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if we ever have more than one command... whats your idea about the structure? I've been checking the skeleton https://github.com/tcnksm/gcli generates, and its quite nice (but does not support cobra). Maybe you want to take a look and see if this affects to the existence/name of this tcpgoon.go

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My idea would be create new files for each subcommand and call them from the main.go, something like https://github.com/gohugoio/hugo/blob/master/commands/hugo.go#L185
and implement them like this https://github.com/gohugoio/hugo/blob/master/commands/server.go.
So, to make it more clearer I could rename root.go to tcpgoon.go and consolidate this root command...

Copy link
Collaborator

Choose a reason for hiding this comment

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

👏

@dcaba
Copy link
Collaborator

dcaba commented Dec 3, 2017

Hi! Take a look to the previous comments... most of them are just ideas, rather than change requirements (i was expecting your thoughts rather than immediate new commits :) . For instance, having a generic target is something you may have been able to defend, for instance, if we want to reuse the same field to other ways to indicate the target when we cover service discovery integration - but with this comment i don't expect you to rollback, just to think about it!!!). Good job Christian!

@chadell
Copy link
Collaborator Author

chadell commented Dec 3, 2017

I haven't been able to reproduce the error with "go build"..., but I've seen teh reference you mentioned. It seems that also works in Travis.

cmd/tcpgoon.go Outdated
params.hostPtr = args[0]
if port, err := strconv.Atoi(args[1]); err != nil && port <= 0 {
return errors.New("Port argument is not a valid integer")
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Improvement captured by codacy: "if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)" -> this means you don't need the else block. Move params.portPtr = port after the if, as if the condition matches it means it will exit before reaching that point

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, I've needed to move the strconv out of the if to make this possible, if not, a warning because port not assigned was raising

Copy link
Collaborator

@dcaba dcaba left a comment

Choose a reason for hiding this comment

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

See latest comments, but you can merge after minor changes / considering them

@dcaba
Copy link
Collaborator

dcaba commented Dec 7, 2017

@chadell , please, update also the dockerhub README with the updated flags!

@chadell chadell merged commit 21ac799 into master Dec 8, 2017
@chadell chadell deleted the issue-14-flags branch December 8, 2017 14:26
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

2 participants