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

CustomOperation for each output type. #51

Merged
merged 2 commits into from
May 27, 2023
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
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"isRoot": true,
"tools": {
"paket": {
"version": "7.1.5",
"version": "7.2.1",
"commands": [
"paket"
]
},
"fantomas": {
"version": "5.1.4",
"version": "6.0.3",
"commands": [
"fantomas"
]
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ cli {
|> Command.execute
```

Write output to a function (logging, printing, etc.):
```fsharp
let log (output: string) = Debug.Log($"CLI log: {output}")

cli {
Exec "dotnet"
Arguments "--list-sdks"
Output log
}
|> Command.execute
```

Add environment variables for the executing program:
```fsharp
cli {
Expand Down
35 changes: 24 additions & 11 deletions src/Fli/CE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ module CE =

member this.Yield(_) = this

member internal _.outputTypeMapping(output) =
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 =
{ config: Config option }

Expand Down Expand Up @@ -45,8 +38,18 @@ module CE =

/// Extra `Output` that is being executed immediately after getting output from execution.
[<CustomOperation("Output")>]
member this.Output(context: ICommandContext<ShellContext>, output) =
Cli.output ((this :> ICommandContext<_>).outputTypeMapping output) context.Context
member _.Output(context: ICommandContext<ShellContext>, filePath: string) =
Cli.output (File filePath) context.Context

/// Extra `Output` that is being executed immediately after getting output from execution.
[<CustomOperation("Output")>]
member _.Output(context: ICommandContext<ShellContext>, stringBuilder: StringBuilder) =
Cli.output (StringBuilder stringBuilder) context.Context

/// Extra `Output` that is being executed immediately after getting output from execution.
[<CustomOperation("Output")>]
member _.Output(context: ICommandContext<ShellContext>, func: string -> unit) =
Cli.output (Custom func) context.Context

/// Current executing `working directory`.
[<CustomOperation("WorkingDirectory")>]
Expand Down Expand Up @@ -92,8 +95,18 @@ module CE =

/// Extra `Output` that is being executed immediately after getting output from execution.
[<CustomOperation("Output")>]
member this.Output(context: ICommandContext<ExecContext>, output) =
Program.output ((this :> ICommandContext<_>).outputTypeMapping output) context.Context
member _.Output(context: ICommandContext<ExecContext>, filePath: string) =
Program.output (File filePath) context.Context

/// Extra `Output` that is being executed immediately after getting output from execution.
[<CustomOperation("Output")>]
member _.Output(context: ICommandContext<ExecContext>, stringBuilder: StringBuilder) =
Program.output (StringBuilder stringBuilder) context.Context

/// Extra `Output` that is being executed immediately after getting output from execution.
[<CustomOperation("Output")>]
member _.Output(context: ICommandContext<ExecContext>, func: string -> unit) =
Program.output (Custom func) context.Context

/// Current executing `working directory`.
[<CustomOperation("WorkingDirectory")>]
Expand Down