diff options
| author | Will Hawkins <[email protected]> | 2021-12-14 08:59:04 -0500 |
|---|---|---|
| committer | Will Hawkins <[email protected]> | 2021-12-14 08:59:04 -0500 |
| commit | 45b8e5f0e195de0a9c077b4cdab9410f22654699 (patch) | |
| tree | 19061564ff76bf7d9adc7bf9de087152047503ea | |
| parent | 728a81b8205678abeeb54a69e9e26ee06c18b607 (diff) | |
Add sanity checking for configuration.
| -rw-r--r-- | networkQuality.go | 19 | ||||
| -rw-r--r-- | utilities/utilities.go | 7 |
2 files changed, 26 insertions, 0 deletions
diff --git a/networkQuality.go b/networkQuality.go index ae8e25b..886022e 100644 --- a/networkQuality.go +++ b/networkQuality.go @@ -9,6 +9,7 @@ import ( "io/ioutil" _ "log" "net/http" + "net/url" "os" "time" @@ -33,6 +34,19 @@ func (c *Config) String() string { return fmt.Sprintf("Version: %d\nSmall URL: %s\nLarge URL: %s\nUpload URL: %s", c.Version, c.Urls.SmallUrl, c.Urls.LargeUrl, c.Urls.UploadUrl) } +func (c *Config) IsValid() error { + if parsedUrl, err := url.ParseRequestURI(c.Urls.LargeUrl); err != nil || parsedUrl.Scheme != "https" { + return fmt.Errorf("Configuration url large_https_download_url is invalid: %s", utilities.Conditional(len(c.Urls.LargeUrl) != 0, c.Urls.LargeUrl, "Missing")) + } + if parsedUrl, err := url.ParseRequestURI(c.Urls.SmallUrl); err != nil || parsedUrl.Scheme != "https" { + return fmt.Errorf("Configuration url small_https_download_url is invalid: %s", utilities.Conditional(len(c.Urls.SmallUrl) != 0, c.Urls.SmallUrl, "Missing")) + } + if parsedUrl, err := url.ParseRequestURI(c.Urls.UploadUrl); err != nil || parsedUrl.Scheme != "https" { + return fmt.Errorf("Configuration url https_upload_url is invalid: %s", utilities.Conditional(len(c.Urls.UploadUrl) != 0, c.Urls.UploadUrl, "Missing")) + } + return nil +} + func toMBs(bytes float64) float64 { return float64(bytes) / float64(1024*1024) } @@ -171,6 +185,11 @@ func main() { // TODO: Make sure that all configuration values are present and accounted for! + if err := config.IsValid(); err != nil { + fmt.Fprintf(os.Stderr, "Error: Invalid configuration returned from %s: %v\n", configUrl, err) + return + } + if *debug { fmt.Printf("Configuration: %s\n", &config) } diff --git a/utilities/utilities.go b/utilities/utilities.go index fd5c824..54f0f4a 100644 --- a/utilities/utilities.go +++ b/utilities/utilities.go @@ -8,3 +8,10 @@ func SignedPercentDifference(current float64, previous float64) (difference floa func AbsPercentDifference(current float64, previous float64) (difference float64) { return (math.Abs(current-previous) / (float64(current+previous) / 2.0)) * float64(100) } + +func Conditional(condition bool, t string, f string) string { + if condition { + return t + } + return f +} |
