| #!/bin/bash | |
| # Copyright 2018 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| #### CATEGORY=Device discovery | |
| ### set the default device to interact with | |
| ## usage: fx set-device DEVICE[:SSH_PORT] | |
| ## | |
| ## fx set-device is used to specify the default device to target for | |
| ## fx commands that communicate with a Fuchsia device. | |
| ## A device is set within the scope of a build directory (i.e. out/arm64 may | |
| ## have a different default device than out/x64). | |
| ## | |
| ## If no device name is given, set-device will attempt to discover devices. If | |
| ## one device is found, that device is set as the default for the current build | |
| ## directory. If more than one device is found, the user must select one and | |
| ## provide it to a subsequent invocation of the command. | |
| ## | |
| ## If specified, DEVICE may be a Fuchsia device name that will be resolved | |
| ## using device-finder or an IP address. An IPv4 address must be specified | |
| ## directly, while IPv6 need to be surrounded by brackets. | |
| ## | |
| ## SSH_PORT, if specified, will be used for all commands that rely on SSH to | |
| ## connect to the device, instead of the default SSH port (22). | |
| ## | |
| ## Examples: | |
| ## fx set-device strut-wind-ahead-turf | |
| ## fx set-device strut-wind-ahead-turf:222 | |
| ## fx set-device 192.168.1.2 | |
| ## fx set-device 192.168.3.1:8022 | |
| ## fx set-device [fe80::7:8%eth0] | |
| ## fx set-device [fe80::7:8%eth0]:5222 | |
| ## fx set-device | |
| ## | |
| ## To unset, use `fx unset-device`. | |
| set -e | |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? | |
| fx-standard-switches "$@" | |
| fx-config-read | |
| function select_device() { | |
| IFS=$'\n' read -rd '' -a devices <<<"$1" | |
| select choice in "${devices[@]}"; do | |
| echo "${devices[$((REPLY-1))]}" | |
| break | |
| done | |
| } | |
| # FX_REMOTE_INVOCATION is set by serve-remote to supress the warning | |
| # for that use case. | |
| if is-remote-workflow-device && [[ -z "${FX_REMOTE_INVOCATION}" ]]; then | |
| fx-warn "Local configuration indicates a remote-workflow setup" | |
| fx-warn " 'fx set-device' does not work correctly at this end of a remote setup" | |
| fx-warn "Execute set-device on the local machine in order to change targets in the remote flow" | |
| fi | |
| device="$1" | |
| if [[ -z "$device" ]]; then | |
| devices="$(fx-target-finder-info | cut -d ' ' -f 2)" | |
| if [[ -z "${devices}" ]]; then | |
| fx-error "No devices discovered, please supply a device name" | |
| exit 1 | |
| fi | |
| if [[ "$(echo "$devices" | wc -l)" -ge 2 ]]; then | |
| fx-error "Multiple devices found, please pick one from the list:" | |
| device=$(select_device "$devices") | |
| else | |
| device="${devices}" | |
| fi | |
| elif ! is-valid-device "${device}"; then | |
| fx-error "Invalid device: ${device}" | |
| fx-command-help | |
| exit 1 | |
| fi | |
| if [[ ! -d "${FUCHSIA_BUILD_DIR}" ]]; then | |
| fx-error "Build directory ${FUCHSIA_BUILD_DIR} does not exist, run \"fx set\" first." | |
| exit 1 | |
| fi | |
| echo "New default device: ${device}" | |
| echo "$device" > "${FUCHSIA_BUILD_DIR}.device" |