GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - UnstoppableMango/CliWrap.FSharp: Idiomatic F# support for CliWrap

BranchesTags
Open more actions menu

Latest commit

 

History

273 Commits
273 Commits

Folders and files

NameName
Last commit message
Last commit date
.config
.config
 
 
.devcontainer
.devcontainer
 
 
.githooks
.githooks
 
 
.github
.github
 
 
.idea/.idea.CliWrap.FSharp/.idea
.idea/.idea.CliWrap.FSharp/.idea
 
 
.vscode
.vscode
 
 
docs
docs
 
 
examples
examples
 
 
scripts
scripts
 
 
src
src
 
 
.editorconfig
.editorconfig
 
 
.fantomasignore
.fantomasignore
 
 
.git-blame-ignore-revs
.git-blame-ignore-revs
 
 
.gitignore
.gitignore
 
 
.release-please-manifest.json
.release-please-manifest.json
 
 
AGENTS.md
AGENTS.md
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CONTEXT.md
CONTEXT.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
CliWrap.FSharp.sln
CliWrap.FSharp.sln
 
 
CliWrap.FSharp.sln.DotSettings
CliWrap.FSharp.sln.DotSettings
 
 
Directory.Build.props
Directory.Build.props
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
SECURITY.md
SECURITY.md
 
 
codecov.yml
codecov.yml
 
 
flake.lock
flake.lock
 
 
flake.nix
flake.nix
 
 
global.json
global.json
 
 
release-please-config.json
release-please-config.json
 
 

Repository files navigation

CliWrap.FSharp

Build Codecov OpenSSF Scorecard OpenSSF Best Practices GitHub Release NuGet Version NuGet Downloads Hercules CI

Idiomatic F# support for CliWrap.

Install

  • NuGet: dotnet add package UnMango.CliWrap.FSharp
  • GitHub Packages: dotnet add package UnMango.CliWrap.FSharp -s github
    • Authenticating to GitHub Packages
    • Installing from GitHub Packages

Usage

Bindings for normal CliWrap commands are available in the Cli module.

let main args = async {
  let! result =
    Cli.wrap "dotnet"
    |> Cli.args [ "build" ]
    |> Cli.workDir "~/src/CliWrap.FSharp"
    |> Cli.exec

  return result.ExitCode
}

Cancellation token overloads are available in Cli.Tasks.

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

  return result.ExitCode
}
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

  return result.ExitCode
}

Buffered execution captures standard output and standard error, and the CommandResult active patterns match on the result.

let main args = async {
  let! result =
    Cli.wrap "git"
    |> Cli.args [ "status"; "--short" ]
    |> Cli.resourcePolicyf _.SetPriority(ProcessPriorityClass.BelowNormal)
    |> Cli.validation CommandResultValidation.None
    |> Cli.execBuffered

  match result with
  | CommandResult.Buffered(0, stdout, _) -> printfn "%s" stdout
  | CommandResult.Buffered(code, _, stderr) -> eprintfn "git exited with %i: %s" code stderr
}

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.

let main args =
  let cmd = command "dotnet" {
    args [ "build" ]
    workingDirectory "~/src/CliWrap.FSharp"
  }

  cmd.ExecuteAsync()

The computation expression also supports executing the command with exec.

let main args = task {
  let! result = command "dotnet" {
    args [ "build" ]
    workingDirectory "~/src/CliWrap.FSharp"
    exec
  }

  return result.ExitCode
}

Cancellation is also supported.

let main args = task {
  use cts = new CancellationTokenSource()
  let! result = command "dotnet" {
    args [ "build" ]
    workingDirectory "~/src/CliWrap.FSharp"
    exec cts.Token
  }

  return result.ExitCode
}

CliWrap's buffered execution is supported with buffered.

let main args = task {
  use cts = new CancellationTokenSource()
  let! result = command "dotnet" {
    args [ "build" ]
    workingDirectory "~/src/CliWrap.FSharp"
    buffered Encoding.UTF8 cts.Token
  }

  return result.ExitCode
}

Asynchrony with F#'s Async<'T> is supported with async.

let main args = async {
  use cts = new CancellationTokenSource()
  let! result = command "dotnet" {
    args [ "build" ]
    workingDirectory "~/src/CliWrap.FSharp"
    async cts.Token
  }

  return result.ExitCode
}

Event streams

CliWrap's event streams are supported in two styles. Both produce the same command events, which you can match with the Started, StdOut, StdErr and Exited active pattern.

Observe pushes events to subscribers as an IObservable<CommandEvent>, so F#'s Observable module works with it.

// Disposing the subscription stops the process
let subscription =
  Cli.wrap "dotnet"
  |> Cli.args [ "build" ]
  |> Cli.observe CancellationToken.None
  |> Observable.subscribe (function
    | Started pid -> printfn $"Started %d{pid}"
    | StdOut line -> printfn $"%s{line}"
    | StdErr line -> eprintfn $"%s{line}"
    | Exited code -> printfn $"Exited with %d{code}")

Listen pulls events as an IAsyncEnumerable<CommandEvent>. With FSharp.Control.TaskSeq opened, task can loop over it.

open FSharp.Control

let main args = task {
  use cts = new CancellationTokenSource()
  let events = command "dotnet" {
    args [ "build" ]
    listen cts.Token
  }

  for event in events do
    match event with
    | StdOut line -> printfn $"%s{line}"
    | _ -> ()
}

Both styles have the same set of overloads.

Cli command { }
observe / listen No cancellation
observe ct / listen ct observe ct / listen ct Cancellation
observee encoding ct / listene encoding ct observe encoding ct / listen encoding ct One encoding for standard output and standard error
observef forceful graceful / listenf forceful graceful Forceful and graceful cancellation
observe stdoutEnc stderrEnc forceful graceful / listen ... Everything

CliWrap's piping functionality is supported via the pipeline computation expression.

let main args =
  let cmd = pipeline {
    "an inline string source"
    Cli.wrap "echo"
  }

  cmd.ExecuteAsync()

Each command's standard output is piped into the next command's standard input, in order. A leading string, Stream, byte[], ReadOnlyMemory<byte> or PipeSource feeds the first command. A single trailing PipeTarget, Stream, StringBuilder or line delegate takes the last command's standard output, and a trailing pair of targets takes its standard output and standard error, in that order.

let main args =
  let stdout = StringBuilder()
  let stderr = StringBuilder()

  let cmd = pipeline {
    "Hello world"
    Cli.wrap "cat"
    Cli.wrap "head" |> Cli.args [ "-c"; "5" ]
    stdout, stderr
  }

  cmd.ExecuteAsync()

Limited support for piping is available with |>>.

let main args =
  let cmd = Cli.wrap "yes" |>> Cli.wrap "echo"

  cmd.ExecuteAsync()

AOT Compatibility

Trimming is supported, but FSharp.Core still has some work to do before full AOT is supported.

dotnet/fsharp#13398

Contributing

Contributions are welcome! See CONTRIBUTING.md for how to build, test, and submit changes. To report a security vulnerability, follow SECURITY.md instead of opening an issue.

Inspirations

The idea to abuse F# computation expressions was inspired by Akkling.Hocon and FsHttp.

Obviously CliWrap was a big inspiration for this package.

Q/A

Idiomatic? This looks nothing like the F# I write!

If something looks off please open an issue! I've only recently been diving further into the F# ecosystem.

Why not paket?

If renovate ever supports it!

About

Idiomatic F# support for CliWrap

Topics

clidotnetfsharpprocessshell

Resources

Readme
MIT license

Contributing

Contributing

Security policy

Security policy
Activity

Stars

14 stars

Watchers

3 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.