Skip to content

Commit

Permalink
♻️ Fix up dump file format detection
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 6, 2022
1 parent e7f380b commit 0038ca3
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 65 deletions.
63 changes: 35 additions & 28 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var conf config.Dump

func init() {
flags.Directory(Command, &conf.Directory)
flags.Format(Command)
flags.Format(Command, &conf.OutputFormat)
flags.IfExists(Command, &conf.IfExists)
flags.Clean(Command, &conf.Clean)
flags.NoOwner(Command, &conf.NoOwner)
Expand All @@ -65,25 +65,17 @@ func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string,
}

func preRun(cmd *cobra.Command, args []string) (err error) {
formatStr, err := cmd.Flags().GetString("format")
if err != nil {
panic(err)
}

conf.OutputFormat, err = sqlformat.ParseFormat(formatStr)
if err != nil {
return err
if len(args) > 0 {
conf.Filename = args[0]
}

return util.DefaultSetup(cmd, &conf.Global)
}
switch conf.Filename {
case "":
if conf.OutputFormat == sqlformat.Unknown {
conf.OutputFormat = sqlformat.Gzip
}

func run(cmd *cobra.Command, args []string) (err error) {
var filename string
if len(args) > 0 {
filename = args[0]
} else {
filename, err = Filename{
conf.Filename, err = Filename{
Dir: conf.Directory,
Namespace: conf.Client.Namespace,
Format: conf.OutputFormat,
Expand All @@ -92,20 +84,35 @@ func run(cmd *cobra.Command, args []string) (err error) {
if err != nil {
return err
}
}

if _, err := os.Stat(filepath.Dir(filename)); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(filename), os.ModePerm)
if err != nil {
return err
case "-":
if conf.OutputFormat == sqlformat.Unknown {
conf.OutputFormat = sqlformat.Plain
}
default:
if conf.OutputFormat == sqlformat.Unknown {
conf.OutputFormat, err = sqlformat.ParseFilename(conf.Filename)
if err != nil {
return err
}
}
}

return util.DefaultSetup(cmd, &conf.Global)
}

func run(cmd *cobra.Command, args []string) (err error) {
var f io.WriteCloser
if filename == "-" {
if conf.Filename == "-" {
f = os.Stdout
} else {
f, err = os.Create(filename)
if _, err := os.Stat(filepath.Dir(conf.Filename)); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(conf.Filename), os.ModePerm)
if err != nil {
return err
}
}

f, err = os.Create(conf.Filename)
if err != nil {
return err
}
Expand All @@ -116,13 +123,13 @@ func run(cmd *cobra.Command, args []string) (err error) {

log.WithFields(log.Fields{
"pod": conf.Pod.Name,
"file": filename,
"file": conf.Filename,
}).Info("exporting database")

if githubActions, err := cmd.Flags().GetBool("github-actions"); err != nil {
panic(err)
} else if githubActions {
fmt.Println("::set-output name=filename::" + filename)
fmt.Println("::set-output name=filename::" + conf.Filename)
}

bar := progressbar.New(-1)
Expand Down Expand Up @@ -200,7 +207,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
}
}

log.WithField("file", filename).Info("dump complete")
log.WithField("file", conf.Filename).Info("dump complete")
return nil
}

Expand Down
35 changes: 11 additions & 24 deletions cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ Supported Input Filetypes:
}

var (
conf config.Restore
inputFormat sqlformat.Format
conf config.Restore
)

func init() {
flags.Format(Command)
flags.Format(Command, &conf.Format)
flags.SingleTransaction(Command, &conf.SingleTransaction)
flags.Clean(Command, &conf.Clean)
flags.NoOwner(Command, &conf.NoOwner)
Expand All @@ -56,25 +55,13 @@ func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string,
return []string{"sql", "sql.gz", "dmp"}, cobra.ShellCompDirectiveFilterFileExt
}

func preRun(cmd *cobra.Command, args []string) error {
formatStr, err := cmd.Flags().GetString("format")
if err != nil {
panic(err)
}

inputFormat, err = sqlformat.ParseFormat(formatStr)
if err != nil {
inputFormat, err = sqlformat.ParseFilename(args[0])
if err != nil {
return err
}
}

func preRun(cmd *cobra.Command, args []string) (err error) {
conf.Filename = args[0]
return util.DefaultSetup(cmd, &conf.Global)
}

func run(cmd *cobra.Command, args []string) (err error) {
f, err := os.Open(args[0])
f, err := os.Open(conf.Filename)
if err != nil {
return err
}
Expand All @@ -83,7 +70,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
pr, pw := io.Pipe()

log.WithFields(log.Fields{
"file": args[0],
"file": conf.Filename,
"pod": conf.Pod.Name,
}).Info("ready to restore database")

Expand All @@ -102,14 +89,14 @@ func run(cmd *cobra.Command, args []string) (err error) {
}

ch := make(chan error, 1)
go runInDatabasePod(pr, ch, inputFormat)
go runInDatabasePod(pr, ch, conf.Format)

bar := progressbar.New(-1)
log.SetOutput(progressbar.NewBarSafeLogger(os.Stderr))

w := io.MultiWriter(pw, bar)

if conf.Clean && inputFormat != sqlformat.Custom {
if conf.Clean && conf.Format != sqlformat.Custom {
dropQuery := conf.Grammar.DropDatabaseQuery(conf.Database)
if dropQuery != "" {
log.Info("cleaning existing data")
Expand All @@ -121,7 +108,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
}

log.Info("restoring database")
switch inputFormat {
switch conf.Format {
case sqlformat.Gzip, sqlformat.Custom:
_, err = io.Copy(w, f)
if err != nil {
Expand All @@ -136,7 +123,7 @@ func run(cmd *cobra.Command, args []string) (err error) {

analyzeQuery := conf.Grammar.AnalyzeQuery()
if analyzeQuery != "" {
if inputFormat == sqlformat.Custom {
if conf.Format == sqlformat.Custom {
_ = pw.Close()

err = <-ch
Expand Down Expand Up @@ -166,7 +153,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
return err
}

log.WithField("file", args[0]).Info("restore complete")
log.WithField("file", conf.Filename).Info("restore complete")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion docs/kubedb_dump.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kubedb dump [filename] [flags]
-c, --clean clean (drop) database objects before recreating (default true)
-T, --exclude-table strings do NOT dump the specified table(s)
-D, --exclude-table-data strings do NOT dump data for the specified table(s)
-F, --format string output file format ([g]zip, [c]ustom, [p]lain) (default "g")
-F, --format string output file format ([g]zip, [c]ustom, [p]lain) (default "gzip")
-h, --help help for dump
--if-exists use IF EXISTS when dropping objects (default true)
-O, --no-owner skip restoration of object ownership in plain-text format (default true)
Expand Down
2 changes: 1 addition & 1 deletion docs/kubedb_restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kubedb restore filename [flags]
```
-c, --clean clean (drop) database objects before recreating (default true)
-f, --force do not prompt before restore
-F, --format string output file format ([g]zip, [c]ustom, [p]lain) (default "g")
-F, --format string output file format ([g]zip, [c]ustom, [p]lain) (default "gzip")
-h, --help help for restore
-O, --no-owner skip restoration of object ownership in plain-text format (default true)
-1, --single-transaction restore as a single transaction (default true)
Expand Down
1 change: 1 addition & 0 deletions internal/config/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/clevyr/kubedb/internal/database/sqlformat"
type Dump struct {
Global
Directory string
Filename string
OutputFormat sqlformat.Format
IfExists bool
Clean bool
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 @@ -4,6 +4,7 @@ import (
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/util"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"os"
"strings"
)
Expand All @@ -20,8 +21,8 @@ func Grammar(cmd *cobra.Command) {
}
}

func Format(cmd *cobra.Command) {
cmd.Flags().StringP("format", "F", "g", "output file format ([g]zip, [c]ustom, [p]lain)")
func Format(cmd *cobra.Command, p flag.Value) {
cmd.Flags().VarP(p, "format", "F", "output file format ([g]zip, [c]ustom, [p]lain)")
err := cmd.RegisterFlagCompletionFunc(
"format",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
4 changes: 4 additions & 0 deletions internal/config/restore.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package config

import "github.com/clevyr/kubedb/internal/database/sqlformat"

type Restore struct {
Global
SingleTransaction bool
Clean bool
NoOwner bool
Force bool
Format sqlformat.Format
Filename string
}
28 changes: 19 additions & 9 deletions internal/database/sqlformat/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,39 @@ import (
"strings"
)

//go:generate stringer -type Format -linecomment

type Format uint8

const (
Unknown = iota
Gzip
Plain
Custom
Unknown Format = iota // unknown
Gzip // gzip
Plain // plain
Custom // custom
)

func (i *Format) Type() string {
return "string"
}

func (i *Format) Set(s string) (err error) {
*i, err = ParseFormat(s)
return err
}

var UnknownFileFormat = errors.New("unknown file format")

func ParseFormat(format string) (Format, error) {
format = strings.ToLower(format)
switch format {
case "gzip", "gz", "g":
case Gzip.String(), "gz", "g":
return Gzip, nil
case "plain", "sql", "p":
case Plain.String(), "sql", "p":
return Plain, nil
case "custom", "c":
case Custom.String(), "c":
return Custom, nil
default:
return Unknown, fmt.Errorf("%w: %s", UnknownFileFormat, format)
}
return Unknown, fmt.Errorf("%w: %s", UnknownFileFormat, format)
}

func ParseFilename(filename string) (Format, error) {
Expand Down
26 changes: 26 additions & 0 deletions internal/database/sqlformat/format_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0038ca3

Please sign in to comment.