-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c2dd5f
First try using cobra
chadell d706ed5
Output usage when validation fails
chadell 5527d43
Update Readme
chadell 9b0b935
Simply the main run function moving all flags work in the PreRun
chadell 6fdbb66
Move host/target and port to positional arguments instead of flags
chadell 8e5d4da
Improve readiness of cmd structure
chadell 29a2ba3
Move flags before arguments
chadell 3f2a9b7
small refinement
chadell File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/dachad/tcpgoon/cmdutil" | ||
"github.com/dachad/tcpgoon/debugging" | ||
"github.com/dachad/tcpgoon/mtcpclient" | ||
"github.com/dachad/tcpgoon/tcpclient" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type tcpgoonParams struct { | ||
hostPtr string | ||
portPtr int | ||
numberConnectionsPtr int | ||
delayPtr int | ||
connDialTimeoutPtr int | ||
debugPtr bool | ||
reportingIntervalPtr int | ||
assumeyesPtr bool | ||
} | ||
|
||
var params tcpgoonParams | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "tcpgoon [flags] <host> <port>", | ||
Short: "tcpgoon tests concurrent connections towards a server listening on a TCP port", | ||
Long: ``, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if err := validateRequiredArgs(¶ms, args); err != nil { | ||
cmd.Println(cmd.UsageString()) | ||
os.Exit(1) | ||
} | ||
enableDebuggingIfFlagSet(params) | ||
autorunValidation(params) | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runroot(params) | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
rootCmd.Flags().IntVarP(¶ms.numberConnectionsPtr, "connections", "c", 100, "Number of connections you want to open") | ||
rootCmd.Flags().IntVarP(¶ms.delayPtr, "sleep", "s", 10, "Time you want to sleep between connections, in ms") | ||
rootCmd.Flags().IntVarP(¶ms.connDialTimeoutPtr, "dial-timeout", "d", 5000, "Connection dialing timeout, in ms") | ||
rootCmd.Flags().BoolVarP(¶ms.debugPtr, "verbose", "v", false, "Print debugging information to the standard error") | ||
rootCmd.Flags().IntVarP(¶ms.reportingIntervalPtr, "interval", "i", 1, "Interval, in seconds, between stats updates") | ||
rootCmd.Flags().BoolVarP(¶ms.assumeyesPtr, "assume-yes", "y", false, "Force execution without asking for confirmation") | ||
} | ||
|
||
func validateRequiredArgs(params *tcpgoonParams, args []string) error { | ||
if len(args) != 2 { | ||
return errors.New("Number of required parameters doesn't match") | ||
} | ||
params.hostPtr = args[0] | ||
port, err := strconv.Atoi(args[1]) | ||
if err != nil && port <= 0 { | ||
return errors.New("Port argument is not a valid integer") | ||
} | ||
params.portPtr = port | ||
|
||
return nil | ||
} | ||
|
||
func enableDebuggingIfFlagSet(params tcpgoonParams) { | ||
if params.debugPtr { | ||
debugging.EnableDebug() | ||
} | ||
} | ||
|
||
func autorunValidation(params tcpgoonParams) { | ||
if !(params.assumeyesPtr || cmdutil.AskForUserConfirmation(params.hostPtr, params.portPtr, params.numberConnectionsPtr)) { | ||
fmt.Fprintln(debugging.DebugOut, "Execution not approved by the user") | ||
cmdutil.CloseAbruptly() | ||
} | ||
} | ||
|
||
func runroot(params tcpgoonParams) { | ||
tcpclient.DefaultDialTimeoutInMs = params.connDialTimeoutPtr | ||
|
||
// TODO: we should decouple the caller from the mtcpclient package (too many structures being moved from | ||
// one side to the other.. everything in a single structure, or applying something like the builder pattern, | ||
// may help | ||
connStatusCh, connStatusTracker := mtcpclient.StartBackgroundReporting(params.numberConnectionsPtr, params.reportingIntervalPtr) | ||
closureCh := mtcpclient.StartBackgroundClosureTrigger(connStatusTracker) | ||
mtcpclient.MultiTCPConnect(params.numberConnectionsPtr, params.delayPtr, params.hostPtr, params.portPtr, connStatusCh, closureCh) | ||
fmt.Fprintln(debugging.DebugOut, "Tests execution completed") | ||
|
||
cmdutil.CloseNicely(params.hostPtr, params.portPtr, connStatusTracker) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/dachad/tcpgoon/cmd" | ||
) | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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