refactor: Decompose key readers
This commit is contained in:
@@ -96,14 +96,9 @@ var archiveCmd = &cobra.Command{
|
||||
lastIndexedBlock = b
|
||||
}
|
||||
|
||||
pubkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pubkey = p
|
||||
pubkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hdrs, err := archive(
|
||||
@@ -398,6 +393,14 @@ func checkKeyAccessible(encryptionFormat string, pathToKey string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readKey(encryptionFormat string, pathToKey string) ([]byte, error) {
|
||||
if encryptionFormat == encryptionFormatNoneKey {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
return ioutil.ReadFile(pathToKey)
|
||||
}
|
||||
|
||||
func checkCompressionLevel(compressionLevel string) error {
|
||||
compressionLevelIsKnown := false
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"archive/tar"
|
||||
"bufio"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/pojntfx/stfs/pkg/controllers"
|
||||
@@ -43,14 +42,9 @@ var deleteCmd = &cobra.Command{
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
pubkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pubkey = p
|
||||
pubkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return delete(
|
||||
|
||||
@@ -3,7 +3,6 @@ package cmd
|
||||
import (
|
||||
"archive/tar"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/pojntfx/stfs/pkg/converters"
|
||||
@@ -36,14 +35,9 @@ var moveCmd = &cobra.Command{
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
pubkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pubkey = p
|
||||
pubkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return move(
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"context"
|
||||
"encoding/base32"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -41,12 +40,6 @@ var recoveryFetchCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
if _, err := os.Stat(viper.GetString(identityFlag)); err != nil {
|
||||
return errIdentityNotAccessible
|
||||
}
|
||||
}
|
||||
|
||||
return checkKeyAccessible(viper.GetString(encryptionFlag), viper.GetString(identityFlag))
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -58,14 +51,9 @@ var recoveryFetchCmd = &cobra.Command{
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
privkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
privkey = p
|
||||
privkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return restoreFromRecordAndBlock(
|
||||
|
||||
@@ -42,14 +42,9 @@ var recoveryIndexCmd = &cobra.Command{
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
privkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
privkey = p
|
||||
privkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return index(
|
||||
|
||||
@@ -34,14 +34,9 @@ var recoveryQueryCmd = &cobra.Command{
|
||||
boil.DebugMode = true
|
||||
}
|
||||
|
||||
privkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
privkey = p
|
||||
privkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return query(
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"archive/tar"
|
||||
"context"
|
||||
"database/sql"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -47,14 +46,9 @@ var restoreCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
privkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
privkey = p
|
||||
privkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(identityFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
headersToRestore := []*models.Header{}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -56,14 +55,9 @@ var updateCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
pubkey := []byte{}
|
||||
if viper.GetString(encryptionFlag) != encryptionFormatNoneKey {
|
||||
p, err := ioutil.ReadFile(viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pubkey = p
|
||||
pubkey, err := readKey(viper.GetString(encryptionFlag), viper.GetString(recipientFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hdrs, err := update(
|
||||
|
||||
Reference in New Issue
Block a user