feat: Add move command with support for moving children
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/persisters"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,6 +32,10 @@ var archiveCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
metadataPersister := persisters.NewMetadataPersister(viper.GetString(metadataFlag))
|
||||
if err := metadataPersister.Open(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/controllers"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
var ejectCmd = &cobra.Command{
|
||||
@@ -17,6 +18,10 @@ var ejectCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(viper.GetString(tapeFlag), os.O_RDONLY, os.ModeCharDevice)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/persisters"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
var indexCmd = &cobra.Command{
|
||||
@@ -27,6 +28,10 @@ var indexCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
return index(
|
||||
viper.GetString(tapeFlag),
|
||||
viper.GetString(metadataFlag),
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/formatting"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
var listCmd = &cobra.Command{
|
||||
@@ -22,6 +23,10 @@ var listCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
f, isRegular, err := openTapeReadOnly(viper.GetString(tapeFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/pojntfx/stfs/pkg/converters"
|
||||
models "github.com/pojntfx/stfs/pkg/db/sqlite/models/metadata"
|
||||
"github.com/pojntfx/stfs/pkg/formatting"
|
||||
"github.com/pojntfx/stfs/pkg/pax"
|
||||
"github.com/pojntfx/stfs/pkg/persisters"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
var moveCmd = &cobra.Command{
|
||||
Use: "move",
|
||||
Aliases: []string{"m"},
|
||||
Short: "Move a file from tape or tar file and index",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
dirty := false
|
||||
tw, _, cleanup, err := openTapeWriter(viper.GetString(tapeFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cleanup(&dirty)
|
||||
|
||||
metadataPersister := persisters.NewMetadataPersister(viper.GetString(metadataFlag))
|
||||
if err := metadataPersister.Open(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
headersToMove := []*models.Header{}
|
||||
dbhdr, err := metadataPersister.GetHeader(context.Background(), viper.GetString(srcFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
headersToMove = append(headersToMove, dbhdr)
|
||||
|
||||
// If the header refers to a directory, get it's children
|
||||
if dbhdr.Typeflag == tar.TypeDir {
|
||||
dbhdrs, err := metadataPersister.GetHeaderChildren(context.Background(), viper.GetString(srcFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
headersToMove = append(headersToMove, dbhdrs...)
|
||||
}
|
||||
|
||||
// Move the headers in the index
|
||||
if err := metadataPersister.MoveHeaders(context.Background(), headersToMove, viper.GetString(srcFlag), viper.GetString(dstFlag)); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := formatting.PrintCSV(formatting.TARHeaderCSV); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Append deletion headers to the tape/tar file
|
||||
for _, dbhdr := range headersToMove {
|
||||
hdr, err := converters.DBHeaderToTarHeader(dbhdr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hdr.Size = 0 // Don't try to seek after the record
|
||||
hdr.Name = strings.TrimSuffix(viper.GetString(dstFlag), "/") + strings.TrimPrefix(hdr.Name, strings.TrimSuffix(viper.GetString(srcFlag), "/"))
|
||||
hdr.PAXRecords[pax.STFSRecordVersion] = pax.STFSRecordVersion1
|
||||
hdr.PAXRecords[pax.STFSRecordAction] = pax.STFSRecordActionUpdate
|
||||
hdr.PAXRecords[pax.STFSRecordReplacesName] = viper.GetString(srcFlag)
|
||||
|
||||
if err := tw.WriteHeader(hdr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dirty = true
|
||||
|
||||
if err := formatting.PrintCSV(formatting.GetTARHeaderAsCSV(-1, -1, hdr)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
moveCmd.PersistentFlags().IntP(recordSizeFlag, "e", 20, "Amount of 512-bit blocks per record")
|
||||
moveCmd.PersistentFlags().StringP(srcFlag, "s", "", "Current path of the file or directory to move")
|
||||
moveCmd.PersistentFlags().StringP(dstFlag, "d", "", "Path to move the file or directory to")
|
||||
|
||||
viper.AutomaticEnv()
|
||||
|
||||
rootCmd.AddCommand(moveCmd)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/persisters"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
var queryCmd = &cobra.Command{
|
||||
@@ -19,6 +20,10 @@ var queryCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
metadataPersister := persisters.NewMetadataPersister(viper.GetString(metadataFlag))
|
||||
if err := metadataPersister.Open(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/persisters"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -30,6 +31,10 @@ var removeCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
dirty := false
|
||||
tw, _, cleanup, err := openTapeWriter(viper.GetString(tapeFlag))
|
||||
if err != nil {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/formatting"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -29,6 +30,10 @@ var restoreCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
f, isRegular, err := openTapeReadOnly(viper.GetString(tapeFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
const (
|
||||
tapeFlag = "tape"
|
||||
metadataFlag = "metadata"
|
||||
verboseFlag = "verbose"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
@@ -37,6 +38,7 @@ func Execute() {
|
||||
|
||||
rootCmd.PersistentFlags().StringP(tapeFlag, "t", "/dev/nst0", "Tape or tar file to use")
|
||||
rootCmd.PersistentFlags().StringP(metadataFlag, "m", metadataPath, "Metadata database to use")
|
||||
rootCmd.PersistentFlags().BoolP(verboseFlag, "v", false, "Enable verbose logging")
|
||||
|
||||
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/pojntfx/stfs/pkg/controllers"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/volatiletech/sqlboiler/v4/boil"
|
||||
)
|
||||
|
||||
var tellCmd = &cobra.Command{
|
||||
@@ -18,6 +19,10 @@ var tellCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetBool(verboseFlag) {
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(viper.GetString(tapeFlag), os.O_RDONLY, os.ModeCharDevice)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
Reference in New Issue
Block a user