Skip to content

Commit

Permalink
fix tests and move tmpl from config to app
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 23, 2022
1 parent 983eea3 commit 483d627
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
4 changes: 3 additions & 1 deletion cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
slackCookieEnv = "COOKIE"
)

const defFilenameTemplate = "{{.ID}}{{ if .ThreadTS}}-{{.ThreadTS}}{{end}}"

var build = "dev"

// secrets defines the names of the supported secret files that we load our
Expand Down Expand Up @@ -136,7 +138,7 @@ func parseCmdLine(args []string) (params, error) {
fs.StringVar(&p.appCfg.Output.Filename, "o", "-", "Output `filename` for users and channels. Use '-' for standard\nOutput.")
fs.StringVar(&p.appCfg.Output.Format, "r", "", "report `format`. One of 'json' or 'text'")

fs.StringVar(&p.appCfg.FilenameTemplate, "ft", "{{.ID}}{{ if .ThreadTS}}-{{.ThreadTS}}{{end}}", "output file naming template.")
fs.StringVar(&p.appCfg.FilenameTemplate, "ft", defFilenameTemplate, "output file naming template.")

// options
fs.BoolVar(&p.appCfg.Options.DumpFiles, "f", slackdump.DefOptions.DumpFiles, "same as -download")
Expand Down
14 changes: 8 additions & 6 deletions cmd/slackdump/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ func Test_checkParameters(t *testing.T) {
Token: "x",
Cookie: "d",
},
Input: app.Input{List: []string{}},
Output: app.Output{Filename: "-", Format: "text"},
Options: slackdump.DefOptions,
FilenameTemplate: defFilenameTemplate,
Input: app.Input{List: []string{}},
Output: app.Output{Filename: "-", Format: "text"},
Options: slackdump.DefOptions,
}},
false,
},
Expand All @@ -77,9 +78,10 @@ func Test_checkParameters(t *testing.T) {
Token: "x",
Cookie: "d",
},
Input: app.Input{List: []string{}},
Output: app.Output{Filename: "-", Format: "text"},
Options: slackdump.DefOptions,
FilenameTemplate: defFilenameTemplate,
Input: app.Input{List: []string{}},
Output: app.Output{Filename: "-", Format: "text"},
Options: slackdump.DefOptions,
}},
false,
},
Expand Down
15 changes: 12 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"html/template"
"io"
"os"
"strings"
Expand All @@ -19,14 +20,22 @@ const (
)

type App struct {
sd *slackdump.SlackDumper
sd *slackdump.SlackDumper
tmpl *template.Template

cfg Config
}

// New creates a new slackdump app.
func New(cfg Config) (*App, error) {
return &App{cfg: cfg}, nil
if err := cfg.Validate(); err != nil {
return nil, err
}
tmpl, err := cfg.compileTemplates()
if err != nil {
return nil, err
}
return &App{cfg: cfg, tmpl: tmpl}, nil
}

