From 05b1aca6bec483286f840e9d043ac6b7bb4b7c57 Mon Sep 17 00:00:00 2001 From: "bparodi@lezzo.org" Date: Sat, 16 Nov 2024 11:12:01 +0100 Subject: [PATCH] add shell --- src/Jobs.fs | 5 ----- src/Library.fs | 1 - src/Shell.fs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/src.fsproj | 3 +-- 4 files changed, 51 insertions(+), 8 deletions(-) delete mode 100644 src/Jobs.fs create mode 100644 src/Shell.fs diff --git a/src/Jobs.fs b/src/Jobs.fs deleted file mode 100644 index 86bc152..0000000 --- a/src/Jobs.fs +++ /dev/null @@ -1,5 +0,0 @@ -module Bidello.Jobs - -open Bidello.Datatypes - -open NodaTime diff --git a/src/Library.fs b/src/Library.fs index c63f833..f48fa40 100644 --- a/src/Library.fs +++ b/src/Library.fs @@ -70,7 +70,6 @@ type Bidello(client: IClusterClient) = let timeout = next_job_at - now |> _.TotalMilliseconds |> floor |> int32 // printfn $"Waking up in: {timeout}|{next_job_at}" let! _wake_up = db |> Database.wait_notification timeout ct - // TODO: se il lavoro da fare, è già in esecuzione (e ancora non terminato, che fare?) () } diff --git a/src/Shell.fs b/src/Shell.fs new file mode 100644 index 0000000..9b8970e --- /dev/null +++ b/src/Shell.fs @@ -0,0 +1,50 @@ +module Bidello.Shell + +open Sheller + +open FSharp.Control.LazyExtensions + +open Pentole.Path + +let private shell_ () = + let possible_shells = [ + "/bin/sh" + "/bin/bash" + "/bin/dash" + "/bin/fish" + "/usr/bin/sh" + "/usr/bin/bash" + "/usr/bin/dash" + "/usr/bin/fish" + ] + + let error = Error "Can't find the system shell. Check your system $PATH." + List.tryFind (fun sh -> sh |> Path.of_string |> Result.isOk) possible_shells + |> Option.map Ok |> Option.defaultValue error + + +let shell = Lazy.Create shell_ + +let which (executable: string) = + let run (shell: string) = + let rc = + Builder + .UseShell(shell) + .UseExecutable("which") + .WithArgument(executable) + .UseNoThrow() + .ExecuteAsync () + |> Async.AwaitTask + |> Async.RunSynchronously + if rc.ExitCode = 0 then + rc.StandardOutput + |> _.Trim() + |> Path.of_string + elif rc.ExitCode = 1 then + Error $"Can't find executable path for \"{executable}\"." + else + rc.StandardError + |> Error + + shell.Value + |> Result.bind run diff --git a/src/src.fsproj b/src/src.fsproj index 081c662..03e87d2 100644 --- a/src/src.fsproj +++ b/src/src.fsproj @@ -13,13 +13,12 @@ - + -