Skip to content

Commit

Permalink
♻️ Refactor filename generation, make filename dynamic in dump help text
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 10, 2022
1 parent 5ff6e44 commit 88118a0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
40 changes: 6 additions & 34 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dump

import (
"bytes"
"errors"
"fmt"
"github.com/clevyr/kubedb/internal/config"
Expand All @@ -17,8 +16,6 @@ import (
"os"
"path/filepath"
"strings"
"text/template"
"time"
)

var Command = &cobra.Command{
Expand All @@ -29,7 +26,7 @@ var Command = &cobra.Command{
If no filename is provided, the filename will be generated.
For example, if a dump is performed in the namespace "clevyr" with no extra flags,
the generated filename might look like "clevyr_2022-04-01_094100.sql.gz"`,
the generated filename might look like "` + HelpFilename() + `"`,

Args: cobra.MaximumNArgs(1),
ValidArgsFunction: validArgs,
Expand Down Expand Up @@ -77,7 +74,11 @@ func run(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
filename = args[1]
} else {
filename, err = generateFilename(conf.Directory, conf.Client.Namespace, conf.OutputFormat)
filename, err = Filename{
Dir: conf.Directory,
Namespace: conf.Client.Namespace,
Format: conf.OutputFormat,
}.Generate()
if err != nil {
return err
}
Expand Down Expand Up @@ -157,35 +158,6 @@ func run(cmd *cobra.Command, args []string) (err error) {
return nil
}

func generateFilename(directory, namespace string, outputFormat sqlformat.Format) (string, error) {
directory = filepath.Clean(directory)
t, err := template.
New("filename").
Parse("{{.directory}}/{{.namespace}}_{{.now.Format \"2006-01-02_150405\"}}")
if err != nil {
return "", err
}

var tpl bytes.Buffer
data := map[string]interface{}{
"directory": directory,
"namespace": namespace,
"now": time.Now(),
}
err = t.Execute(&tpl, data)
if err != nil {
return "", err
}

ext, err := sqlformat.WriteExtension(outputFormat)
if err != nil {
return "", err
}
tpl.WriteString(ext)

return tpl.String(), err
}

func buildCommand(db config.Databaser, conf config.Dump) []string {
cmd := db.DumpCommand(conf)
if conf.OutputFormat != sqlformat.Custom {
Expand Down
58 changes: 58 additions & 0 deletions cmd/dump/generate_filename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dump

import (
"bytes"
"fmt"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"path/filepath"
"text/template"
"time"
)

const DateFormat = "2006-01-02_150405"

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

type Filename struct {
Dir string
Namespace string
Format sqlformat.Format
}

func (vars Filename) Now(layout string) string {
return time.Now().Format(layout)
}

func (vars Filename) Ext() string {
ext, err := sqlformat.WriteExtension(vars.Format)
if err != nil {
panic(err)
}
return ext
}

func (vars Filename) Generate() (string, error) {
vars.Dir = filepath.Clean(vars.Dir)

t, err := template.New("filename").Parse(FilenameTemplate)
if err != nil {
return "", err
}

var tpl bytes.Buffer
err = t.Execute(&tpl, vars)
if err != nil {
return "", err
}

return filepath.Join(vars.Dir, tpl.String()), nil
}

func HelpFilename() string {
filename, _ := Filename{
Dir: ".",
Namespace: "clevyr",
Format: sqlformat.Gzip,
}.Generate()
return filename
}

0 comments on commit 88118a0

Please sign in to comment.