diff --git a/.goreleaser.yaml b/.goreleaser.yaml index d74bf1ce..362d408b 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -139,25 +139,11 @@ release: name_template: "{{.ProjectName}} v{{.Version}}" header: | - To update: - ``` - mangal update - ``` - To install: ```sh curl -sSL mangal.metafates.one/install | sh ``` -
- If you have any problems updating - Run the install script (you can run it multiple times): - - ```sh - curl -sSL mangal.metafates.one/install | sh - ``` -
- ## What's new? footer: | diff --git a/CHANGELOG.md b/CHANGELOG.md index 5348660b..9edf7396 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## 3.14.1 + +- Mark flags as required for `inline anilist` commands +- Remove `update` command [Why?](https://github.com/metafates/mangal/discussions/112) +- `mangal version` will notify if new version is available +- Use correct page image extension for custom sources #110 + ## 3.14.0 - New commands related to the anilist manga linkage. Now you can set what anilist manga should be linked with what titles by id. See `mangal inline anilist` for more information. #106 diff --git a/README.md b/README.md index 26e87e3a..120a6b3c 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,6 @@ the official bucket) Thanks to [@T-Dynamos](https://github.com/T-Dynamos) for adding it to the [termux-packages](https://github.com/termux/termux-packages) -> `mangal update` command may not work properly - ```shell pkg install mangal ``` @@ -93,8 +91,6 @@ pkg install mangal Install using [Nix](https://nixos.org/download.html#download-nix). Thanks to [@bertof](https://github.com/bertof) for adding it to the [nixpkgs](https://github.com/NixOS/nixpkgs) -> `mangal update` command may not work properly - ```shell # NixOS nix-env -iA nixos.mangal diff --git a/cmd/inline.go b/cmd/inline.go index 0a936a6e..72c11a02 100644 --- a/cmd/inline.go +++ b/cmd/inline.go @@ -44,19 +44,19 @@ var inlineCmd = &cobra.Command{ Manga selectors: first - first manga in the list last - last manga in the list - [number] - select manga by index + [number] - select manga by index (starting from 0) Chapter selectors: first - first chapter in the list last - last chapter in the list all - all chapters in the list - [number] - select chapter by index + [number] - select chapter by index (starting from 0) [from]-[to] - select chapters by range @[substring]@ - select chapters by name substring When using the json flag manga selector could be omitted. That way, it will select all mangas`, - Example: "mangal inline --source Manganelo --query \"death note\" --manga first --chapters \"@Vol.1 @\" -d", + Example: "https://github.com/metafates/mangal/wiki/Inline-mode", PreRun: func(cmd *cobra.Command, args []string) { json, _ := cmd.Flags().GetBool("json") @@ -146,6 +146,11 @@ func init() { var inlineAnilistSearchCmd = &cobra.Command{ Use: "search", Short: "Search anilist manga by name", + PreRun: func(cmd *cobra.Command, args []string) { + if !cmd.Flags().Changed("name") && !cmd.Flags().Changed("id") { + handleErr(errors.New("name or id flag is required")) + } + }, Run: func(cmd *cobra.Command, args []string) { mangaName := lo.Must(cmd.Flags().GetString("name")) mangaId := lo.Must(cmd.Flags().GetInt("id")) @@ -169,7 +174,8 @@ var inlineAnilistSearchCmd = &cobra.Command{ func init() { inlineAnilistCmd.AddCommand(inlineAnilistGetCmd) - inlineAnilistGetCmd.Flags().StringP("name", "n", "", "manga name to get bind for") + inlineAnilistGetCmd.Flags().StringP("name", "n", "", "manga name to get the bind for") + lo.Must0(inlineAnilistGetCmd.MarkFlagRequired("name")) } var inlineAnilistGetCmd = &cobra.Command{ @@ -195,6 +201,9 @@ func init() { inlineAnilistBindCmd.Flags().StringP("name", "n", "", "manga name") inlineAnilistBindCmd.Flags().IntP("id", "i", 0, "anilist manga id") + lo.Must0(inlineAnilistBindCmd.MarkFlagRequired("name")) + lo.Must0(inlineAnilistBindCmd.MarkFlagRequired("id")) + inlineAnilistBindCmd.MarkFlagsRequiredTogether("name", "id") } diff --git a/cmd/update.go b/cmd/update.go deleted file mode 100644 index a4ea0701..00000000 --- a/cmd/update.go +++ /dev/null @@ -1,21 +0,0 @@ -package cmd - -import ( - "github.com/metafates/mangal/updater" - "github.com/spf13/cobra" - "os" -) - -func init() { - rootCmd.AddCommand(updateCmd) - updateCmd.SetOut(os.Stdout) -} - -var updateCmd = &cobra.Command{ - Use: "update", - Short: "Update mangal", - Aliases: []string{"upgrade"}, - Run: func(cmd *cobra.Command, args []string) { - handleErr(updater.Update()) - }, -} diff --git a/cmd/version.go b/cmd/version.go index 81c60660..7f5241cc 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,14 +1,17 @@ package cmd import ( + "fmt" + "github.com/metafates/mangal/icon" "github.com/metafates/mangal/style" + "github.com/metafates/mangal/updater" + "github.com/metafates/mangal/util" "os" "runtime" "strings" "text/template" "github.com/metafates/mangal/constant" - "github.com/metafates/mangal/updater" "github.com/spf13/cobra" ) @@ -22,57 +25,60 @@ var versionCmd = &cobra.Command{ Short: "Print the version number of mangal", Long: `All software has versions. This is mangal's`, Run: func(cmd *cobra.Command, args []string) { - var installedWith string + erase := util.PrintErasable(fmt.Sprintf("%s Checking if new version is available...", icon.Get(icon.Progress))) + var newVersion string - switch updater.DetectInstallationMethod() { - case updater.Homebrew: - installedWith = "Homebrew" - case updater.Scoop: - installedWith = "Scoop" - case updater.Termux: - installedWith = "Termux" - case updater.Standalone: - installedWith = "Standalone" - case updater.Go: - installedWith = "From source (" + runtime.Version() + ")" - default: - installedWith = "Unknown" + version, err := updater.LatestVersion() + if err == nil { + comp, err := util.CompareVersions(constant.Version, version) + if err == nil && comp == -1 { + newVersion = version + } } + erase() + versionInfo := struct { - Version string - InstalledWith string - OS string - Arch string - BuiltAt string - BuiltBy string - Revision string - App string + Version string + OS string + Arch string + BuiltAt string + BuiltBy string + Revision string + App string + NewVersion string }{ - Version: constant.Version, - App: constant.Mangal, - InstalledWith: installedWith, - OS: runtime.GOOS, - Arch: runtime.GOARCH, - BuiltAt: strings.TrimSpace(constant.BuiltAt), - BuiltBy: constant.BuiltBy, - Revision: constant.Revision, + Version: constant.Version, + App: constant.Mangal, + OS: runtime.GOOS, + Arch: runtime.GOARCH, + BuiltAt: strings.TrimSpace(constant.BuiltAt), + BuiltBy: constant.BuiltBy, + Revision: constant.Revision, + NewVersion: newVersion, } t, err := template.New("version").Funcs(map[string]any{ "faint": style.Faint, "bold": style.Bold, "magenta": style.Magenta, + "green": style.Green, "repeat": strings.Repeat, + "concat": func(a, b string) string { + return a + b + }, }).Parse(`{{ magenta "▇▇▇" }} {{ magenta .App }} {{ faint "Version" }} {{ bold .Version }} {{ faint "Git Commit" }} {{ bold .Revision }} {{ faint "Build Date" }} {{ bold .BuiltAt }} {{ faint "Built By" }} {{ bold .BuiltBy }} - {{ faint "Installed With" }} {{ bold .InstalledWith }} {{ faint "Platform" }} {{ bold .OS }}/{{ bold .Arch }} -`) + +{{ if not (eq .NewVersion "") }} +{{ green "▇▇▇" }} New version available {{ bold .NewVersion }} +{{ faint (concat "https://github.com/metafates/mangal/releases/tag/v" .NewVersion) }} +{{ end }}`) handleErr(err) handleErr(t.Execute(cmd.OutOrStdout(), versionInfo)) }, diff --git a/constant/meta.go b/constant/meta.go index e00d6ffc..352cfe37 100644 --- a/constant/meta.go +++ b/constant/meta.go @@ -2,6 +2,6 @@ package constant const ( Mangal = "mangal" - Version = "3.14.0" + Version = "3.14.1" UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" ) diff --git a/provider/custom/translator.go b/provider/custom/translator.go index f9bc8e1c..7f0e687d 100644 --- a/provider/custom/translator.go +++ b/provider/custom/translator.go @@ -6,6 +6,7 @@ import ( "github.com/samber/lo" lua "github.com/yuin/gopher-lua" "net/url" + "path/filepath" "strconv" "strings" ) @@ -133,14 +134,6 @@ func pageFromTable(table *lua.LTable, chapter *source.Chapter) (page *source.Pag page.Index = uint16(num) return nil }}, - "extension": {A: lua.LTString, B: false, C: func(v string) error { - if !strings.HasPrefix(v, ".") { - v = "." + v - } - - page.Extension = v - return nil - }, D: ".jpg"}, } err = translate(table, mappings) @@ -148,6 +141,7 @@ func pageFromTable(table *lua.LTable, chapter *source.Chapter) (page *source.Pag return } + page.Extension = filepath.Ext(page.URL) chapter.Pages = append(chapter.Pages, page) return } diff --git a/updater/detect.go b/updater/detect.go deleted file mode 100644 index 1764d715..00000000 --- a/updater/detect.go +++ /dev/null @@ -1,87 +0,0 @@ -package updater - -import ( - "os" - "os/exec" - "path/filepath" - "strings" -) - -type InstallationMethod int - -const ( - Go InstallationMethod = iota - Homebrew - Scoop - Termux - Standalone -) - -// DetectInstallationMethod detects the installation method. -func DetectInstallationMethod() InstallationMethod { - switch { - case isUnderScoop(): - return Scoop - case isUnderHomebrew(): - return Homebrew - case isUnderTermux(): - return Termux - case isUnderGo(): - return Go - default: - return Standalone - } -} - -// isUnderTermux returns true if mangal is running under Termux. -func isUnderTermux() (ok bool) { - return has("termux-setup-storage") -} - -// isUnderHomebrew returns true if mangal is running under Homebrew. -func isUnderHomebrew() (ok bool) { - if !has("brew") { - return - } - - path, err := os.Executable() - if err != nil { - return false - } - - return strings.Contains(path, filepath.Join("homebrew", "bin")) -} - -// isUnderScoop returns true if mangal is running under Scoop. -func isUnderScoop() (ok bool) { - if !has("Scoop") { - return false - } - - path, err := os.Executable() - if err != nil { - return false - } - - return strings.Contains(path, filepath.Join("Scoop", "shims")) -} - -// isUnderGo returns true if mangal is running under Go. -func isUnderGo() (ok bool) { - if !has("go") { - return false - } - - path, err := os.Executable() - if err != nil { - return false - } - - return strings.Contains(path, filepath.Join("go", "bin")) -} - -// has returns true if the command exists. -func has(command string) bool { - ok, err := exec.LookPath(command) - return err == nil && ok != "" -} diff --git a/updater/update.go b/updater/update.go deleted file mode 100644 index 6a6d96cd..00000000 --- a/updater/update.go +++ /dev/null @@ -1,270 +0,0 @@ -package updater - -import ( - "errors" - "fmt" - "github.com/metafates/mangal/constant" - "github.com/metafates/mangal/filesystem" - "github.com/metafates/mangal/icon" - "github.com/metafates/mangal/log" - "github.com/metafates/mangal/open" - "github.com/metafates/mangal/style" - "github.com/metafates/mangal/util" - "github.com/metafates/mangal/where" - "io" - "net/http" - "os" - "os/exec" - "path/filepath" - "runtime" -) - -var erase func() - -func info(format string, args ...any) (erase func()) { - return util.PrintErasable( - fmt.Sprintf( - "%s %s", - icon.Get(icon.Progress), - fmt.Sprintf(format, args...), - ), - ) -} - -// Update updates mangal to the latest version. -func Update() (err error) { - erase = info("Fetching latest version") - version, err := LatestVersion() - if err != nil { - return - } - - erase() - - comp, err := util.CompareVersions(constant.Version, version) - if err != nil { - return err - } - - if comp >= 0 { - fmt.Printf( - "%s %s %s\n", - style.Green("Congrats!"), - "You're already on the latest version of "+constant.Mangal, - style.Faint("(which is "+constant.Version+")"), - ) - return - } - - fmt.Printf("%s %s is out! You're on %s\n", style.Bold(constant.Mangal), style.Cyan(version), style.Blue(constant.Version)) - - method := DetectInstallationMethod() - - switch method { - case Homebrew: - erase = info("Homebrew installation detected") - err = updateHomebrew() - case Scoop: - erase = info("Scoop installation detected") - err = updateScoop() - case Termux: - releaseURL := fmt.Sprintf("https://github.com/metafates/mangal/releases/tag/v%s", version) - err = fmt.Errorf("self-updating is not supported on termux yet, please update mangal manually. %s", releaseURL) - _ = open.Start(releaseURL) - case Go, Standalone: - erase = info("Non-package manager installation detected") - err = update() - default: - err = errors.New("unknown installation method, can't update") - } - - if err != nil { - return - } - - fmt.Printf(` -%s - -Report any bugs: - - %s - -What's new: - - %s - -Changelog: - - %s -`, - style.Combined(style.Bold, style.Green)("Welcome to mangal v"+version), - style.Faint("https://github.com/metafates/mangal/issues"), - style.Cyan("https://github.com/metafates/mangal/releases/tag/v"+version), - style.Faint(fmt.Sprintf("https://github.com/metafates/mangal/compare/v%s...v%s", constant.Version, version)), - ) - - return -} - -// updateHomebrew updates mangal using Homebrew. -func updateHomebrew() (err error) { - erase() - info("Running %s", style.Yellow("brew upgrade mangal")) - cmd := exec.Command("brew", "upgrade", constant.Mangal) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} - -// updateScoop updates mangal using Scoop. -func updateScoop() (err error) { - erase() - info("Running %s", style.Yellow("scoop update mangal")) - cmd := exec.Command("Scoop", "update", constant.Mangal) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} - -// update mangal by downloading it directly. -func update() (err error) { - erase() - log.Info("Updating mangal to the latest version") - - var ( - version string - arch string - selfPath string - archiveName string - archiveType string - ) - - if selfPath, err = os.Executable(); err != nil { - return - } - - if version, err = LatestVersion(); err != nil { - return - } - - switch runtime.GOARCH { - case "amd64": - arch = "x86_64" - case "386": - arch = "i386" - default: - arch = runtime.GOARCH - } - - switch runtime.GOOS { - case constant.Windows: - archiveType = "zip" - default: - archiveType = "tar.gz" - } - - archiveName = fmt.Sprintf("%s_%s_%s_%s.%s", constant.Mangal, version, util.Capitalize(runtime.GOOS), arch, archiveType) - url := fmt.Sprintf( - "https://github.com/metafates/%s/releases/download/v%s/%s", - constant.Mangal, - version, - archiveName, - ) - - erase = info("Downloading %s", style.Yellow(url)) - - res, err := http.Get(url) - if err != nil { - log.Error(err) - return err - } - - if res.StatusCode != http.StatusOK { - err = fmt.Errorf("error downloading binary: status code %d", res.StatusCode) - log.Error(err) - return - } - - defer util.Ignore(res.Body.Close) - - erase() - erase = info("Extracting %s", style.Yellow(archiveName)) - out := filepath.Join(where.Temp(), "mangal_update") - - switch archiveType { - case "zip": - archive, err := io.ReadAll(res.Body) - if err != nil { - log.Error(err) - return err - } - - archivePath := filepath.Join(out, archiveName) - file, err := filesystem.Api().OpenFile(archivePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) - if err != nil { - log.Error(err) - return err - } - - defer util.Ignore(file.Close) - - _, err = file.Write(archive) - if err != nil { - log.Error(err) - return err - } - - stat, err := file.Stat() - if err != nil { - log.Error(err) - return err - } - - err = util.Unzip(file, stat.Size(), out) - case "tar.gz": - err = util.UntarGZ(res.Body, out) - } - - if err != nil { - log.Error(err) - return err - } - - erase() - erase = info("Replacing %s", style.Yellow(selfPath)) - // remove the old binary - // it should not interrupt the running process - file, err := filesystem.Api().OpenFile(selfPath, os.O_WRONLY|os.O_TRUNC, os.ModePerm) - if err != nil { - log.Error(err) - err = errors.New("error removing old binary, try running this as a root user") - return err - } - - newMangal, err := filesystem.Api().OpenFile(filepath.Join(out, constant.Mangal), os.O_RDONLY, os.ModePerm) - if err != nil { - log.Error(err) - return - } - - stat, err := newMangal.Stat() - if err != nil { - log.Error(err) - return - } - - if stat.Size() == 0 { - log.Error(err) - return err - } - - _, err = io.Copy(file, newMangal) - if err != nil { - log.Error(err) - return err - } - - erase() - - return -} diff --git a/updater/version.go b/updater/version.go index 0f99d2e6..d8af8b8e 100644 --- a/updater/version.go +++ b/updater/version.go @@ -3,17 +3,81 @@ package updater import ( "encoding/json" "errors" + "github.com/metafates/mangal/filesystem" "github.com/metafates/mangal/util" + "github.com/metafates/mangal/where" "net/http" + "os" + "path/filepath" + "time" ) -var cachedLatestVersion string +var ( + cachedLatestVersion string + versionCacheFile = filepath.Join(where.Cache(), "version.json") +) + +type versionCache struct { + Version string `json:"version"` + Updated time.Time `json:"updated"` +} + +func getCachedVersion() (version string, err error) { + if cachedLatestVersion != "" { + return cachedLatestVersion, nil + } + + exists, err := filesystem.Api().Exists(versionCacheFile) + if err != nil { + return + } + + if !exists { + return + } + + var data []byte + data, err = filesystem.Api().ReadFile(versionCacheFile) + if err != nil { + return + } + + var cache versionCache + + err = json.Unmarshal(data, &cache) + if err != nil { + return + } + + if time.Since(cache.Updated) > time.Hour { + return + } + + version = cache.Version + return +} + +func cacheVersion(version string) error { + cachedLatestVersion = version + cache := versionCache{ + Version: version, + Updated: time.Now(), + } + + data, err := json.Marshal(cache) + if err != nil { + return err + } + + return filesystem.Api().WriteFile(versionCacheFile, data, os.ModePerm) +} // LatestVersion returns the latest version of mangal. // It will fetch the latest version from the GitHub API. func LatestVersion() (version string, err error) { - if cachedLatestVersion != "" { - return cachedLatestVersion, nil + version, err = getCachedVersion() + if err == nil && version != "" { + return } resp, err := http.Get("https://api.github.com/repos/metafates/mangal/releases/latest") @@ -39,6 +103,6 @@ func LatestVersion() (version string, err error) { } version = release.TagName[1:] - cachedLatestVersion = version + _ = cacheVersion(version) return } diff --git a/util/untar.go b/util/untar.go deleted file mode 100644 index f6a50831..00000000 --- a/util/untar.go +++ /dev/null @@ -1,71 +0,0 @@ -package util - -import ( - "archive/tar" - "compress/gzip" - "fmt" - "github.com/metafates/mangal/filesystem" - "io" - "os" - "path/filepath" -) - -func UntarGZ(gzipStream io.Reader, path string) error { - err := filesystem.Api().MkdirAll(path, os.ModePerm) - if err != nil { - return err - } - - uncompressedStream, err := gzip.NewReader(gzipStream) - if err != nil { - return err - } - - tarReader := tar.NewReader(uncompressedStream) - - for { - header, err := tarReader.Next() - - if err == io.EOF { - break - } - - if err != nil { - return err - } - - name := filepath.Join(path, header.Name) - - switch header.Typeflag { - case tar.TypeDir: - if err = filesystem.Api().MkdirAll(name, os.ModePerm); err != nil { - return err - } - case tar.TypeReg: - err = filesystem.Api().MkdirAll(filepath.Dir(name), os.ModePerm) - if err != nil { - return err - } - - outFile, err := filesystem.Api().OpenFile(name, os.O_CREATE|os.O_WRONLY, os.ModePerm) - if err != nil { - return err - } - if _, err = io.Copy(outFile, tarReader); err != nil { - return err - } - err = outFile.Close() - default: - err = fmt.Errorf( - "uknown type: %s in %s", - string(header.Typeflag), - header.Name) - } - - if err != nil { - return err - } - } - - return nil -} diff --git a/util/untar_test.go b/util/untar_test.go deleted file mode 100644 index cb71f9d3..00000000 --- a/util/untar_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package util - -import ( - "github.com/metafates/mangal/filesystem" - "github.com/samber/lo" - . "github.com/smartystreets/goconvey/convey" - "path/filepath" - "testing" -) - -func TestUntarGZ(t *testing.T) { - Convey("Given a tar.gz file", t, func() { - filesystem.SetOsFs() - path := filepath.Join(filepath.Dir(lo.Must(filepath.Abs("."))), filepath.Join("assets", "testdata", "tardata.tar.gz")) - file := lo.Must(filesystem.Api().Open(path)) - filesystem.SetMemMapFs() - Convey("When untarring it", func() { - err := UntarGZ(file, ".") - Convey("Then the error should be nil", func() { - So(err, ShouldBeNil) - Convey("And the files should be extracted", func() { - for _, info := range []lo.Tuple2[string, bool]{ - {filepath.Join("tardata", "hey.jpeg"), false}, - {filepath.Join("tardata", "a"), true}, - {filepath.Join("tardata", "a", "b"), true}, - {filepath.Join("tardata", "a", "hello.txt"), false}, - } { - filename := info.A - isDir := info.B - - exists := lo.Must(filesystem.Api().Exists(filename)) - So(exists, ShouldBeTrue) - - if isDir { - isDir := lo.Must(filesystem.Api().IsDir(filename)) - So(isDir, ShouldBeTrue) - } - } - }) - }) - }) - }) -} diff --git a/util/unzip.go b/util/unzip.go deleted file mode 100644 index b83f16d8..00000000 --- a/util/unzip.go +++ /dev/null @@ -1,76 +0,0 @@ -package util - -import ( - "archive/zip" - "fmt" - "github.com/metafates/mangal/filesystem" - "io" - "os" - "path/filepath" - "strings" -) - -func Unzip(zipStream io.ReaderAt, size int64, dest string) error { - r, err := zip.NewReader(zipStream, size) - if err != nil { - return err - } - - err = filesystem.Api().MkdirAll(dest, os.ModePerm) - if err != nil { - return err - } - - // Closure to address file descriptors issue with all the deferred .Close() methods - extractAndWriteFile := func(f *zip.File) error { - rc, err := f.Open() - if err != nil { - return err - } - - defer Ignore(rc.Close) - - path := filepath.Join(dest, f.Name) - - // Check for ZipSlip (Directory traversal) - if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) { - return fmt.Errorf("illegal file path: %s", path) - } - - if f.FileInfo().IsDir() { - err = filesystem.Api().MkdirAll(path, f.Mode()) - if err != nil { - return err - } - } else { - err = filesystem.Api().MkdirAll(filepath.Dir(path), f.Mode()) - if err != nil { - return err - } - - f, err := filesystem.Api().OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) - if err != nil { - return err - } - - defer Ignore(f.Close) - - _, err = io.Copy(f, rc) - if err != nil { - return err - } - } - - return nil - } - - for _, f := range r.File { - err := extractAndWriteFile(f) - - if err != nil { - return err - } - } - - return nil -} diff --git a/util/unzip_test.go b/util/unzip_test.go deleted file mode 100644 index 68ca97b3..00000000 --- a/util/unzip_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package util - -import ( - "github.com/metafates/mangal/filesystem" - "github.com/samber/lo" - . "github.com/smartystreets/goconvey/convey" - "path/filepath" - "testing" -) - -func TestUnzip(t *testing.T) { - Convey("Given a zip file", t, func() { - filesystem.SetOsFs() - path := filepath.Join(filepath.Dir(lo.Must(filepath.Abs("."))), filepath.Join("assets", "testdata", "zipdata.zip")) - file := lo.Must(filesystem.Api().Open(path)) - filesystem.SetMemMapFs() - Convey("When unzipping it", func() { - err := Unzip(file, lo.Must(file.Stat()).Size(), "a") - Convey("Then the error should be nil", func() { - So(err, ShouldBeNil) - Convey("And the files should be extracted", func() { - for _, info := range []lo.Tuple2[string, bool]{ - {filepath.Join("a", "zipdata", "hey.jpeg"), false}, - {filepath.Join("a", "zipdata", "a"), true}, - {filepath.Join("a", "zipdata", "a", "b"), true}, - {filepath.Join("a", "zipdata", "a", "hello.txt"), false}, - } { - filename := info.A - isDir := info.B - - exists := lo.Must(filesystem.Api().Exists(filename)) - So(exists, ShouldBeTrue) - - if isDir { - isDir := lo.Must(filesystem.Api().IsDir(filename)) - So(isDir, ShouldBeTrue) - } - } - }) - }) - }) - }) -}