Skip to content

Commit

Permalink
style(dump): Simplify filename generation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 20, 2023
1 parent 0819089 commit 0dd5ea5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 74 deletions.
2 changes: 1 addition & 1 deletion internal/actions/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Dump struct {

func (action Dump) Run(ctx context.Context) (err error) {
if action.Filename == "" {
action.Filename, err = Filename{
action.Filename = Filename{
Namespace: action.Client.Namespace,
Ext: action.Dialect.DumpExtension(action.Format),
Date: time.Now(),
Expand Down
27 changes: 3 additions & 24 deletions internal/actions/dump/generate_filename.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
package dump

import (
"fmt"
"strings"
"text/template"
"time"
)

const DateFormat = "2006-01-02_150405"

var FilenameTemplate = fmt.Sprintf("{{ .Namespace }}_{{ .Date.Format %#v }}{{ .Ext }}", DateFormat)

type Filename struct {
Namespace string
Ext string
Date time.Time
}

func (vars Filename) Generate() (string, error) {
return generate(vars, FilenameTemplate)
}

func generate(vars Filename, tmpl string) (string, error) {
t, err := template.New("filename").Parse(tmpl)
if err != nil {
return "", err
}

var buf strings.Builder
err = t.Execute(&buf, vars)
if err != nil {
return "", err
}

return buf.String(), nil
func (vars Filename) Generate() string {
return vars.Namespace + "_" + vars.Date.Format(DateFormat) + vars.Ext
}

func HelpFilename() string {
filename, _ := Filename{
return Filename{
Namespace: "clevyr",
Ext: ".sql.gz",
Date: time.Date(2022, 1, 9, 9, 41, 0, 0, time.UTC),
}.Generate()
return filename
}
50 changes: 1 addition & 49 deletions internal/actions/dump/generate_filename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ func TestFilename_Generate(t *testing.T) {
Ext: tt.fields.Ext,
Date: tt.fields.Date,
}
got, err := vars.Generate()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
got := vars.Generate()
assert.Equal(t, tt.want, got)
})
}
Expand All @@ -53,46 +48,3 @@ func TestHelpFilename(t *testing.T) {
})
}
}

func Test_generate(t *testing.T) {
type args struct {
vars Filename
tmpl string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
"simple",
args{Filename{"test", ".sql.gz", time.Time{}}, FilenameTemplate},
"test_0001-01-01_000000.sql.gz",
false,
},
{
"invalid template",
args{Filename{"test", ".sql.gz", time.Time{}}, "{{"},
"",
true,
},
{
"invalid function",
args{Filename{"test", ".sql.gz", time.Time{}}, "{{ .NotARealFunction }}"},
"",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := generate(tt.args.vars, tt.args.tmpl)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 0dd5ea5

Please sign in to comment.