Skip to content

Commit

Permalink
Merge pull request #2 from marcinkoziej/feature/ssl-option
Browse files Browse the repository at this point in the history
Add -S --ssl flag to use SSL (amqps)
  • Loading branch information
hassansin committed Dec 6, 2021
2 parents e2de50f + df544aa commit 2bdb8fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 1 addition & 2 deletions cmd/consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/oleiade/reflections"
Expand Down Expand Up @@ -36,8 +35,8 @@ Use comma-separated values for binding the same queue with multiple routing keys
cmd.SilenceUsage = true
cmd.SilenceErrors = true

uri := getUri()
// Dial amqp server
uri := "amqp://" + username + ":" + password + "@" + host + ":" + strconv.Itoa(port) + vhost
conn, err := amqp.Dial(uri)
if err != nil {
return fmt.Errorf("connection.open: %v", err)
Expand Down
3 changes: 1 addition & 2 deletions cmd/produce.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"reflect"
"strconv"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -46,7 +45,7 @@ To pass headers and properites, use '--headers' & '--properties' any number of t
message = string(bytes)
}

uri := "amqp://" + username + ":" + password + "@" + host + ":" + strconv.Itoa(port) + vhost
uri := getUri()
conn, err := amqp.Dial(uri)
if err != nil {
return fmt.Errorf("connection.open: %v", err)
Expand Down
17 changes: 17 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strings"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -18,6 +19,7 @@ var (
vhost string
username string
password string
ssl bool

// exchange options
exchange string
Expand All @@ -34,6 +36,20 @@ var (
durableQueue bool
)

func getUri() string {
var proto string = "amqp://"
if (ssl) {
proto = "amqps://"
// Change the port from amqp default to amqps default.
// not sure how to check if -P flag was given by the user
// so the perverse situtation where amqps runs on port 5672 would not work sorry
if (port == 5672) {
port = 5671
}
}
return proto + username + ":" + password + "@" + host + ":" + strconv.Itoa(port) + vhost
}

var valid_properties = map[string]string{
"content-type": "ContentType",
"content-encoding": "ContentEncoding",
Expand Down Expand Up @@ -93,6 +109,7 @@ func commonFlagSet() *pflag.FlagSet {
fs.StringVarP(&vhost, "vhost", "v", "/", "specify vhost")
fs.StringVarP(&username, "username", "u", "guest", "specify username")
fs.StringVarP(&password, "password", "p", "guest", "specify password")
fs.BoolVarP(&ssl, "ssl", "S", false, "use amqps")

fs.StringVarP(&exchange, "exchange", "e", "", `exchange name (default "")`)
fs.StringVarP(&routingkey, "key", "k", "", `routing key (default "")`)
Expand Down

0 comments on commit 2bdb8fe

Please sign in to comment.