Skip to content

Commit

Permalink
Add “paperboyInfo” method to API
Browse files Browse the repository at this point in the history
  • Loading branch information
rykov committed Jul 25, 2017
1 parent d9fc161 commit 5641ed9
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 26 deletions.
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ to quickly create a Cobra application.`,

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
func Execute(build mail.BuildInfo) {
mail.Config.Build = build
RootCmd.AddCommand(newCmd)
RootCmd.AddCommand(sendCmd)
RootCmd.AddCommand(serverCmd)
Expand Down Expand Up @@ -90,7 +91,7 @@ func loadConfig() error {
if err := viperConfig.ReadInConfig(); err != nil {
return err
}
return viperConfig.Unmarshal(&mail.Config)
return viperConfig.Unmarshal(&mail.Config.ConfigFile)
}

// Error helpers
Expand Down
15 changes: 6 additions & 9 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package cmd

import (
"fmt"
"github.com/rykov/paperboy/mail"
"github.com/spf13/cobra"
"runtime"

"fmt"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Paperboy",
Long: `A longer description...`,
}

func SetVersion(ver, date string) {
versionStr := fmt.Sprintf("v%s %s/%s (%s)", ver, runtime.GOOS, runtime.GOARCH, date)
versionCmd.Run = func(cmd *cobra.Command, args []string) {
fmt.Println("Paperboy Email Engine " + versionStr)
}
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Paperboy Email Engine %s\n", mail.Config.Build)
},
}
25 changes: 24 additions & 1 deletion mail/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package mail

import (
"fmt"
"runtime"
)

// Initial blank config
var Config = config{}

// See https://www.paperboy.email/docs/configuration/
type config struct {
// Version/build
Build BuildInfo

// From config.toml
ConfigFile
}

// See https://www.paperboy.email/docs/configuration/
type ConfigFile struct {
// General
Theme string
From string
Expand Down Expand Up @@ -32,3 +45,13 @@ type smtpConfig struct {
User string
Pass string
}

// Initial blank config
type BuildInfo struct {
Version string
BuildDate string
}

func (i BuildInfo) String() string {
return fmt.Sprintf("v%s %s/%s (%s)", i.Version, runtime.GOOS, runtime.GOARCH, i.BuildDate)
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import "github.com/rykov/paperboy/cmd"
import "github.com/rykov/paperboy/mail"

// Populated by goreleaser
var (
Expand All @@ -24,6 +25,5 @@ var (

// Commands managed by Cobra
func main() {
cmd.SetVersion(version, date)
cmd.Execute()
cmd.Execute(mail.BuildInfo{version, date})
}
18 changes: 18 additions & 0 deletions server/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,21 @@ func (e *renderedEmail) HTML() *string {
out := string(e.msg.HTML)
return &out
}

// ===== Build/Version information =====

func (r *Resolver) PaperboyInfo(ctx context.Context) *paperboyInfo {
return &paperboyInfo{mail.Config.Build}
}

type paperboyInfo struct {
b mail.BuildInfo
}

func (i *paperboyInfo) Version() string {
return i.b.Version
}

func (i *paperboyInfo) BuildDate() string {
return i.b.BuildDate
}
7 changes: 7 additions & 0 deletions server/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const schemaText = `
# The Query type, represents all of the entry points
type Query {
renderOne(content: String!, recipient: String!): RenderedEmail
paperboyInfo: PaperboyInfo!
}
# A single rendered email information
Expand All @@ -40,6 +41,12 @@ const schemaText = `
# html: HTML
}
# Build/version information
type PaperboyInfo {
version: String!
buildDate: String!
}
# HTML (same as string)
scalar HTML
`
65 changes: 53 additions & 12 deletions server/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,22 @@ import (
"os"
"strings"
"testing"
"time"
)

func TestMain(m *testing.M) {
mail.SetFs(afero.NewMemMapFs())
os.Exit(m.Run())
}

func TestSchemaBasicQuery(t *testing.T) {
var fs = mail.AppFs

afero.WriteFile(fs, "config.toml", []byte(""), 0644)
afero.WriteFile(fs, fs.ContentPath("c1.md"), []byte("# Hello"), 0644)
afero.WriteFile(fs, fs.ListPath("r1.yaml"), []byte(`---
- email: ex@example.org
`), 0644)

schema := graphql.MustParseSchema(schemaText, &Resolver{})
response := schema.Exec(context.TODO(), `{
func TestRenderOneQuery(t *testing.T) {
response := issueGraphQLQuery(`{
renderOne(content: "c1", recipient: "r1#0") {
rawMessage
text
html
}
}`, "", map[string]interface{}{})
}`)

if errs := response.Errors; len(errs) > 0 {
t.Fatalf("GraphQL errors %+v", errs)
Expand Down Expand Up @@ -76,3 +68,52 @@ func TestSchemaBasicQuery(t *testing.T) {
t.Errorf("Invalid RawMessage Text: %s", s)
}
}

func TestPaperboyInfoQuery(t *testing.T) {
expected := &mail.Config.Build
expected.BuildDate = time.Now().String()
expected.Version = "1.2.3"

response := issueGraphQLQuery(`{
paperboyInfo {
version
buildDate
}
}`)

if errs := response.Errors; len(errs) > 0 {
t.Fatalf("GraphQL errors %+v", errs)
}

resp := struct {
PaperboyInfo struct {
Version string
BuildDate string
}
}{}

if err := json.Unmarshal(response.Data, &resp); err != nil {
t.Fatalf("JSON unmarshal error: %s", err)
}

actual := resp.PaperboyInfo
if actual.Version != expected.Version {
t.Errorf("Invalid version: %s", actual.Version)
}
if actual.BuildDate != expected.BuildDate {
t.Errorf("Invalid buildDate: %s", actual.BuildDate)
}
}

func issueGraphQLQuery(query string) *graphql.Response {
var fs = mail.AppFs

afero.WriteFile(fs, "config.toml", []byte(""), 0644)
afero.WriteFile(fs, fs.ContentPath("c1.md"), []byte("# Hello"), 0644)
afero.WriteFile(fs, fs.ListPath("r1.yaml"), []byte(`---
- email: ex@example.org
`), 0644)

schema := graphql.MustParseSchema(schemaText, &Resolver{})
return schema.Exec(context.TODO(), query, "", map[string]interface{}{})
}

0 comments on commit 5641ed9

Please sign in to comment.