Skip to content
This repository was archived by the owner on Mar 27, 2020. It is now read-only.

Commit 15ff56c

Browse files
committed
Add "process-mail" CLI command (resolves #8)
1 parent 65e1edb commit 15ff56c

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed

main.go

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,89 @@
11
package main
22

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+
}
486

587
func main() {
6-
fmt.Println("gpg-validation-server")
88+
runApp(os.Args)
789
}

main_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"github.com/codegangsta/cli"
5+
"testing"
6+
)
7+
8+
const okExitCode = 0
9+
10+
func getMockOsExiter(t *testing.T, expectedExitCode int) func(int) {
11+
return func(code int) {
12+
if code != expectedExitCode {
13+
t.Fatalf("Unexpected exit code: expected %d, got %d", expectedExitCode, code)
14+
}
15+
}
16+
}
17+
18+
func testMainWithArguments(t *testing.T, expectedExitCode int, args ...string) {
19+
cli.OsExiter = getMockOsExiter(t, expectedExitCode)
20+
21+
appArgs := append([]string{"name-of-binary"}, args...)
22+
runApp(appArgs)
23+
}
24+
25+
func TestRunMain(t *testing.T) {
26+
testMainWithArguments(t, okExitCode)
27+
}
28+
29+
func TestProcessMailDefault(t *testing.T) {
30+
testMainWithArguments(t, okExitCode, "process-mail")
31+
}
32+
33+
func TestProcessMailFile(t *testing.T) {
34+
testMainWithArguments(t, okExitCode, "process-mail", "--file", "./test-mails/plaintext.eml")
35+
}
36+
37+
func TestProcessFileError(t *testing.T) {
38+
testMainWithArguments(t, errorExitCode, "process-mail", "--file", "./invalid")
39+
}

test-mails/plaintext.eml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Date: Fri, 13 May 2016 13:00:00 -0700
2+
From: Tests <server@server.local>
3+
To: TNG/gpg-validation-server <gpg-validation-server@noreply.github.com>
4+
Cc:
5+
Subject: What a subject
6+
Mime-Version: 1.0
7+
Content-Type: text/plain;
8+
charset=UTF-8
9+
Content-Transfer-Encoding: 7bit
10+
11+
This is some nice plain text!

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -e
44
echo "" > coverage.txt
55

6-
for d in $(find ./* -maxdepth 10 -type d); do
6+
for d in . $(find ./* -maxdepth 10 -type d); do
77
if ls $d/*.go &> /dev/null; then
88
go test -v -coverprofile=profile.out -covermode=atomic $d
99
if [ -f profile.out ]; then

0 commit comments

Comments
 (0)