From fdd59b79dfdb063d03aa8c4b88e28b9281455e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Fri, 30 Aug 2024 23:42:48 +0200 Subject: [PATCH] Code optimization --- src/which/Process.hx | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/which/Process.hx b/src/which/Process.hx index c1534a1..c34d508 100644 --- a/src/which/Process.hx +++ b/src/which/Process.hx @@ -4,10 +4,9 @@ package which; import js.Node; #elseif php import php.Syntax; -#else -import asys.io.Process as AsysProcess; #end +import asys.io.Process as AsysProcess; using StringTools; using tink.io.Source; @@ -16,29 +15,30 @@ abstract class Process { /** The identifier of the current process's group. **/ public static var gid(get, never): Promise; - static inline function get_gid() return getProcessId("g"); + static inline function get_gid(): Promise + return Finder.isWindows ? new Error(MethodNotAllowed, "Not supported on Windows platform.") : + #if js Node.process.getgid() + #elseif php Syntax.code("posix_getgid()") + #else getProcessId("g") #end; /** The identifier of the current process's user. **/ public static var uid(get, never): Promise; - static inline function get_uid() return getProcessId("u"); + static inline function get_uid(): Promise + return Finder.isWindows ? new Error(MethodNotAllowed, "Not supported on Windows platform.") : + #if js Node.process.getuid() + #elseif php Syntax.code("posix_getuid()") + #else getProcessId("u") #end; - /** Gets the numeric identity of the current process. **/ + /** Gets the numeric identity of the current process by using the "id" command. **/ static function getProcessId(identity: String): Promise { - if (Finder.isWindows) return Promise.reject(new Error(MethodNotAllowed, "Not supported on Windows platform.")); - - #if nodejs - return Promise.resolve(identity == "g" ? Node.process.getgid() : Node.process.getuid()); - #elseif php - return Promise.resolve(identity == "g" ? Syntax.code("posix_getgid()") : Syntax.code("posix_getuid()")); - #else - final process = new AsysProcess("id", ['-$identity']); - return process.exitCode() - .next(exitCode -> exitCode == 0 ? process.stdout.all() : Error.withData('The "id" process exited with a $exitCode code.', exitCode)) - .next(stdout -> { - process.close(); - final processId = Std.parseInt(stdout.toString().trim()); - processId != null ? processId : new Error("Unable to parse the process output."); - }); - #end + final process = new AsysProcess("id", ['-$identity']); + return process.exitCode() + .next(exitCode -> exitCode == 0 ? process.stdout.all() : Error.withData('The "id" command failed.', exitCode)) + .next(stdout -> { + process.close(); + final output = stdout.toString(); + final processId = Std.parseInt(output.trim()); + processId != null ? processId : Error.withData('Unable to parse the output of the "id" command.', output); + }); } }