fuchsia_dev_finder.dart 2.24 KB
Newer Older
1 2 3 4 5 6
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../base/common.dart';
import '../base/process.dart';
7
import '../globals.dart';
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import 'fuchsia_sdk.dart';

// Usage: dev_finder <flags> <subcommand> <subcommand args>
//
// Subcommands:
//   commands         list all command names
//   flags            describe all known top-level flags
//   help             describe subcommands and their syntax
//   list             lists all Fuchsia devices on the network
//   resolve          attempts to resolve all passed Fuchsia domain names on the
//                    network

/// A simple wrapper for the Fuchsia SDK's 'dev_finder' tool.
class FuchsiaDevFinder {
  /// Returns a list of attached devices as a list of strings with entries
  /// formatted as follows:
  /// 192.168.42.172 scare-cable-skip-joy
  Future<List<String>> list() async {
26 27
    if (fuchsiaArtifacts.devFinder == null ||
        !fuchsiaArtifacts.devFinder.existsSync()) {
28 29 30 31 32
      throwToolExit('Fuchsia dev_finder tool not found.');
    }
    final List<String> command = <String>[
      fuchsiaArtifacts.devFinder.path,
      'list',
33
      '-full',
34
    ];
35
    final RunResult result = await processUtils.run(command);
36 37 38 39 40
    if (result.exitCode != 0) {
      printError('dev_finder failed: ${result.stderr}');
      return null;
    }
    return result.stdout.split('\n');
41 42 43 44 45 46 47 48
  }

  /// Returns the host address by which the device [deviceName] should use for
  /// the host.
  ///
  /// The string [deviceName] should be the name of the device from the
  /// 'list' command, e.g. 'scare-cable-skip-joy'.
  Future<String> resolve(String deviceName) async {
49 50
    if (fuchsiaArtifacts.devFinder == null ||
        !fuchsiaArtifacts.devFinder.existsSync()) {
51 52 53 54 55 56 57
      throwToolExit('Fuchsia dev_finder tool not found.');
    }
    final List<String> command = <String>[
      fuchsiaArtifacts.devFinder.path,
      'resolve',
      '-local',
      '-device-limit', '1',
58
      deviceName,
59
    ];
60
    final RunResult result = await processUtils.run(command);
61 62 63 64 65
    if (result.exitCode != 0) {
      printError('dev_finder failed: ${result.stderr}');
      return null;
    }
    return result.stdout.trim();
66 67
  }
}