refactor: Decompose restore func
This commit is contained in:
@@ -1,20 +1,9 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pojntfx/stfs/internal/converters"
|
|
||||||
models "github.com/pojntfx/stfs/internal/db/sqlite/models/metadata"
|
|
||||||
"github.com/pojntfx/stfs/internal/formatting"
|
|
||||||
"github.com/pojntfx/stfs/internal/keys"
|
"github.com/pojntfx/stfs/internal/keys"
|
||||||
"github.com/pojntfx/stfs/internal/persisters"
|
|
||||||
"github.com/pojntfx/stfs/pkg/config"
|
"github.com/pojntfx/stfs/pkg/config"
|
||||||
"github.com/pojntfx/stfs/pkg/recovery"
|
"github.com/pojntfx/stfs/pkg/operations"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||||
@@ -48,11 +37,6 @@ var restoreCmd = &cobra.Command{
|
|||||||
boil.DebugMode = true
|
boil.DebugMode = true
|
||||||
}
|
}
|
||||||
|
|
||||||
metadataPersister := persisters.NewMetadataPersister(viper.GetString(metadataFlag))
|
|
||||||
if err := metadataPersister.Open(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
pubkey, err := readKey(viper.GetString(signatureFlag), viper.GetString(recipientFlag))
|
pubkey, err := readKey(viper.GetString(signatureFlag), viper.GetString(recipientFlag))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -73,91 +57,27 @@ var restoreCmd = &cobra.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
headersToRestore := []*models.Header{}
|
return operations.Restore(
|
||||||
src := strings.TrimSuffix(viper.GetString(fromFlag), "/")
|
config.StateConfig{
|
||||||
dbhdr, err := metadataPersister.GetHeader(context.Background(), src)
|
Drive: viper.GetString(driveFlag),
|
||||||
if err != nil {
|
Metadata: viper.GetString(metadataFlag),
|
||||||
if err == sql.ErrNoRows {
|
},
|
||||||
src = src + "/"
|
config.PipeConfig{
|
||||||
|
Compression: viper.GetString(compressionFlag),
|
||||||
|
Encryption: viper.GetString(encryptionFlag),
|
||||||
|
Signature: viper.GetString(signatureFlag),
|
||||||
|
},
|
||||||
|
config.CryptoConfig{
|
||||||
|
Recipient: recipient,
|
||||||
|
Identity: identity,
|
||||||
|
Password: viper.GetString(passwordFlag),
|
||||||
|
},
|
||||||
|
|
||||||
dbhdr, err = metadataPersister.GetHeader(context.Background(), src)
|
viper.GetInt(recordSizeFlag),
|
||||||
if err != nil {
|
viper.GetString(fromFlag),
|
||||||
return err
|
viper.GetString(toFlag),
|
||||||
}
|
viper.GetBool(flattenFlag),
|
||||||
} else {
|
)
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
headersToRestore = append(headersToRestore, dbhdr)
|
|
||||||
|
|
||||||
// If the header refers to a directory, get it's children
|
|
||||||
if dbhdr.Typeflag == tar.TypeDir {
|
|
||||||
dbhdrs, err := metadataPersister.GetHeaderChildren(context.Background(), src)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
headersToRestore = append(headersToRestore, dbhdrs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, dbhdr := range headersToRestore {
|
|
||||||
if i == 0 {
|
|
||||||
if err := formatting.PrintCSV(formatting.TARHeaderCSV); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
hdr, err := converters.DBHeaderToTarHeader(dbhdr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := formatting.PrintCSV(formatting.GetTARHeaderAsCSV(dbhdr.Record, dbhdr.Lastknownrecord, dbhdr.Block, dbhdr.Lastknownblock, hdr)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
dst := dbhdr.Name
|
|
||||||
if viper.GetString(toFlag) != "" {
|
|
||||||
if viper.GetBool(flattenFlag) {
|
|
||||||
dst = viper.GetString(toFlag)
|
|
||||||
} else {
|
|
||||||
dst = filepath.Join(viper.GetString(toFlag), strings.TrimPrefix(dst, viper.GetString(fromFlag)))
|
|
||||||
|
|
||||||
if strings.TrimSuffix(dst, "/") == strings.TrimSuffix(viper.GetString(toFlag), "/") {
|
|
||||||
dst = filepath.Join(dst, path.Base(dbhdr.Name)) // Append the name so we don't overwrite
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := recovery.Fetch(
|
|
||||||
config.StateConfig{
|
|
||||||
Drive: viper.GetString(driveFlag),
|
|
||||||
Metadata: viper.GetString(metadataFlag),
|
|
||||||
},
|
|
||||||
config.PipeConfig{
|
|
||||||
Compression: viper.GetString(compressionFlag),
|
|
||||||
Encryption: viper.GetString(encryptionFlag),
|
|
||||||
Signature: viper.GetString(signatureFlag),
|
|
||||||
},
|
|
||||||
config.CryptoConfig{
|
|
||||||
Recipient: recipient,
|
|
||||||
Identity: identity,
|
|
||||||
Password: viper.GetString(passwordFlag),
|
|
||||||
},
|
|
||||||
|
|
||||||
viper.GetInt(recordSizeFlag),
|
|
||||||
int(dbhdr.Record),
|
|
||||||
int(dbhdr.Block),
|
|
||||||
dst,
|
|
||||||
false,
|
|
||||||
|
|
||||||
false,
|
|
||||||
); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,19 +4,6 @@ import (
|
|||||||
"github.com/pojntfx/stfs/pkg/config"
|
"github.com/pojntfx/stfs/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Restore(
|
|
||||||
state config.StateConfig,
|
|
||||||
pipes config.PipeConfig,
|
|
||||||
crypto config.CryptoConfig,
|
|
||||||
|
|
||||||
recordSize int,
|
|
||||||
from string,
|
|
||||||
to string,
|
|
||||||
flatten bool,
|
|
||||||
) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Update(
|
func Update(
|
||||||
state config.StateConfig,
|
state config.StateConfig,
|
||||||
pipes config.PipeConfig,
|
pipes config.PipeConfig,
|
||||||
|
|||||||
108
pkg/operations/restore.go
Normal file
108
pkg/operations/restore.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
package operations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pojntfx/stfs/internal/converters"
|
||||||
|
models "github.com/pojntfx/stfs/internal/db/sqlite/models/metadata"
|
||||||
|
"github.com/pojntfx/stfs/internal/formatting"
|
||||||
|
"github.com/pojntfx/stfs/internal/persisters"
|
||||||
|
"github.com/pojntfx/stfs/pkg/config"
|
||||||
|
"github.com/pojntfx/stfs/pkg/recovery"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Restore(
|
||||||
|
state config.StateConfig,
|
||||||
|
pipes config.PipeConfig,
|
||||||
|
crypto config.CryptoConfig,
|
||||||
|
|
||||||
|
recordSize int,
|
||||||
|
from string,
|
||||||
|
to string,
|
||||||
|
flatten bool,
|
||||||
|
) error {
|
||||||
|
metadataPersister := persisters.NewMetadataPersister(state.Metadata)
|
||||||
|
if err := metadataPersister.Open(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
headersToRestore := []*models.Header{}
|
||||||
|
src := strings.TrimSuffix(from, "/")
|
||||||
|
dbhdr, err := metadataPersister.GetHeader(context.Background(), src)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
src = src + "/"
|
||||||
|
|
||||||
|
dbhdr, err = metadataPersister.GetHeader(context.Background(), src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
headersToRestore = append(headersToRestore, dbhdr)
|
||||||
|
|
||||||
|
// If the header refers to a directory, get it's children
|
||||||
|
if dbhdr.Typeflag == tar.TypeDir {
|
||||||
|
dbhdrs, err := metadataPersister.GetHeaderChildren(context.Background(), src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
headersToRestore = append(headersToRestore, dbhdrs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, dbhdr := range headersToRestore {
|
||||||
|
if i == 0 {
|
||||||
|
if err := formatting.PrintCSV(formatting.TARHeaderCSV); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hdr, err := converters.DBHeaderToTarHeader(dbhdr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := formatting.PrintCSV(formatting.GetTARHeaderAsCSV(dbhdr.Record, dbhdr.Lastknownrecord, dbhdr.Block, dbhdr.Lastknownblock, hdr)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dst := dbhdr.Name
|
||||||
|
if to != "" {
|
||||||
|
if flatten {
|
||||||
|
dst = to
|
||||||
|
} else {
|
||||||
|
dst = filepath.Join(to, strings.TrimPrefix(dst, from))
|
||||||
|
|
||||||
|
if strings.TrimSuffix(dst, "/") == strings.TrimSuffix(to, "/") {
|
||||||
|
dst = filepath.Join(dst, path.Base(dbhdr.Name)) // Append the name so we don't overwrite
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := recovery.Fetch(
|
||||||
|
state,
|
||||||
|
pipes,
|
||||||
|
crypto,
|
||||||
|
|
||||||
|
recordSize,
|
||||||
|
int(dbhdr.Record),
|
||||||
|
int(dbhdr.Block),
|
||||||
|
dst,
|
||||||
|
false,
|
||||||
|
|
||||||
|
false,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user