Skip to content

Commit

Permalink
feat(migrate): support migrate data to another storage
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Dec 18, 2023
1 parent 6470e57 commit 8d4ce97
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/archive/backup.go → app/migrate/backup.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package archive
package migrate

import (
"context"
Expand Down
44 changes: 44 additions & 0 deletions app/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package migrate

import (
"context"

"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
"github.com/go-faster/errors"

"github.com/iyear/tdl/pkg/kv"
)

func Migrate(ctx context.Context, to map[string]string) error {
meta, err := kv.From(ctx).MigrateTo()
if err != nil {
return errors.Wrap(err, "read data")
}

dest, err := kv.NewWithMap(to)
if err != nil {
return errors.Wrap(err, "create dest storage")
}

var confirm bool
if err = survey.AskOne(&survey.Confirm{
Message: "It will overwrite the namespace data in the destination storage, continue?",
Default: false,
}, &confirm); err != nil {
return errors.Wrap(err, "confirm")
}
if !confirm {
return nil
}

if err = dest.MigrateFrom(meta); err != nil {
return errors.Wrap(err, "migrate from")
}

color.Green("Migrate successfully.")
for ns := range meta {
color.Green(" - %s", ns)
}
return nil
}
2 changes: 1 addition & 1 deletion app/archive/recover.go → app/migrate/recover.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package archive
package migrate

import (
"bytes"
Expand Down
26 changes: 23 additions & 3 deletions cmd/archive.go → cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package cmd

import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/iyear/tdl/app/archive"
"github.com/iyear/tdl/app/migrate"
"github.com/iyear/tdl/pkg/kv"
)

func NewBackup() *cobra.Command {
Expand All @@ -20,7 +22,7 @@ func NewBackup() *cobra.Command {
dst = fmt.Sprintf("%s.backup.tdl", time.Now().Format("2006-01-02-15_04_05"))
}

return archive.Backup(cmd.Context(), dst)
return migrate.Backup(cmd.Context(), dst)
},
}

Expand All @@ -36,7 +38,7 @@ func NewRecover() *cobra.Command {
Use: "recover",
Short: "Recover your data",
RunE: func(cmd *cobra.Command, args []string) error {
return archive.Recover(cmd.Context(), file)
return migrate.Recover(cmd.Context(), file)
},
}

Expand All @@ -50,3 +52,21 @@ func NewRecover() *cobra.Command {

return cmd
}

func NewMigrate() *cobra.Command {
var to map[string]string

cmd := &cobra.Command{
Use: "migrate",
Short: "Migrate your current data to another storage",
RunE: func(cmd *cobra.Command, args []string) error {
return migrate.Migrate(cmd.Context(), to)
},
}

cmd.Flags().StringToStringVar(&to, "to", map[string]string{},
fmt.Sprintf("destination storage options, format: type=driver,key1=value1,key2=value2. Available drivers: [%s]",
strings.Join(kv.DriverNames(), ",")))

return cmd
}
22 changes: 4 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
)

func New() *cobra.Command {
driverTypeKey := "type"

cmd := &cobra.Command{
Use: "tdl",
Short: "Telegram Downloader, but more than a downloader",
Expand All @@ -43,19 +41,7 @@ func New() *cobra.Command {
zap.String("namespace", ns))
}

// check storage flag
storageOpts := viper.GetStringMapString(consts.FlagStorage)
driver, err := kv.ParseDriver(storageOpts[driverTypeKey])
if err != nil {
return errors.Wrap(err, "parse driver")
}
delete(storageOpts, driverTypeKey)

opts := make(map[string]any)
for k, v := range storageOpts {
opts[k] = v
}
storage, err := kv.New(driver, opts)
storage, err := kv.NewWithMap(viper.GetStringMapString(consts.FlagStorage))
if err != nil {
return errors.Wrap(err, "create kv storage")
}
Expand All @@ -72,11 +58,11 @@ func New() *cobra.Command {
}

cmd.AddCommand(NewVersion(), NewLogin(), NewDownload(), NewForward(),
NewChat(), NewUpload(), NewBackup(), NewRecover(), NewGen())
NewChat(), NewUpload(), NewBackup(), NewRecover(), NewMigrate(), NewGen())

cmd.PersistentFlags().StringToString(consts.FlagStorage, map[string]string{
driverTypeKey: kv.DriverLegacy.String(),
"path": consts.KVPath,
kv.DriverTypeKey: kv.DriverLegacy.String(),
"path": consts.KVPath,
}, fmt.Sprintf("storage options, format: type=driver,key1=value1,key2=value2. Available drivers: [%s]",
strings.Join(kv.DriverNames(), ",")))

Expand Down
17 changes: 17 additions & 0 deletions pkg/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
// ENUM(legacy, bolt, file)
type Driver string

const DriverTypeKey = "type"

var ErrNotFound = errors.New("key not found")

type Meta map[string]map[string][]byte // namespace, key, value
Expand Down Expand Up @@ -46,6 +48,21 @@ func New(driver Driver, opts map[string]any) (Storage, error) {
return nil, errors.Errorf("unsupported driver: %s", driver)
}

func NewWithMap(o map[string]string) (Storage, error) {
driver, err := ParseDriver(o[DriverTypeKey])
if err != nil {
return nil, errors.Wrap(err, "parse driver")
}
delete(o, DriverTypeKey)

opts := make(map[string]any)
for k, v := range o {
opts[k] = v
}

return New(driver, opts)
}

type ctxKey struct{}

func With(ctx context.Context, kv Storage) context.Context {
Expand Down

0 comments on commit 8d4ce97

Please sign in to comment.