execWhich method
Call POSIX which
command.
Calls exec with command which
and args commandName
.
Caches response than returns the executable path for commandName
.
Implementation
Future<String?> execWhich(String containerName, String commandName,
{bool ignoreCache = false, String? def}) async {
if (isEmptyString(containerName) || isEmptyString(commandName)) return null;
if (isEmptyString(commandName, trim: true)) return null;
commandName = commandName.trim();
var containerCache =
_whichCache.putIfAbsent(containerName, () => <String, String>{});
String? cached;
if (!ignoreCache) {
cached = containerCache[commandName];
if (cached != null) {
return cached.isNotEmpty ? cached : def;
}
}
var path = await execAndWaitStdoutAsString(
containerName, 'which', [commandName],
trim: true, desiredExitCode: 0, dataMatcher: commandName);
path ??= '';
containerCache[commandName] = path;
return path.isNotEmpty ? path : def;
}