diff --git a/README.md b/README.md index 29e19ce..b8f9ee6 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,65 @@ # CliWrap.FSharp -Idiomatic F# support for CliWrap. +[![Build](https://img.shields.io/github/actions/workflow/status/UnstoppableMango/CliWrap.FSharp/main.yml?branch=main)](https://github.com/UnstoppableMango/CliWrap.FSharp/actions) +[![Codecov](https://img.shields.io/codecov/c/github/UnstoppableMango/CliWrap.FSharp)](https://app.codecov.io/gh/UnstoppableMango/CliWrap.FSharp) +[![GitHub Release](https://img.shields.io/github/v/release/UnstoppableMango/CliWrap.FSharp)](https://github.com/UnstoppableMango/CliWrap.FSharp/releases) +[![NuGet Version](https://img.shields.io/nuget/v/UnMango.CliWrap.FSharp)](https://nuget.org/packages/UnMango.CliWrap.FSharp) +[![NuGet Downloads](https://img.shields.io/nuget/dt/UnMango.CliWrap.FSharp)](https://nuget.org/packages/UnMango.CliWrap.FSharp) + +Idiomatic F# support for [CliWrap](https://github.com/Tyrrrz/CliWrap). + +## Install + +- [NuGet](https://nuget.org/packages/UnMango.CliWrap.FSharp): `dotnet add package UnMango.CliWrap.FSharp` +- [GitHub Packages](): `dotnet add package UnMango.CliWrap.FSharp -s github` + - [Authenticating to GitHub Packages](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry#authenticating-to-github-packages) + - [Installing from GitHub Packages](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry#installing-a-package) ## Usage +Bindings for normal CliWrap commands are available in the `Cli` module. + +```fsharp +let main args = async { + let! result = + Cli.wrap "dotnet" + |> Cli.args [ "build" ] + |> Cli.workDir "~/src/CliWrap.FSharp" + |> Cli.exec + + result.ExitCode +} +``` + +Cancellation token overloads are available in `Cli.Tasks`. + +```fsharp +let main args = task { + use cts = new CancellationTokenSource() + let! result = + Cli.wrap "dotnet" + |> Cli.args [ "build" ] + |> Cli.workDir "~/src/CliWrap.FSharp" + |> Cli.Task.exec cts.Token + + result.ExitCode +} +``` + +```fsharp +let main args = task { + use graceful = new CancellationTokenSource() + use forceful = new CancellationTokenSource() + let! result = + Cli.wrap "dotnet" + |> Cli.args [ "build" ] + |> Cli.workDir "~/src/CliWrap.FSharp" + |> Cli.Task.execf forceful.Token graceful.Token + + result.ExitCode +} +``` + The core of the package is a simple computation expression that wraps `CliWrap.Command`. It attempts to mimic the builder pattern and `.With*` style methods. @@ -76,6 +132,8 @@ let main args = async { } ``` +CliWrap's piping functionality is supported via the `pipeline` computation expression. + ```fsharp let main args = let cmd = pipeline { @@ -86,6 +144,21 @@ let main args = cmd.ExecuteAsync() ``` +Limited support for piping is available with `|>>`. + +```fsharp +let main args = + let cmd = Cli.wrap "yes" |>> Cli.wrap "echo" + + cmd.ExecuteAsync() +``` + +## Inspirations + +The idea to abuse F# computation expressions was inspired by [Akkling.Hocon](https://github.com/Horusiath/Akkling/tree/master/src/Akkling.Hocon) and [FsHttp](https://github.com/fsprojects/FsHttp). + +Obviously CliWrap was a big inspiration for this package. + ## Q/A ### Idiomatic? This looks nothing like the F# I write!