Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -S --ssl flag to use SSL (amqps) #2

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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