findToolPathInteractive method

Future<String?> findToolPathInteractive({
  1. required String username,
  2. required InternetAddress ip,
  3. required String toolName,
  4. String? toolPath,
  5. List<String>? preferredPaths,
  6. bool addHostToKnownHosts = true,
})

Implementation

Future<String?> findToolPathInteractive({
  required String username,
  required InternetAddress ip,
  required String toolName,
  String? toolPath,
  List<String>? preferredPaths,
  bool addHostToKnownHosts = true,
}) async {
  return processRunner.runCommand<String>(
    hostPlatform.sshCommand(
      ipv6: ip.isIpv6,
      sshTarget: ip.sshTarget(username),
      command: toolPath != null
          ? 'find / -type f -name "$toolName" -path "$toolPath" 2>/dev/null'
          : 'find / -type f -name "$toolName" 2>/dev/null',
      addHostToKnownHosts: addHostToKnownHosts,
    ),
    throwOnError: false,
    parseResult: (runResult) {
      final output = runResult.stdout.trim();

      if (runResult.exitCode != 0 && output.isEmpty) {
        return null;
      }

      final outputLines = output.split('\n').map((e) => e.trim()).toList();

      logger.detail(
          'findToolPathInteractive $toolName outputLines: $outputLines');

      final outputLinesLength = outputLines.length;
      final isOutputMultipleLines = outputLinesLength > 1;

      final preferredPathsSet = preferredPaths?.toSet();

      if (preferredPathsSet != null) {
        final preferredPathsInOutput = outputLines
            .where(
              (line) => preferredPathsSet.any(
                (path) => line.contains(path),
              ),
            )
            .toList();

        if (preferredPathsInOutput.isNotEmpty) {
          logger.detail('Find $toolName in Preferred Paths');
          logger.detail('Preferred Paths in Output: $preferredPathsInOutput');
          return preferredPathsInOutput.first;
        }
      }

      logger.detail('Find $toolName in Output');
      logger.detail('Output: $outputLines');

      return isOutputMultipleLines ? outputLines.first : output;
    },
    parseFail: (e, s) {
      logger.detail(
        'Something went wrong while trying to find $toolName. \n $e \n $s',
      );

      return null;
    },
    label: 'Find $toolName',
    logger: logger,
  );
}