Skip to content

Commit

Permalink
create transport subpackage; remove exchange data from transport bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Jun 26, 2019
1 parent 464766a commit 1a4248c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 66 deletions.
4 changes: 2 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"strings"

"github.com/lukasjarosch/godin/internal/bundle"
"github.com/lukasjarosch/godin/internal/bundle/transport"
"github.com/lukasjarosch/godin/internal/godin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,7 +60,7 @@ func addCmd(cmd *cobra.Command, args []string) {
logrus.Info("sorry, this bundle type is not yet implemented :(")
case "subscriber":

_, err := bundle.InitializeAMQPTransport()
_, err := transport.InitializeAMQP()
if err != nil {
logrus.Errorf("failed to initialize AMQP transport: %s", err)
}
Expand Down
64 changes: 0 additions & 64 deletions internal/bundle/transport.go

This file was deleted.

69 changes: 69 additions & 0 deletions internal/bundle/transport/amqp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package transport

import (
"fmt"

"github.com/lukasjarosch/godin/internal/godin"
"github.com/lukasjarosch/godin/internal/prompt"
"github.com/sirupsen/logrus"
config "github.com/spf13/viper"
)

const AMQPTransportKey = "transport.amqp"
const AMQPDefaultAddressKey = "transport.amqp.default_address"

type amqpTransport struct {
DefaultAddress string `json:"default_address"`
Exchange string `json:"exchange"`
ExchangeType string `json:"exchange_type"`
}

// InitializeAMQP will try to load the amqpTransport struct from the configuration.
// If that fails, the user will be prompted for the required values.
// The loaded or entered data is then saved to the config before returning.
func InitializeAMQP() (transport *amqpTransport, err error) {
// Load AMQP from config, if that fails create it and prompt the user for the values
transport, err = AmqpFromConfig()
if err != nil {
transport = &amqpTransport{}
if err := promptAmqpTransportValues(transport); err != nil {
return nil, fmt.Errorf("error while prompting AMQP transport values: %s", err)
}
}

config.Set(AMQPDefaultAddressKey, transport.DefaultAddress)
godin.SaveConfiguration()

return transport, nil
}

// Try and load the amqpTransport from the configuration.
// Returns an error if the config key "transport.amqp" does not exist.
// If any sub-key is not set properly, it will not be checked as it's still the
// developers responsibility to provide sane values.
func AmqpFromConfig() (*amqpTransport, error) {
if config.IsSet(AMQPTransportKey) {
defaultAddress := config.GetString(AMQPDefaultAddressKey)
logrus.Debug("AMQP transport is already configured, loading from config")

return &amqpTransport{
DefaultAddress: defaultAddress,
}, nil
}
return nil, fmt.Errorf("amqp transport not configured")
}

func promptAmqpTransportValues(transport *amqpTransport) (err error) {
// default AMQP server address
p := prompt.NewPrompt(
"AMQP default server address to use if AMQP_URL is not provided",
"amqp://username:password@host:port/vhost",
prompt.Validate(),
)
transport.DefaultAddress, err = p.Run()
if err != nil {
return err
}

return nil
}

0 comments on commit 1a4248c

Please sign in to comment.