services.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package services
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "log"
  7. "github.com/fgm/izidic"
  8. )
  9. const (
  10. PArgs = "args"
  11. PHandler = "handler"
  12. PName = "name"
  13. PProfile = "profile"
  14. PQName = "queue-name"
  15. PRegion = "region"
  16. PURL = "url"
  17. PWriter = "writer"
  18. SvcClient = "sqs"
  19. SvcConsumer = "consumeMessage"
  20. SvcFlags = "flags"
  21. SvcLister = "lister"
  22. SvcLogger = "logger"
  23. SvcProducer = "producer"
  24. SvcReceiver = "receiver"
  25. SvcRedriver = "redriver"
  26. )
  27. func FlagsService(dic *izidic.Container) (any, error) {
  28. fs := flag.NewFlagSet(dic.MustParam(PName).(string), flag.ContinueOnError)
  29. profile := fs.String(PProfile, "test-profile", "The AWS profile")
  30. region := fs.String(PRegion, "eu-west-3", "The AWS region to connect to")
  31. qName := fs.String(PQName, "dummy-queue", "The queue name")
  32. sqsURL := fs.String(PURL, "http://localhost:4566", "The SQS endpoint URL")
  33. if err := fs.Parse(dic.MustParam(PArgs).([]string)); err != nil {
  34. return nil, fmt.Errorf("cannot obtain CLI args")
  35. }
  36. dic.Store(PProfile, *profile)
  37. dic.Store(PRegion, *region)
  38. dic.Store(PURL, *sqsURL)
  39. dic.Store(PQName, *qName)
  40. return fs, nil
  41. }
  42. // LoggerService is an izidic.Service also containing a one-time initialization action.
  43. func LoggerService(dic *izidic.Container) (any, error) {
  44. w := dic.MustParam(PWriter).(io.Writer)
  45. log.SetOutput(w) // Support dependency code not taking an injected logger.
  46. logger := log.New(w, "", log.LstdFlags)
  47. return logger, nil
  48. }