Skip to content

Commit 5641ed9

Browse files
committed
Add “paperboyInfo” method to API
1 parent d9fc161 commit 5641ed9

File tree

7 files changed

+113
-26
lines changed

7 files changed

+113
-26
lines changed

cmd/root.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ to quickly create a Cobra application.`,
2525

2626
// Execute adds all child commands to the root command sets flags appropriately.
2727
// This is called by main.main(). It only needs to happen once to the rootCmd.
28-
func Execute() {
28+
func Execute(build mail.BuildInfo) {
29+
mail.Config.Build = build
2930
RootCmd.AddCommand(newCmd)
3031
RootCmd.AddCommand(sendCmd)
3132
RootCmd.AddCommand(serverCmd)
@@ -90,7 +91,7 @@ func loadConfig() error {
9091
if err := viperConfig.ReadInConfig(); err != nil {
9192
return err
9293
}
93-
return viperConfig.Unmarshal(&mail.Config)
94+
return viperConfig.Unmarshal(&mail.Config.ConfigFile)
9495
}
9596

9697
// Error helpers

cmd/version.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package cmd
22

33
import (
4-
"fmt"
4+
"github.com/rykov/paperboy/mail"
55
"github.com/spf13/cobra"
6-
"runtime"
6+
7+
"fmt"
78
)
89

910
var versionCmd = &cobra.Command{
1011
Use: "version",
1112
Short: "Print the version number of Paperboy",
1213
Long: `A longer description...`,
13-
}
14-
15-
func SetVersion(ver, date string) {
16-
versionStr := fmt.Sprintf("v%s %s/%s (%s)", ver, runtime.GOOS, runtime.GOARCH, date)
17-
versionCmd.Run = func(cmd *cobra.Command, args []string) {
18-
fmt.Println("Paperboy Email Engine " + versionStr)
19-
}
14+
Run: func(cmd *cobra.Command, args []string) {
15+
fmt.Printf("Paperboy Email Engine %s\n", mail.Config.Build)
16+
},
2017
}

mail/config.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package mail
22

3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
38
// Initial blank config
49
var Config = config{}
510

6-
// See https://www.paperboy.email/docs/configuration/
711
type config struct {
12+
// Version/build
13+
Build BuildInfo
14+
15+
// From config.toml
16+
ConfigFile
17+
}
18+
19+
// See https://www.paperboy.email/docs/configuration/
20+
type ConfigFile struct {
821
// General
922
Theme string
1023
From string
@@ -32,3 +45,13 @@ type smtpConfig struct {
3245
User string
3346
Pass string
3447
}
48+
49+
// Initial blank config
50+
type BuildInfo struct {
51+
Version string
52+
BuildDate string
53+
}
54+
55+
func (i BuildInfo) String() string {
56+
return fmt.Sprintf("v%s %s/%s (%s)", i.Version, runtime.GOOS, runtime.GOARCH, i.BuildDate)
57+
}

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import "github.com/rykov/paperboy/cmd"
18+
import "github.com/rykov/paperboy/mail"
1819

1920
// Populated by goreleaser
2021
var (
@@ -24,6 +25,5 @@ var (
2425

2526
// Commands managed by Cobra
2627
func main() {
27-
cmd.SetVersion(version, date)
28-
cmd.Execute()
28+
cmd.Execute(mail.BuildInfo{version, date})
2929
}

server/resolver.go

+18
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,21 @@ func (e *renderedEmail) HTML() *string {
7878
out := string(e.msg.HTML)
7979
return &out
8080
}
81+
82+
// ===== Build/Version information =====
83+
84+
func (r *Resolver) PaperboyInfo(ctx context.Context) *paperboyInfo {
85+
return &paperboyInfo{mail.Config.Build}
86+
}
87+
88+
type paperboyInfo struct {
89+
b mail.BuildInfo
90+
}
91+
92+
func (i *paperboyInfo) Version() string {
93+
return i.b.Version
94+
}
95+
96+
func (i *paperboyInfo) BuildDate() string {
97+
return i.b.BuildDate
98+
}

server/schema.go

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const schemaText = `
3030
# The Query type, represents all of the entry points
3131
type Query {
3232
renderOne(content: String!, recipient: String!): RenderedEmail
33+
paperboyInfo: PaperboyInfo!
3334
}
3435
3536
# A single rendered email information
@@ -40,6 +41,12 @@ const schemaText = `
4041
# html: HTML
4142
}
4243
44+
# Build/version information
45+
type PaperboyInfo {
46+
version: String!
47+
buildDate: String!
48+
}
49+
4350
# HTML (same as string)
4451
scalar HTML
4552
`

