2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00

return error if RESTIC_COMPRESSION env variable is invalid

This commit is contained in:
Michael Eischer 2022-10-29 22:03:39 +02:00
parent f8910bc4ff
commit 01f0db4e56
2 changed files with 17 additions and 3 deletions

View File

@ -67,9 +67,10 @@ type CompressionMode uint
// Constants for the different compression levels.
const (
CompressionAuto CompressionMode = 0
CompressionOff CompressionMode = 1
CompressionMax CompressionMode = 2
CompressionAuto CompressionMode = 0
CompressionOff CompressionMode = 1
CompressionMax CompressionMode = 2
CompressionInvalid CompressionMode = 3
)
// Set implements the method needed for pflag command flag parsing.
@ -82,6 +83,7 @@ func (c *CompressionMode) Set(s string) error {
case "max":
*c = CompressionMax
default:
*c = CompressionInvalid
return fmt.Errorf("invalid compression mode %q, must be one of (auto|off|max)", s)
}
@ -107,6 +109,10 @@ func (c *CompressionMode) Type() string {
// New returns a new repository with backend be.
func New(be restic.Backend, opts Options) (*Repository, error) {
if opts.Compression == CompressionInvalid {
return nil, errors.Fatalf("invalid compression mode")
}
if opts.PackSize == 0 {
opts.PackSize = DefaultPackSize
}

View File

@ -679,3 +679,11 @@ func testStreamPack(t *testing.T, version uint) {
}
})
}
func TestInvalidCompression(t *testing.T) {
var comp repository.CompressionMode
err := comp.Set("nope")
rtest.Assert(t, err != nil, "missing error")
_, err = repository.New(nil, repository.Options{Compression: comp})
rtest.Assert(t, err != nil, "missing error")
}