Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Aug 30, 2024
1 parent dd4d58e commit fdd59b7
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/which/Process.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,29 +15,30 @@ abstract class Process {

/** The identifier of the current process's group. **/
public static var gid(get, never): Promise<Int>;
static inline function get_gid() return getProcessId("g");
static inline function get_gid(): Promise<Int>
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<Int>;
static inline function get_uid() return getProcessId("u");
static inline function get_uid(): Promise<Int>
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<Int> {
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);
});
}
}

0 comments on commit fdd59b7

Please sign in to comment.