Skip to content

Commit

Permalink
Add -S --ssl flag to use SSL (amqps)
Browse files Browse the repository at this point in the history
You still need to pass -P 5671 for SSL port. Unsure how to override the
default port depending on if ssl flag is given
  • Loading branch information
marcinkoziej committed Dec 3, 2021
1 parent e2de50f commit 7adf7c4
Show file tree
Hide file tree
Showing 3 changed files with 13 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
11 changes: 11 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,14 @@ var (
durableQueue bool
)

func getUri() string {
var proto string = "amqp://"
if (ssl) {
proto = "amqps://"
}
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 +103,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 7adf7c4

Please sign in to comment.