Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom function as an output. #41

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ cli {
| `Shell` | `Fli.Shells` |
| `Command` | `string` |
| `Input` | `string` |
| `Output` | `string` / `StringBuilder` |
| `Output` | `Outputs` (see below) |
| `WorkingDirectory` | `string` |
| `EnvironmentVariable` | `string * string` |
| `EnvironmentVariables` | `(string * string) list` |
Expand All @@ -168,7 +168,7 @@ cli {
| `Exec` | `string` |
| `Arguments` | `string` / `string seq` / `string list` / `string array` |
| `Input` | `string` |
| `Output` | `string` / `StringBuilder` |
| `Output` | `Outputs` (see below) |
| `Verb` | `string` |
| `Username` | `string` |
| `Credentials` | `string * string * string` |
Expand All @@ -184,9 +184,10 @@ Currently provided `Fli.Shells`:
- `WSL` runs `wsl.exe -- ...`
- `BASH` runs `bash -c ...`

`Fli.Outputs`:
- `string` which is the absolute path of the output file.
- `StringBuilder` which will be filled with output text.
Provided `Fli.Outputs`:
- `File of string` a string with an absolute path of the output file.
- `StringBuilder of StringBuilder` a StringBuilder which will be filled with the output text.
- `Custom of Func<string, unit>` a custom function (`string -> unit`) that will be called with the output string (logging, printing etc.).

### Do you miss something?
Open an [issue](https://github.com/CaptnCodr/Fli/issues) or start a [discussion](https://github.com/CaptnCodr/Fli/discussions).
Expand Down
20 changes: 20 additions & 0 deletions src/Fli.Tests/ExecContext/ExecCommandExecuteTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ let ``Get output in StringBuilder`` () =

sb.ToString() |> should equal "Test\n"

[<Test>]
let ``Call custom function in output`` () =
let testFunc (test: string) (s: string) = s |> should equal test
if OperatingSystem.IsWindows() then
cli {
Exec "cmd.exe"
Arguments "/c echo Test"
Output (testFunc "Test\r\n")
}
|> Command.execute
|> ignore
else
cli {
Exec "bash"
Arguments "-c \"echo Test\""
Output (testFunc "Test\n")
}
|> Command.execute
|> ignore

[<Test>]
let ``Hello World with executing program async`` () =
if OperatingSystem.IsWindows() then
Expand Down
1 change: 1 addition & 0 deletions src/Fli/CE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module CE =
match box (output) with
| :? string as s -> Outputs.File s
| :? StringBuilder as sb -> Outputs.StringBuilder sb
| :? (string -> unit) as func -> Outputs.Custom func
| _ -> failwith "Cannot convert output type."

type StartingContext =
Expand Down
1 change: 1 addition & 0 deletions src/Fli/Command.fs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ module Command =
match o with
| Outputs.File(file) -> File.WriteAllText(file, output)
| Outputs.StringBuilder(stringBuilder) -> output |> stringBuilder.Append |> ignore
| Outputs.Custom(func) -> func.Invoke(output)

| None -> ()

Expand Down
3 changes: 2 additions & 1 deletion src/Fli/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ module Domain =
| PWSH
| WSL
| BASH

and Outputs =
| File of string
| StringBuilder of StringBuilder
| Custom of System.Func<string, unit>

type ExecConfig =
{ Program: string
Expand Down