From 00bc11acf1d7a0a414d1c466c3c729814daa1bcd Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 26 Jun 2019 20:04:02 +0200 Subject: [PATCH] add CamelCase validator; basic subscriber init done --- cmd/add.go | 23 ++++++--------------- go.mod | 1 + go.sum | 2 ++ internal/bundle/subscribe.go | 34 ++++++++++++++++++++++++------- internal/bundle/transport/amqp.go | 4 ++-- internal/prompt/validate.go | 10 +++++++++ pkg/amqp/subscribe.go | 7 ++++--- 7 files changed, 52 insertions(+), 29 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 855a51f..40c24fb 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -10,6 +10,7 @@ import ( "github.com/lukasjarosch/godin/internal/godin" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/lukasjarosch/godin/internal/bundle" ) func init() { @@ -59,28 +60,16 @@ func addCmd(cmd *cobra.Command, args []string) { "transport": logrus.Info("sorry, this bundle type is not yet implemented :(") case "subscriber": - _, err := transport.InitializeAMQP() if err != nil { logrus.Errorf("failed to initialize AMQP transport: %s", err) } - /* - sub, err := bundle.InitializeSubscriber() - if err != nil { - logrus.Errorf("failed to initialize subscriber: %s", err) - os.Exit(1) - } - - // add to config - subscriptions := make(map[string]interface{}) - if config.IsSet("transport.amqp.subscriptions") { - subscriptions = config.GetStringMap("transport.amqp.subscriptions") - } - subscriptions[sub.HandlerName] = sub.Subscription - config.Set("transport.amqp.subscriptions", subscriptions) - godin.SaveConfiguration() - */ + _, err = bundle.InitializeSubscriber() + if err != nil { + logrus.Errorf("failed to initialize subscriber: %s", err) + os.Exit(1) + } } } diff --git a/go.mod b/go.mod index b216f51..38cd90c 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/gobuffalo/packr/v2 v2.4.0 github.com/google/uuid v1.1.1 // indirect github.com/huandu/xstrings v1.2.0 // indirect + github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 github.com/imdario/mergo v0.3.7 // indirect github.com/manifoldco/promptui v0.3.2 github.com/pelletier/go-toml v1.3.0 // indirect diff --git a/go.sum b/go.sum index 750618e..88c7534 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0= github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4= +github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 h1:ECW73yc9MY7935nNYXUkK7Dz17YuSUI9yqRqYS8aBww= +github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE= github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI= github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= diff --git a/internal/bundle/subscribe.go b/internal/bundle/subscribe.go index a39be14..79b4298 100644 --- a/internal/bundle/subscribe.go +++ b/internal/bundle/subscribe.go @@ -1,10 +1,17 @@ package bundle import ( + "fmt" + + "github.com/iancoleman/strcase" + "github.com/lukasjarosch/godin/internal/godin" "github.com/lukasjarosch/godin/internal/prompt" "github.com/lukasjarosch/godin/pkg/amqp" + config "github.com/spf13/viper" ) +const SubscriberKey = "transport.amqp.subscriber" + type subscriber struct { Subscription amqp.Subscription HandlerName string `json:"handler_name"` @@ -12,18 +19,29 @@ type subscriber struct { func InitializeSubscriber() (*subscriber, error) { sub := amqp.Subscription{} - handlerName, err := promptValues(sub) + handlerName, err := promptValues(&sub) if err != nil { return nil, err } + handlerName = strcase.ToSnake(handlerName) + sub.ExchangeType = "durable" // currently not configurable + + confSub := config.GetStringMap(SubscriberKey) + if _, ok := confSub[handlerName]; ok == true { + return nil, fmt.Errorf("subscriber '%s' is already registered", handlerName) + } + confSub[handlerName] = sub + config.Set(SubscriberKey, confSub) + godin.SaveConfiguration() + return &subscriber{ Subscription: sub, HandlerName: handlerName, }, nil } -func promptValues(sub amqp.Subscription) (handlerName string, err error) { +func promptValues(sub *amqp.Subscription) (handlerName string, err error) { // Topic p := prompt.NewPrompt( "AMQP subscription Topic", @@ -39,7 +57,7 @@ func promptValues(sub amqp.Subscription) (handlerName string, err error) { // Exchange p = prompt.NewPrompt( "AMQP Exchange name", - "user-Exchange", + "user-exchange", prompt.Validate(), ) exchange, err := p.Run() @@ -51,7 +69,7 @@ func promptValues(sub amqp.Subscription) (handlerName string, err error) { // Queue p = prompt.NewPrompt( "AMQP Queue name which is bound to the Exchange", - "user-created-Queue", + "user-created-queue", prompt.Validate(), ) queue, err := p.Run() @@ -62,9 +80,11 @@ func promptValues(sub amqp.Subscription) (handlerName string, err error) { // HandlerName p = prompt.NewPrompt( - "Name of the handler for this subscription", - "UserCreatedHandler", - prompt.Validate(), + "Name of the handler for this subscription (CamelCase)", + "UserCreatedSubscriber", + prompt.Validate( + prompt.CamelCase(), + ), ) handler, err := p.Run() if err != nil { diff --git a/internal/bundle/transport/amqp.go b/internal/bundle/transport/amqp.go index 5d58627..4bd58b2 100644 --- a/internal/bundle/transport/amqp.go +++ b/internal/bundle/transport/amqp.go @@ -23,7 +23,7 @@ type amqpTransport struct { // 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() + transport, err = AMQPFromConfig() if err != nil { transport = &amqpTransport{} if err := promptAmqpTransportValues(transport); err != nil { @@ -41,7 +41,7 @@ func InitializeAMQP() (transport *amqpTransport, err error) { // 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) { +func AMQPFromConfig() (*amqpTransport, error) { if config.IsSet(AMQPTransportKey) { defaultAddress := config.GetString(AMQPDefaultAddressKey) logrus.Debug("AMQP transport is already configured, loading from config") diff --git a/internal/prompt/validate.go b/internal/prompt/validate.go index f912b51..6f7e616 100644 --- a/internal/prompt/validate.go +++ b/internal/prompt/validate.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" "github.com/manifoldco/promptui" + "regexp" ) func Validate(validators ...promptui.ValidateFunc) promptui.ValidateFunc { @@ -44,3 +45,12 @@ func GoSuffix() promptui.ValidateFunc { } } +func CamelCase() promptui.ValidateFunc { + return func(s string) error { + ok, _ := regexp.Match(`([A-Z][a-z0-9]+)+`, []byte(s)) + if !ok { + return fmt.Errorf("string is not CamelCase") + } + return nil + } +} \ No newline at end of file diff --git a/pkg/amqp/subscribe.go b/pkg/amqp/subscribe.go index 9e04c1c..b84e06f 100644 --- a/pkg/amqp/subscribe.go +++ b/pkg/amqp/subscribe.go @@ -7,9 +7,10 @@ import ( type SubscriberHandler func(delivery amqp.Delivery) type Subscription struct { - Topic string `json:"topic"` - Exchange string `json:"exchange"` - Queue string `json:"queue"` + Topic string `json:"topic"` + Exchange string `json:"exchange"` + ExchangeType string `json:"exchange_type"` + Queue string `json:"queue"` } type handler struct {