101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"archive/tar"
|
|
|
|
"github.com/pojntfx/stfs/pkg/config"
|
|
"github.com/pojntfx/stfs/pkg/recovery"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"github.com/volatiletech/sqlboiler/v4/boil"
|
|
)
|
|
|
|
var recoveryIndexCmd = &cobra.Command{
|
|
Use: "index",
|
|
Short: "Index contents of tape or tar file",
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := checkKeyAccessible(viper.GetString(encryptionFlag), viper.GetString(identityFlag)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return checkKeyAccessible(viper.GetString(signatureFlag), viper.GetString(recipientFlag))
|
|
},
|
|
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
|
|
}
|
|
|
|
pubkey, err := readKey(viper.GetString(signatureFlag), viper.GetString(recipientFlag))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
recipient, err := parseSignerRecipient(viper.GetString(signatureFlag), pubkey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
privkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(identityFlag))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
identity, err := parseIdentity(viper.GetString(encryptionFlag), privkey, viper.GetString(passwordFlag))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return recovery.Index(
|
|
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),
|
|
viper.GetInt(recordFlag),
|
|
viper.GetInt(blockFlag),
|
|
viper.GetBool(overwriteFlag),
|
|
|
|
0,
|
|
func(hdr *tar.Header, i int) error {
|
|
return decryptHeader(hdr, viper.GetString(encryptionFlag), identity)
|
|
},
|
|
func(hdr *tar.Header, isRegular bool) error {
|
|
return verifyHeader(hdr, isRegular, viper.GetString(signatureFlag), recipient)
|
|
},
|
|
)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
recoveryIndexCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record")
|
|
recoveryIndexCmd.PersistentFlags().IntP(recordFlag, "k", 0, "Record to seek too before counting")
|
|
recoveryIndexCmd.PersistentFlags().IntP(blockFlag, "b", 0, "Block in record to seek too before counting")
|
|
recoveryIndexCmd.PersistentFlags().BoolP(overwriteFlag, "o", false, "Remove the old index before starting to index")
|
|
recoveryIndexCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key of recipient that has been encrypted for")
|
|
recoveryIndexCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key")
|
|
recoveryIndexCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to the public key to verify with")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
recoveryCmd.AddCommand(recoveryIndexCmd)
|
|
}
|