refactor: Add automatic indexing for update cmd

This commit is contained in:
Felix Pojtinger
2021-12-13 22:20:07 +01:00
parent f9868aec50
commit d8b4c44fa1
3 changed files with 64 additions and 64 deletions
+11 -58
View File
@@ -1,17 +1,13 @@
package cmd
import (
"archive/tar"
"context"
"fmt"
"github.com/pojntfx/stfs/internal/compression"
"github.com/pojntfx/stfs/internal/keys"
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/internal/persisters"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/operations"
"github.com/pojntfx/stfs/pkg/recovery"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -37,16 +33,6 @@ var updateCmd = &cobra.Command{
return keys.CheckKeyAccessible(viper.GetString(signatureFlag), viper.GetString(identityFlag))
},
RunE: func(cmd *cobra.Command, args []string) error {
metadataPersister := persisters.NewMetadataPersister(viper.GetString(metadataFlag))
if err := metadataPersister.Open(); err != nil {
return err
}
lastIndexedRecord, lastIndexedBlock, err := metadataPersister.GetLastIndexedRecordAndBlock(context.Background(), viper.GetInt(recordSizeFlag))
if err != nil {
return err
}
pubkey, err := keys.ReadKey(viper.GetString(encryptionFlag), viper.GetString(recipientFlag))
if err != nil {
return err
@@ -84,39 +70,16 @@ var updateCmd = &cobra.Command{
}
defer reader.Close()
hdrs, err := operations.Update(
if _, err := operations.Update(
config.DriveWriterConfig{
Drive: writer,
DriveIsRegular: writerIsRegular,
},
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),
viper.GetString(fromFlag),
viper.GetBool(overwriteFlag),
viper.GetString(compressionLevelFlag),
logging.NewLogger().PrintHeader,
)
if err != nil {
return err
}
return recovery.Index(
config.DriveReaderConfig{
config.DriveConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
config.DriveConfig{
config.DriveReaderConfig{
Drive: reader,
DriveIsRegular: readerIsRegular,
},
@@ -135,26 +98,16 @@ var updateCmd = &cobra.Command{
},
viper.GetInt(recordSizeFlag),
int(lastIndexedRecord),
int(lastIndexedBlock),
false,
1, // Ignore the first header, which is the last header which we already indexed
func(hdr *tar.Header, i int) error {
if len(hdrs) <= i {
return config.ErrTarHeaderMissing
}
*hdr = *hdrs[i]
return nil
},
func(hdr *tar.Header, isRegular bool) error {
return nil // We sign above, no need to verify
},
viper.GetString(fromFlag),
viper.GetBool(overwriteFlag),
viper.GetString(compressionLevelFlag),
logging.NewLogger().PrintHeader,
)
); err != nil {
return err
}
return nil
},
}