diff --git a/internal/archive/archive.go b/internal/archive/archive.go index a0e3bee..0eb4554 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -68,6 +68,43 @@ type archiveItems struct { sync.RWMutex } +// ExtractFormatLevel parses the given string in the format format[:level], +// where :level is one of CompressLevel consts. If level is omitted from the +// string, or if it can't be parsed, the level is set to the default level for +// the given format. If format is unknown, gzip is selected. This function is +// designed to always return something usable within this package. +func ExtractFormatLevel(s string) (format CompressFormat, level CompressLevel) { + + f, l, found := strings.Cut(s, ":") + if !found { + l = "default" + } + + level = CompressLevel(strings.ToLower(l)) + format = CompressFormat(strings.ToLower(f)) + switch level { + + } + switch level { + case LevelBest: + case LevelDefault: + case LevelFast: + default: + log.Print("Unknown or no compression level set, using default") + level = LevelDefault + } + + switch format { + case FormatGzip: + case FormatZstd: + default: + log.Print("Unknown or no compression format set, using gzip") + format = FormatGzip + } + + return +} + // Adds the given item to the archiveItems, only if it doesn't already exist in // the list. The items are kept sorted in ascending order. func (a *archiveItems) add(item archiveItem) { diff --git a/internal/archive/archive_test.go b/internal/archive/archive_test.go index 378ba6c..110b177 100644 --- a/internal/archive/archive_test.go +++ b/internal/archive/archive_test.go @@ -187,3 +187,74 @@ func TestArchiveItemsAdd(t *testing.T) { }) } } + +func TestExtractFormatLevel(t *testing.T) { + tests := []struct { + name string + in string + expectedFormat CompressFormat + expectedLevel CompressLevel + }{ + { + name: "gzip, default level", + in: "gzip:default", + expectedFormat: FormatGzip, + expectedLevel: LevelDefault, + }, + { + name: "unknown format, level 12", + in: "pear:12", + expectedFormat: FormatGzip, + expectedLevel: LevelDefault, + }, + { + name: "zstd, level not given", + in: "zstd", + expectedFormat: FormatZstd, + expectedLevel: LevelDefault, + }, + { + name: "zstd, invalid level 'fast:'", + in: "zstd:fast:", + expectedFormat: FormatZstd, + expectedLevel: LevelDefault, + }, + { + name: "zstd, best", + in: "zstd:best", + expectedFormat: FormatZstd, + expectedLevel: LevelBest, + }, + { + name: "zstd, level empty :", + in: "zstd:", + expectedFormat: FormatZstd, + expectedLevel: LevelDefault, + }, + { + name: "gzip, best", + in: "gzip:best", + expectedFormat: FormatGzip, + expectedLevel: LevelBest, + }, + { + name: "none, none", + in: "", + expectedFormat: FormatGzip, + expectedLevel: LevelDefault, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + format, level := ExtractFormatLevel(test.in) + if format != test.expectedFormat { + t.Fatal("format expected: ", test.expectedFormat, " got: ", format) + } + if level != test.expectedLevel { + t.Fatal("level expected: ", test.expectedLevel, " got: ", level) + } + + }) + } +}