refactor: Decompose index func
This commit is contained in:
12
internal/suffix/config.go
Normal file
12
internal/suffix/config.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package suffix
|
||||
|
||||
const (
|
||||
CompressionFormatGZipSuffix = ".gz"
|
||||
CompressionFormatLZ4Suffix = ".lz4"
|
||||
CompressionFormatZStandardSuffix = ".zst"
|
||||
CompressionFormatBrotliSuffix = ".br"
|
||||
CompressionFormatBzip2Suffix = ".bz2"
|
||||
|
||||
EncryptionFormatAgeSuffix = ".age"
|
||||
EncryptionFormatPGPSuffix = ".pgp"
|
||||
)
|
||||
41
internal/suffix/remove.go
Normal file
41
internal/suffix/remove.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package suffix
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pojntfx/stfs/pkg/config"
|
||||
)
|
||||
|
||||
func RemoveSuffix(name string, compressionFormat string, encryptionFormat string) (string, error) {
|
||||
switch encryptionFormat {
|
||||
case config.EncryptionFormatAgeKey:
|
||||
name = strings.TrimSuffix(name, EncryptionFormatAgeSuffix)
|
||||
case config.EncryptionFormatPGPKey:
|
||||
name = strings.TrimSuffix(name, EncryptionFormatPGPSuffix)
|
||||
case config.NoneKey:
|
||||
default:
|
||||
return "", config.ErrUnsupportedEncryptionFormat
|
||||
}
|
||||
|
||||
switch compressionFormat {
|
||||
case config.CompressionFormatGZipKey:
|
||||
fallthrough
|
||||
case config.CompressionFormatParallelGZipKey:
|
||||
name = strings.TrimSuffix(name, CompressionFormatGZipSuffix)
|
||||
case config.CompressionFormatLZ4Key:
|
||||
name = strings.TrimSuffix(name, CompressionFormatLZ4Suffix)
|
||||
case config.CompressionFormatZStandardKey:
|
||||
name = strings.TrimSuffix(name, CompressionFormatZStandardSuffix)
|
||||
case config.CompressionFormatBrotliKey:
|
||||
name = strings.TrimSuffix(name, CompressionFormatBrotliSuffix)
|
||||
case config.CompressionFormatBzip2Key:
|
||||
fallthrough
|
||||
case config.CompressionFormatBzip2ParallelKey:
|
||||
name = strings.TrimSuffix(name, CompressionFormatBzip2Suffix)
|
||||
case config.NoneKey:
|
||||
default:
|
||||
return "", config.ErrUnsupportedCompressionFormat
|
||||
}
|
||||
|
||||
return name, nil
|
||||
}
|
||||
27
internal/tape/read.go
Normal file
27
internal/tape/read.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package tape
|
||||
|
||||
import "os"
|
||||
|
||||
func OpenTapeReadOnly(tape string) (f *os.File, isRegular bool, err error) {
|
||||
fileDescription, err := os.Stat(tape)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
isRegular = fileDescription.Mode().IsRegular()
|
||||
if isRegular {
|
||||
f, err = os.Open(tape)
|
||||
if err != nil {
|
||||
return f, isRegular, err
|
||||
}
|
||||
|
||||
return f, isRegular, nil
|
||||
}
|
||||
|
||||
f, err = os.OpenFile(tape, os.O_RDONLY, os.ModeCharDevice)
|
||||
if err != nil {
|
||||
return f, isRegular, err
|
||||
}
|
||||
|
||||
return f, isRegular, nil
|
||||
}
|
||||
Reference in New Issue
Block a user