|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/analogj/go-util/utils" |
| 6 | + "github.com/analogj/lodestone-publisher/pkg/notify" |
| 7 | + "github.com/analogj/lodestone-publisher/pkg/version" |
| 8 | + "github.com/analogj/lodestone-publisher/pkg/watch" |
| 9 | + "github.com/fatih/color" |
| 10 | + "github.com/urfave/cli" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +var goos string |
| 17 | +var goarch string |
| 18 | + |
| 19 | +func main() { |
| 20 | + app := &cli.App{ |
| 21 | + Name: "lodestone-email-publisher", |
| 22 | + Usage: "Email watcher & notifications for lodestone", |
| 23 | + Version: version.VERSION, |
| 24 | + Compiled: time.Now(), |
| 25 | + Authors: []cli.Author{ |
| 26 | + cli.Author{ |
| 27 | + Name: "Jason Kulatunga", |
| 28 | + |
| 29 | + }, |
| 30 | + }, |
| 31 | + Before: func(c *cli.Context) error { |
| 32 | + |
| 33 | + capsuleUrl := "AnalogJ/lodestone-publisher" |
| 34 | + |
| 35 | + versionInfo := fmt.Sprintf("%s.%s-%s", goos, goarch, version.VERSION) |
| 36 | + |
| 37 | + subtitle := capsuleUrl + utils.LeftPad2Len(versionInfo, " ", 53-len(capsuleUrl)) |
| 38 | + |
| 39 | + fmt.Fprintf(c.App.Writer, fmt.Sprintf(utils.StripIndent( |
| 40 | + ` |
| 41 | + __ _____ ____ ____ ___ ____ _____ _ _ ____ |
| 42 | + ( ) ( _ )( _ \( ___)/ __)(_ _)( _ )( \( )( ___) |
| 43 | + )(__ )(_)( )(_) ))__) \__ \ )( )(_)( ) ( )__) |
| 44 | + (____)(_____)(____/(____)(___/ (__) (_____)(_)\_)(____) |
| 45 | + %s |
| 46 | + `), subtitle)) |
| 47 | + return nil |
| 48 | + }, |
| 49 | + |
| 50 | + Commands: []cli.Command{ |
| 51 | + { |
| 52 | + Name: "start", |
| 53 | + Usage: "Start the Lodestone email watcher", |
| 54 | + Action: func(c *cli.Context) error { |
| 55 | + |
| 56 | + var notifyClient notify.Interface |
| 57 | + |
| 58 | + notifyClient = new(notify.AmqpNotify) |
| 59 | + err := notifyClient.Init(map[string]string{ |
| 60 | + "amqp-url": c.String("amqp-url"), |
| 61 | + "exchange": c.String("amqp-exchange"), |
| 62 | + "queue": c.String("amqp-queue"), |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + defer notifyClient.Close() |
| 68 | + |
| 69 | + watcher := watch.EmailWatcher{} |
| 70 | + watcher.Start(notifyClient, map[string]string{ |
| 71 | + "imap-hostname": c.String("imap-hostname"), |
| 72 | + "imap-port": c.String("imap-port"), |
| 73 | + "imap-username": c.String("imap-username"), |
| 74 | + "imap-password": c.String("imap-password"), |
| 75 | + "imap-interval": c.String("imap-interval"), |
| 76 | + "bucket": c.String("bucket"), |
| 77 | + "api-endpoint": c.String("api-endpoint"), |
| 78 | + }) |
| 79 | + return nil |
| 80 | + }, |
| 81 | + |
| 82 | + Flags: []cli.Flag{ |
| 83 | + &cli.StringFlag{ |
| 84 | + Name: "imap-hostname", |
| 85 | + Usage: "The imap server hostname", |
| 86 | + }, |
| 87 | + &cli.StringFlag{ |
| 88 | + Name: "imap-port", |
| 89 | + Usage: "The imap server port", |
| 90 | + Value: "993", |
| 91 | + }, |
| 92 | + &cli.StringFlag{ |
| 93 | + Name: "imap-username", |
| 94 | + Usage: "The imap server username", |
| 95 | + }, |
| 96 | + &cli.StringFlag{ |
| 97 | + Name: "imap-password", |
| 98 | + Usage: "The imap server password", |
| 99 | + }, |
| 100 | + &cli.StringFlag{ |
| 101 | + Name: "imap-interval", |
| 102 | + Usage: "The number of seconds to wait before checking for new messages", |
| 103 | + Value: "600", //10 minutes |
| 104 | + }, |
| 105 | + |
| 106 | + &cli.StringFlag{ |
| 107 | + Name: "api-endpoint", |
| 108 | + Usage: "The api server endpoint", |
| 109 | + Value: "http://webapp:3000", |
| 110 | + }, |
| 111 | + &cli.StringFlag{ |
| 112 | + Name: "bucket", |
| 113 | + Usage: "The name of the bucket", |
| 114 | + }, |
| 115 | + |
| 116 | + &cli.StringFlag{ |
| 117 | + Name: "amqp-url", |
| 118 | + Usage: "The amqp connection string", |
| 119 | + Value: "amqp://guest:guest@localhost:5672", |
| 120 | + }, |
| 121 | + |
| 122 | + &cli.StringFlag{ |
| 123 | + Name: "amqp-exchange", |
| 124 | + Usage: "The amqp exchange", |
| 125 | + Value: "lodestone", |
| 126 | + }, |
| 127 | + |
| 128 | + &cli.StringFlag{ |
| 129 | + Name: "amqp-queue", |
| 130 | + Usage: "The amqp queue", |
| 131 | + Value: "storagelogs", |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + err := app.Run(os.Args) |
| 139 | + if err != nil { |
| 140 | + log.Fatal(color.HiRedString("ERROR: %v", err)) |
| 141 | + } |
| 142 | +} |
0 commit comments