Skip to content

Commit

Permalink
♻️ Clean up directory flag code
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 6, 2022
1 parent 12e57cb commit 99c24e2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
24 changes: 12 additions & 12 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,26 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
conf.Filename = args[0]
}

switch conf.Filename {
case "":
if conf.Format == sqlformat.Unknown {
conf.Format = sqlformat.Gzip
if conf.Directory != "" {
cmd.SilenceUsage = true
if err = os.Chdir(conf.Directory); err != nil {
return err
}
cmd.SilenceUsage = false
}

switch conf.Filename {
case "":
conf.Filename, err = Filename{
Dir: conf.Directory,
Namespace: conf.Client.Namespace,
Format: conf.Format,
Date: time.Now(),
}.Generate()
if err != nil {
return err
}
case "-":
if conf.Format == sqlformat.Unknown {
conf.Format = sqlformat.Plain
}
default:
if conf.Format == sqlformat.Unknown {
if !cmd.Flags().Lookup("format").Changed {
conf.Format, err = sqlformat.ParseFilename(conf.Filename)
if err != nil {
return err
Expand All @@ -102,9 +101,10 @@ func preRun(cmd *cobra.Command, args []string) (err error) {

func run(cmd *cobra.Command, args []string) (err error) {
var f io.WriteCloser
if conf.Filename == "-" {
switch conf.Filename {
case "-":
f = os.Stdout
} else {
default:
if _, err := os.Stat(filepath.Dir(conf.Filename)); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(conf.Filename), os.ModePerm)
if err != nil {
Expand Down
11 changes: 4 additions & 7 deletions cmd/dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,33 @@ package dump
import (
"fmt"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"path/filepath"
"strings"
"testing"
"time"
)

func TestGenerateFilename(t *testing.T) {
testCases := []struct {
directory string
namespace string
filetype sqlformat.Format
err error
}{
{".", "test", sqlformat.Gzip, nil},
{"/home/test", "another", sqlformat.Plain, nil},
{"test", sqlformat.Gzip, nil},
{"another", sqlformat.Plain, nil},
}
for _, tc := range testCases {
tc := tc // capture range variable
t.Run(fmt.Sprintf("%v in %v to %v with error %v", tc.directory, tc.namespace, tc.filetype, tc.err), func(t *testing.T) {
t.Run(fmt.Sprintf("%v to %v with error %v", tc.namespace, tc.filetype, tc.err), func(t *testing.T) {
t.Parallel()
filename, err := Filename{
Dir: tc.directory,
Namespace: tc.namespace,
Format: tc.filetype,
Date: time.Now(),
}.Generate()
if err != tc.err {
t.Error(err)
}
expected := filepath.Clean(tc.directory + "/" + tc.namespace + "_")
expected := tc.namespace + "_"
if !strings.HasPrefix(filename, expected) {
t.Errorf("got %v; expected %#v", filename, expected)
}
Expand Down
7 changes: 1 addition & 6 deletions cmd/dump/generate_filename.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dump
import (
"fmt"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"path/filepath"
"strings"
"text/template"
"time"
Expand All @@ -14,7 +13,6 @@ const DateFormat = "2006-01-02_150405"
var FilenameTemplate = fmt.Sprintf("{{ .Namespace }}_{{ .Date.Format %#v }}{{ .Ext }}", DateFormat)

type Filename struct {
Dir string
Namespace string
Format sqlformat.Format
Date time.Time
Expand All @@ -29,8 +27,6 @@ func (vars Filename) Ext() string {
}

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

t, err := template.New("filename").Parse(FilenameTemplate)
if err != nil {
return "", err
Expand All @@ -42,12 +38,11 @@ func (vars Filename) Generate() (string, error) {
return "", err
}

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

func HelpFilename() string {
filename, _ := Filename{
Dir: ".",
Namespace: "clevyr",
Format: sqlformat.Gzip,
Date: time.Date(2022, 1, 9, 9, 41, 0, 0, time.UTC),
Expand Down
5 changes: 3 additions & 2 deletions internal/config/flags/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package flags

import (
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"github.com/clevyr/kubedb/internal/util"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"os"
"strings"
)
Expand All @@ -21,7 +21,8 @@ func Grammar(cmd *cobra.Command) {
}
}

func Format(cmd *cobra.Command, p flag.Value) {
func Format(cmd *cobra.Command, p *sqlformat.Format) {
*p = sqlformat.Gzip
cmd.Flags().VarP(p, "format", "F", "output file format ([g]zip, [c]ustom, [p]lain)")
err := cmd.RegisterFlagCompletionFunc(
"format",
Expand Down

0 comments on commit 99c24e2

Please sign in to comment.