server/schema_test.go

+53-12
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,22 @@ import (
1212
"os"
1313
"strings"
1414
"testing"
15+
"time"
1516
)
1617

1718
func TestMain(m *testing.M) {
1819
mail.SetFs(afero.NewMemMapFs())
1920
os.Exit(m.Run())
2021
}
2122

22-
func TestSchemaBasicQuery(t *testing.T) {
23-
var fs = mail.AppFs
24-
25-
afero.WriteFile(fs, "config.toml", []byte(""), 0644)
26-
afero.WriteFile(fs, fs.ContentPath("c1.md"), []byte("# Hello"), 0644)
27-
afero.WriteFile(fs, fs.ListPath("r1.yaml"), []byte(`---
28-
29-
`), 0644)
30-
31-
schema := graphql.MustParseSchema(schemaText, &Resolver{})
32-
response := schema.Exec(context.TODO(), `{
23+
func TestRenderOneQuery(t *testing.T) {
24+
response := issueGraphQLQuery(`{
3325
renderOne(content: "c1", recipient: "r1#0") {
3426
rawMessage
3527
text
3628
html
3729
}
38-
}`, "", map[string]interface{}{})
30+
}`)
3931

4032
if errs := response.Errors; len(errs) > 0 {
4133
t.Fatalf("GraphQL errors %+v", errs)
@@ -76,3 +68,52 @@ func TestSchemaBasicQuery(t *testing.T) {
7668
t.Errorf("Invalid RawMessage Text: %s", s)
7769
}
7870
}
71+
72+
func TestPaperboyInfoQuery(t *testing.T) {
73+
expected := &mail.Config.Build
74+
expected.BuildDate = time.Now().String()
75+
expected.Version = "1.2.3"
76+
77+
response := issueGraphQLQuery(`{
78+
paperboyInfo {
79+
version
80+
buildDate
81+
}
82+
}`)
83+
84+
if errs := response.Errors; len(errs) > 0 {
85+
t.Fatalf("GraphQL errors %+v", errs)
86+
}
87+
88+
resp := struct {
89+
PaperboyInfo struct {
90+
Version string
91+
BuildDate string
92+
}
93+
}{}
94+
95+
if err := json.Unmarshal(response.Data, &resp); err != nil {
96+
t.Fatalf("JSON unmarshal error: %s", err)
97+
}
98+
99+
actual := resp.PaperboyInfo
100+
if actual.Version != expected.Version {
101+
t.Errorf("Invalid version: %s", actual.Version)
102+
}
103+
if actual.BuildDate != expected.BuildDate {
104+
t.Errorf("Invalid buildDate: %s", actual.BuildDate)
105+
}
106+
}
107+
108+
func issueGraphQLQuery(query string) *graphql.Response {
109+
var fs = mail.AppFs
110+
111+
afero.WriteFile(fs, "config.toml", []byte(""), 0644)
112+
afero.WriteFile(fs, fs.ContentPath("c1.md"), []byte("# Hello"), 0644)
113+
afero.WriteFile(fs, fs.ListPath("r1.yaml"), []byte(`---
114+
115+
`), 0644)
116+
117+
schema := graphql.MustParseSchema(schemaText, &Resolver{})
118+
return schema.Exec(context.TODO(), query, "", map[string]interface{}{})
119+
}

0 commit comments

Comments
 (0)