-
Notifications
You must be signed in to change notification settings - Fork 21
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
Conversation
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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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] ?
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
I did a
I had to run |
cmd/root.go
Outdated
@@ -0,0 +1,83 @@ | |||
package cmd |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏
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! |
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this 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
@chadell , please, update also the dockerhub README with the updated flags! |
8be632d
to
3f2a9b7
Compare
@dcaba I had to change "host" flag to "target", because "-h" is assigned to "help"