1// Copyright 2019 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import '../base/common.dart'; 6import '../base/process.dart'; 7import '../globals.dart'; 8import 'fuchsia_sdk.dart'; 9 10// Usage: dev_finder <flags> <subcommand> <subcommand args> 11// 12// Subcommands: 13// commands list all command names 14// flags describe all known top-level flags 15// help describe subcommands and their syntax 16// list lists all Fuchsia devices on the network 17// resolve attempts to resolve all passed Fuchsia domain names on the 18// network 19 20/// A simple wrapper for the Fuchsia SDK's 'dev_finder' tool. 21class FuchsiaDevFinder { 22 /// Returns a list of attached devices as a list of strings with entries 23 /// formatted as follows: 24 /// 192.168.42.172 scare-cable-skip-joy 25 Future<List<String>> list() async { 26 if (fuchsiaArtifacts.devFinder == null || 27 !fuchsiaArtifacts.devFinder.existsSync()) { 28 throwToolExit('Fuchsia dev_finder tool not found.'); 29 } 30 final List<String> command = <String>[ 31 fuchsiaArtifacts.devFinder.path, 32 'list', 33 '-full' 34 ]; 35 final RunResult result = await runAsync(command); 36 if (result.exitCode != 0) { 37 printError('dev_finder failed: ${result.stderr}'); 38 return null; 39 } 40 return result.stdout.split('\n'); 41 } 42 43 /// Returns the host address by which the device [deviceName] should use for 44 /// the host. 45 /// 46 /// The string [deviceName] should be the name of the device from the 47 /// 'list' command, e.g. 'scare-cable-skip-joy'. 48 Future<String> resolve(String deviceName) async { 49 if (fuchsiaArtifacts.devFinder == null || 50 !fuchsiaArtifacts.devFinder.existsSync()) { 51 throwToolExit('Fuchsia dev_finder tool not found.'); 52 } 53 final List<String> command = <String>[ 54 fuchsiaArtifacts.devFinder.path, 55 'resolve', 56 '-local', 57 '-device-limit', '1', 58 deviceName 59 ]; 60 final RunResult result = await runAsync(command); 61 if (result.exitCode != 0) { 62 printError('dev_finder failed: ${result.stderr}'); 63 return null; 64 } 65 return result.stdout.trim(); 66 } 67} 68