Skip to content

Commit

Permalink
Simplified the API even more
Browse files Browse the repository at this point in the history
The `load` method now returns an `IO[List[Path]]` instead of a `Stream[IO, Path]`

The `open` and `readCursor` methods where eliminated as well (not going to be used that often anyways), just as suggested.

Co-authored-by: TonioGela <toniogela89@gmail.com>
  • Loading branch information
Hombre-x and TonioGela committed Jul 26, 2024
1 parent 377f1e5 commit 7291083
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 45 deletions.
39 changes: 8 additions & 31 deletions core/src/main/scala/io/chrisdavenport/shellfish/FilesOs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package io.chrisdavenport.shellfish

import cats.syntax.all.*
import cats.effect.{IO, Resource}
import cats.effect.IO

import fs2.{Stream, Chunk}
import fs2.io.file.*
Expand Down Expand Up @@ -616,7 +616,7 @@ object FilesOs {
def lineSeparator: String = files.lineSeparator

/** Gets the contents of the specified directory. */
def list(path: Path): Stream[IO, Path] = files.list(path)
def list(path: Path): IO[List[Path]] = files.list(path).compile.toList

/**
* Moves the source to the target, failing if source does not exist or the
Expand All @@ -634,22 +634,6 @@ object FilesOs {
def move(source: Path, target: Path, flags: CopyFlags): IO[Unit] =
files.move(source, target, flags)

/**
* Creates a `FileHandle` for the file at the supplied `Path`. The supplied
* flags indicate the mode used when opening the file (e.g. read, write,
* append) as well as the ability to specify additional options (e.g.
* automatic deletion at process exit).
*/
def open(path: Path, flags: Flags): Resource[IO, FileHandle[IO]] =
files.open(path, flags)

/**
* Returns a `ReadCursor` for the specified path, using the supplied flags
* when opening the file.
*/
def readCursor(path: Path, flags: Flags): Resource[IO, ReadCursor[IO]] =
files.readCursor(path, flags)

/**
* Returns the real path i.e. the actual location of `path`. The precise
* definition of this method is implementation dependent but in general it
Expand Down Expand Up @@ -698,7 +682,7 @@ object FilesOs {
* Creates a temporary file and deletes it at the end of the use of it.
*/
def tempFile[A](use: Path => IO[A]): IO[A] =
IO.defer(files.createTempFile).bracket(use)(deleteIfExists(_).void)
files.tempFile.use(use)

/**
* Creates a temporary file and deletes it at the end of the use of it.
Expand All @@ -723,18 +707,14 @@ object FilesOs {
prefix: String,
suffix: String,
permissions: Permissions
)(use: Path => IO[A]): IO[A] = IO
.defer(files.createTempFile(dir, prefix, suffix, permissions.some))
.bracket(use)(deleteIfExists(_).void)
)(use: Path => IO[A]): IO[A] =
files.tempFile(dir, prefix, suffix, permissions.some).use(use)

/**
* Creates a temporary directory and deletes it at the end of the use of it.
*/
def tempDirectory[A](use: Path => IO[A]): IO[A] =
IO.defer(files.createTempDirectory)
.bracket(use)(deleteRecursively(_).recover {
case _: NoSuchFileException => ()
})
files.tempDirectory.use(use)

/**
* Creates a temporary directory and deletes it at the end of the use of it.
Expand All @@ -756,11 +736,8 @@ object FilesOs {
dir: Option[Path],
prefix: String,
permissions: Permissions
)(use: Path => IO[A]): IO[A] = IO
.defer(files.createTempDirectory(dir, prefix, permissions.some))
.bracket(use)(deleteRecursively(_).recover { case _: NoSuchFileException =>
()
})
)(use: Path => IO[A]): IO[A] =
files.tempDirectory(dir, prefix, permissions.some).use(use)

/** User's home directory */
def userHome: IO[Path] = files.userHome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
package io.chrisdavenport.shellfish
package syntax

import cats.effect.{IO, Resource}
import cats.effect.IO

import fs2.Stream
import fs2.io.file.*

import scodec.bits.ByteVector
Expand Down Expand Up @@ -474,7 +473,7 @@ package object path {
def isSameFile(path2: Path): IO[Boolean] = FilesOs.isSameFile(path, path2)

/** Gets the contents of the specified directory. */
def list: Stream[IO, Path] = FilesOs.list(path)
def list: IO[List[Path]] = FilesOs.list(path)

/**
* Moves the source to the target, failing if source does not exist or the
Expand All @@ -491,17 +490,6 @@ package object path {
def move(target: Path, flags: CopyFlags): IO[Unit] =
FilesOs.move(path, target, flags)

/** Creates a `FileHandle` for the file at the supplied `Path`. */
def open(flags: Flags): Resource[IO, FileHandle[IO]] =
FilesOs.open(path, flags)

/**
* Returns a `ReadCursor` for the specified path, using the supplied flags
* when opening the file.
*/
def readCursor(flags: Flags): Resource[IO, ReadCursor[IO]] =
FilesOs.readCursor(path, flags)

// Real Path
/** Returns the real path i.e. the actual location of `path`. */
def realPath: IO[Path] = FilesOs.realPath(path)
Expand Down

0 comments on commit 7291083

Please sign in to comment.