Skip to content

Commit

Permalink
Removed code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas-gibbons committed Jun 16, 2017
1 parent 68dd90e commit 5bf4bbe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 47 deletions.
25 changes: 2 additions & 23 deletions cmd/tconsume/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/trafero/tstack/consume/graphite"
"github.com/trafero/tstack/consume/influxdb"
"github.com/trafero/tstack/consume/stdout"
"github.com/trafero/tstack/tstackutil"
"log"
"net/url"
)

var username, password, mqtturl, topic, ctype string
Expand Down Expand Up @@ -87,7 +87,7 @@ func main() {

log.Printf("Using broker %s and topic %s.", s.Broker, topic)

secure, err = isSecureUrl(s.Broker)
secure, err = tstackutil.IsSecureUrl(s.Broker)
checkErr(err)

switch ctype {
Expand Down Expand Up @@ -130,27 +130,6 @@ func main() {
select {}
}

// isSecureUrl determines if the given URL has a secure scheme type
func isSecureUrl(urlstring string) (bool, error) {
u, err := url.Parse(urlstring)
if err != nil {
return false, err
}

switch u.Scheme {
case "tcp":
return false, nil
case "ssl":
return true, nil
case "http":
return false, nil
case "https":
return true, nil
default:
return false, errors.New("Unknown broker URL type.")
}
}

func checkErr(err error) {
if err != nil {
panic(err)
Expand Down
26 changes: 2 additions & 24 deletions cmd/tpublish/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main

import (
"errors"
"flag"
"github.com/trafero/tstack/client/mqtt"
"github.com/trafero/tstack/client/settings"
"github.com/trafero/tstack/tstackutil"
"log"
"net/url"
)

var username, password, mqtturl, topic, payload, cacertfile string
Expand Down Expand Up @@ -65,7 +64,7 @@ func main() {

log.Printf("Publishing topic %s.", topic)

secure, err = isSecureUrl(s.Broker)
secure, err = tstackutil.IsSecureUrl(s.Broker)
checkErr(err)

log.Printf("Connecting to broker %s", s.Broker)
Expand All @@ -83,27 +82,6 @@ func main() {
checkErr(err)
}

// isSecureUrl determines if the given URL has a secure scheme type
func isSecureUrl(urlstring string) (bool, error) {
u, err := url.Parse(urlstring)
if err != nil {
return false, err
}

switch u.Scheme {
case "tcp":
return false, nil
case "ssl":
return true, nil
case "http":
return false, nil
case "https":
return true, nil
default:
return false, errors.New("Unknown broker URL type.")
}
}

func checkErr(err error) {
if err != nil {
panic(err)
Expand Down
27 changes: 27 additions & 0 deletions tstackutil/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tstackutil

import (
"errors"
"net/url"
)

// IsSecureUrl determines if the given URL has a secure scheme type
func IsSecureUrl(urlstring string) (bool, error) {
u, err := url.Parse(urlstring)
if err != nil {
return false, err
}

switch u.Scheme {
case "tcp":
return false, nil
case "ssl":
return true, nil
case "http":
return false, nil
case "https":
return true, nil
default:
return false, errors.New("Unknown broker URL type.")
}
}

0 comments on commit 5bf4bbe

Please sign in to comment.