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

Open files in default/assigned program in Exec #56

Merged
merged 2 commits into from
Aug 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ cli {
}
|> Command.execute
```
or open a file in the default/assigned program:
```fsharp
cli {
Exec "test.pdf"
}
|> Command.execute
```
(Hint: if file extension is not assigned to any installed program, it will throw a `System.NullReferenceException`)

Write output to a specific file:
```fsharp
Expand Down
28 changes: 17 additions & 11 deletions src/Fli/Command.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ module Command =
| "" -> None
| _ as s -> Some s

let private createProcess executable argumentString =
let private createProcess executable argumentString openDefault =
ProcessStartInfo(
FileName = executable,
Arguments = argumentString,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
UseShellExecute = openDefault,
RedirectStandardInput = not(openDefault),
RedirectStandardOutput = not(openDefault),
RedirectStandardError = not(openDefault)
)

let private trim (s: string) = s.TrimEnd([| '\r'; '\n' |])
Expand Down Expand Up @@ -87,8 +87,8 @@ module Command =
let proc = Process.Start(startInfo = psi)
proc |> inputFunc

let text = proc.StandardOutput.ReadToEnd()
let error = proc.StandardError.ReadToEnd()
let text = if psi.UseShellExecute |> not then proc.StandardOutput.ReadToEnd() else ""
let error = if psi.UseShellExecute |> not then proc.StandardError.ReadToEnd() else ""
proc.WaitForExit()

text |> outputFunc
Expand All @@ -111,8 +111,11 @@ module Command =
| None -> ()

let private addEnvironmentVariables (variables: (string * string) list option) (psi: ProcessStartInfo) =
((variables |> Option.defaultValue [] |> List.iter (psi.Environment.Add)), psi)
|> snd
if psi.UseShellExecute |> not then
((variables |> Option.defaultValue [] |> List.iter (psi.Environment.Add)), psi)
|> snd
else
psi

let private addCredentials (credentials: Credentials option) (psi: ProcessStartInfo) =
match credentials with
Expand Down Expand Up @@ -182,7 +185,7 @@ module Command =
let (proc, flag) = (context.config.Shell, context.config.Input) ||> shellToProcess
let command = context |> quoteBashCommand

(createProcess proc $"""{flag} {command}""")
(createProcess proc $"""{flag} {command}""" false)
.With(WorkingDirectory = (context.config.WorkingDirectory |> Option.defaultValue ""))
.With(StandardOutputEncoding = (context.config.Encoding |> Option.defaultValue null))
.With(StandardErrorEncoding = (context.config.Encoding |> Option.defaultValue null))
Expand All @@ -191,7 +194,10 @@ module Command =
static member internal buildProcess(context: ExecContext) =
checkVerb context.config.Verb context.config.Program

(createProcess context.config.Program (context.config.Arguments |> Option.defaultValue ""))
let arguments = (context.config.Arguments |> Option.defaultValue "")
let openDefault = if arguments = "" && context.config.EnvironmentVariables.IsNone then true else false

(createProcess context.config.Program arguments openDefault)
.With(Verb = (context.config.Verb |> Option.defaultValue null))
.With(WorkingDirectory = (context.config.WorkingDirectory |> Option.defaultValue ""))
.With(UserName = (context.config.UserName |> Option.defaultValue ""))
Expand Down