// init initialises the slack dumper app.
Expand Down Expand Up @@ -107,7 +116,7 @@ func (app *App) newDumpFunc(s string) dumpFunc {

func (app *App) renderFilename(c *slackdump.Conversation) string {
var buf strings.Builder
if err := app.cfg.tmpl.ExecuteTemplate(&buf, filenameTmplName, c); err != nil {
if err := app.tmpl.ExecuteTemplate(&buf, filenameTmplName, c); err != nil {
// this should nevar happen
panic(err)
}
Expand Down
17 changes: 9 additions & 8 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type Config struct {
Latest TimeValue // latest time to dump conversations to

FilenameTemplate string
tmpl *template.Template

Options slackdump.Options
}
Expand Down Expand Up @@ -125,13 +124,15 @@ func (p *Config) Validate() error {
return nil
}

func (p *Config) compileValidateTemplate() error {
var err error
p.tmpl, err = template.New(filenameTmplName).Parse(p.FilenameTemplate)
func (cfg *Config) compileTemplates() (*template.Template, error) {
return template.New(filenameTmplName).Parse(cfg.FilenameTemplate)
}

func (cfg *Config) compileValidateTemplate() error {
tmpl, err := cfg.compileTemplates()
if err != nil {
return err
}

// are you ready for some filth? Here we go!

// let's define some indicators
Expand All @@ -152,15 +153,15 @@ func (p *Config) compileValidateTemplate() error {

// now we render the template and check for OK/NotOK values in the output.
var buf strings.Builder
if err := p.tmpl.ExecuteTemplate(&buf, filenameTmplName, tc); err != nil {
if err := tmpl.ExecuteTemplate(&buf, filenameTmplName, tc); err != nil {
return err
}
if strings.Contains(buf.String(), NotOK) || len(buf.String()) == 0 {
return fmt.Errorf("invalid fields in the template: %q", p.FilenameTemplate)
return fmt.Errorf("invalid fields in the template: %q", cfg.FilenameTemplate)
}
if !strings.Contains(buf.String(), OK) {
// must contain at least one OK
return fmt.Errorf("this does not resolve to anything useful: %q", p.FilenameTemplate)
return fmt.Errorf("this does not resolve to anything useful: %q", cfg.FilenameTemplate)
}
return nil
}
Expand Down
19 changes: 8 additions & 11 deletions internal/app/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"html/template"
"testing"

"github.com/rusq/slackdump"
Expand All @@ -16,7 +15,6 @@ func TestConfig_compileValidateTemplate(t *testing.T) {
Oldest TimeValue
Latest TimeValue
FilenameTemplate string
tmpl *template.Template
Options slackdump.Options
}
tests := []struct {
Expand All @@ -26,42 +24,42 @@ func TestConfig_compileValidateTemplate(t *testing.T) {
}{
{
"id is ok",
fields{FilenameTemplate: "{{.ID}}", tmpl: template.New("")},
fields{FilenameTemplate: "{{.ID}}"},
false,
},
{
"name is ok",
fields{FilenameTemplate: "{{.Name}}", tmpl: template.New("")},
fields{FilenameTemplate: "{{.Name}}"},
false,
},
{
"just threadTS is not ok",
fields{FilenameTemplate: "{{.ThreadTS}}", tmpl: template.New("")},
fields{FilenameTemplate: "{{.ThreadTS}}"},
true,
},
{
"threadTS and message ID is ok",
fields{FilenameTemplate: "{{.ID}}-{{.ThreadTS}}", tmpl: template.New("")},
fields{FilenameTemplate: "{{.ID}}-{{.ThreadTS}}"},
false,
},
{
"threadTS and message ID is ok (conditional)",
fields{FilenameTemplate: "{{.ID}}{{ if .ThreadTS}}-{{.ThreadTS}}{{end}}", tmpl: template.New("")},
fields{FilenameTemplate: "{{.ID}}{{ if .ThreadTS}}-{{.ThreadTS}}{{end}}"},
false,
},
{
"message is not ok",
fields{FilenameTemplate: "{{.Message}}", tmpl: template.New("")},
fields{FilenameTemplate: "{{.Message}}"},
true,
},
{
"unknown field is not ok",
fields{FilenameTemplate: "{{.Who_dis}}", tmpl: template.New("")},
fields{FilenameTemplate: "{{.Who_dis}}"},
true,
},
{
"empty not ok",
fields{FilenameTemplate: "", tmpl: template.New("")},
fields{FilenameTemplate: ""},
true,
},
}
Expand All @@ -75,7 +73,6 @@ func TestConfig_compileValidateTemplate(t *testing.T) {
Oldest: tt.fields.Oldest,
Latest: tt.fields.Latest,
FilenameTemplate: tt.fields.FilenameTemplate,
tmpl: tt.fields.tmpl,
Options: tt.fields.Options,
}
if err := p.compileValidateTemplate(); (err != nil) != tt.wantErr {
Expand Down

0 comments on commit 483d627

Please sign in to comment.