|
1 | 1 | package main |
2 | 2 |
|
3 | | -import "fmt" |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"github.com/TNG/gpg-validation-server/mail" |
| 6 | +"github.com/codegangsta/cli" |
| 7 | +"io" |
| 8 | +"log" |
| 9 | +"os" |
| 10 | +) |
| 11 | + |
| 12 | +const errorExitCode = 1 |
| 13 | + |
| 14 | +func appAction(c *cli.Context) error { |
| 15 | +fmt.Println("Args", c.Args()) |
| 16 | +fmt.Println("host", c.String("host")) |
| 17 | +// TODO #11 Start the servers |
| 18 | +return nil |
| 19 | +} |
| 20 | + |
| 21 | +func processMailAction(c *cli.Context) error { |
| 22 | +var err error |
| 23 | +var input io.Reader |
| 24 | + |
| 25 | +file := c.String("file") |
| 26 | + |
| 27 | +if file == "" { |
| 28 | +input = os.Stdin |
| 29 | +} else { |
| 30 | + |
| 31 | +input, err = os.Open(file) |
| 32 | +if err != nil { |
| 33 | +return err |
| 34 | +} |
| 35 | +} |
| 36 | + |
| 37 | +entity, _ := mail.ParseMail(input) |
| 38 | +log.Println(entity) |
| 39 | + |
| 40 | +return nil |
| 41 | +} |
| 42 | + |
| 43 | +func cliErrorHandler(action func(*cli.Context) error) func(*cli.Context) cli.ExitCoder { |
| 44 | +return func(c *cli.Context) cli.ExitCoder { |
| 45 | +if err := action(c); err != nil { |
| 46 | +return cli.NewExitError(fmt.Sprint("Error: ", err), errorExitCode) |
| 47 | +} |
| 48 | +return nil |
| 49 | +} |
| 50 | +} |
| 51 | + |
| 52 | +func runApp(args []string) { |
| 53 | +app := cli.NewApp() |
| 54 | +app.Name = "GPG Validation Service" |
| 55 | +app.Usage = "Run a server that manages email verification and signs verified keys with the servers GPG key." |
| 56 | + |
| 57 | +app.Commands = []cli.Command{ |
| 58 | +{ |
| 59 | +Name: "process-mail", |
| 60 | +Usage: "process an incoming email", |
| 61 | +Action: cliErrorHandler(processMailAction), |
| 62 | +Flags: []cli.Flag{ |
| 63 | +cli.StringFlag{ |
| 64 | +Name: "file", |
| 65 | +Value: "", |
| 66 | +Usage: "`FILE_PATH` of the mail file, omit to read from stdin ", |
| 67 | +}, |
| 68 | +}, |
| 69 | +}, |
| 70 | +} |
| 71 | + |
| 72 | +app.Action = cliErrorHandler(appAction) |
| 73 | +app.Flags = []cli.Flag{ |
| 74 | +cli.StringFlag{ |
| 75 | +Name: "host", |
| 76 | +Value: "localhost", |
| 77 | +Usage: "`HOST` of the mail server", |
| 78 | +}, |
| 79 | +} |
| 80 | + |
| 81 | +err := app.Run(args) |
| 82 | +if err != nil { |
| 83 | +cli.OsExiter(errorExitCode) |
| 84 | +} |
| 85 | +} |
4 | 86 |
|
5 | 87 | func main() { |
6 | | -fmt.Println("gpg-validation-server") |
| 88 | +runApp(os.Args) |
7 | 89 | } |
0 commit comments