Skip to content

Commit

Permalink
add CamelCase validator; basic subscriber init done
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Jun 26, 2019
1 parent 1a4248c commit 00bc11a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 29 deletions.
23 changes: 6 additions & 17 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
34 changes: 27 additions & 7 deletions internal/bundle/subscribe.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
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"`
}

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",
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/bundle/transport/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
10 changes: 10 additions & 0 deletions internal/prompt/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"github.com/manifoldco/promptui"
"regexp"
)

func Validate(validators ...promptui.ValidateFunc) promptui.ValidateFunc {
Expand Down Expand Up @@ -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
}
}
7 changes: 4 additions & 3 deletions pkg/amqp/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 00bc11a

Please sign in to comment.