From b24cb38c0ec499d75a623eab6eadd32506a78a18 Mon Sep 17 00:00:00 2001 From: David Quattlebaum Date: Tue, 19 Sep 2017 11:16:13 -0400 Subject: [PATCH 1/2] added --simple option it will only display resulting speed. Good for nagios checks --- main.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index f7545c4..4c48593 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import "io" import "net/http" import "strconv" import "time" +import "strings" import "github.com/spf13/cobra" import "github.com/gesquive/cli" @@ -23,6 +24,7 @@ var cfgFile string var logDebug bool var notHTTPS bool var showVersion bool +var simple bool //RootCmd is the only command var RootCmd = &cobra.Command{ @@ -60,6 +62,8 @@ func init() { RootCmd.PersistentFlags().MarkHidden("debug") //TODO: Allow to estimate using time or size + + RootCmd.PersistentFlags().BoolVar(&simple, "simple", false, "Only display final result") } func initLog() { @@ -137,10 +141,11 @@ func calculateBandwidth(urls []string) (err error) { // Start reading go asyncCopy(i, ch, &bandwidthMeter, response.Body) } - } - cli.Infof("Estimating current download speed\n") + if (! simple ) { + cli.Infof("Estimating current download speed\n") + } for { select { case results := <-ch: @@ -148,18 +153,24 @@ func calculateBandwidth(urls []string) (err error) { fmt.Fprintf(os.Stdout, "\n%v\n", results.err) os.Exit(1) } - - fmt.Printf("\r%s - %s", - format.BitsPerSec(bandwidthMeter.Bandwidth()), - format.Percent(primaryBandwidthReader.BytesRead(), bytesToRead)) completed++ - fmt.Printf(" \n") - fmt.Printf("Completed in %.1f seconds\n", bandwidthMeter.Duration().Seconds()) + if (simple) { + fmt.Printf("%s\n", + strings.TrimSpace(format.BitsPerSec(bandwidthMeter.Bandwidth()))) + } else { + fmt.Printf("\r%s - %s", + format.BitsPerSec(bandwidthMeter.Bandwidth()), + format.Percent(primaryBandwidthReader.BytesRead(), bytesToRead)) + fmt.Printf(" \n") + fmt.Printf("Completed in %.1f seconds\n", bandwidthMeter.Duration().Seconds()) + } return nil case <-time.After(100 * time.Millisecond): - fmt.Printf("\r%s - %s", - format.BitsPerSec(bandwidthMeter.Bandwidth()), - format.Percent(primaryBandwidthReader.BytesRead(), bytesToRead)) + if (! simple) { + fmt.Printf("\r%s - %s", + format.BitsPerSec(bandwidthMeter.Bandwidth()), + format.Percent(primaryBandwidthReader.BytesRead(), bytesToRead)) + } } } } From 2c4a264e2446ca2828eadbcae953be650ccaa62b Mon Sep 17 00:00:00 2001 From: David Quattlebaum Date: Tue, 19 Sep 2017 12:28:04 -0400 Subject: [PATCH 2/2] up version a notch --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 4c48593..ffb40ad 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import "github.com/gesquive/fast-cli/fast" import "github.com/gesquive/fast-cli/format" import "github.com/gesquive/fast-cli/meters" -var version = "v0.2.8" +var version = "v0.2.9" var dirty = "" var displayVersion string