Skip to content

Commit

Permalink
Merge pull request #56 from CaptnCodr/feature/open-default
Browse files Browse the repository at this point in the history
Open files in default/assigned program
  • Loading branch information
CaptnCodr authored Aug 11, 2023
2 parents e6d6d98 + 3d47411 commit 6f1f86f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
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

0 comments on commit 6f1f86f

Please sign in to comment.