v.io/jiri@v0.0.0-20160715023856-abfb8b131290/scripts/jiri (about)

     1  #!/bin/bash
     2  # Copyright 2015 The Vanadium Authors. All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  # jiri is a shim script that determines the jiri root directory and invokes
     7  # $JIRI_ROOT/.jiri_root/bin/jiri with the given arguments.
     8  #
     9  # If the JIRI_ROOT environment variable is set, that is assumed to be the jiri
    10  # root directory.
    11  #
    12  # Otherwise the script looks for the .jiri_root directory, starting in the
    13  # current working directory and walking up the directory chain.  The search is
    14  # terminated successfully when the .jiri_root directory is found; it fails after
    15  # it reaches the root of the file system.
    16  #
    17  # This script should be invoked from the jiri root directory or one of its
    18  # subdirectories, unless the JIRI_ROOT environment variable is set.
    19  
    20  set -euf -o pipefail
    21  
    22  # fatal prints an error message and exits.
    23  fatal() {
    24    echo "ERROR: $@" 1>&2
    25    exit 1
    26  }
    27  
    28  # Handle "jiri which" without any arguments.  This is handy to determine whether
    29  # the PATH is set up pointing at this shim script, or a regular binary.  If
    30  # there are any arguments, we pass the command through to the binary.
    31  if [[ $# -eq 1  ]]; then
    32    if [[ "$1" == "which" ]]; then
    33      echo "# script"
    34      type -p $0
    35      exit 0
    36    fi
    37  fi
    38  
    39  # If $JIRI_ROOT is set we always use it, otherwise look for a .jiri_root
    40  # directory starting with the current working directory, and walking up.
    41  if [[ -z ${JIRI_ROOT+x} ]]; then
    42    CWD="$(pwd)"
    43    while [[ ! -d  "$(pwd)/.jiri_root" ]]; do
    44      if [[ "$(pwd)" == "/" ]]; then
    45        fatal "could not find .jiri_root directory"
    46      fi
    47      cd ..
    48    done
    49    export JIRI_ROOT="$(pwd)"
    50    cd "${CWD}"
    51  fi
    52  
    53  # Make sure the jiri binary exists and is executable.
    54  if [[ ! -e "${JIRI_ROOT}/.jiri_root/bin/jiri" ]]; then
    55    fatal "${JIRI_ROOT}/.jiri_root/bin/jiri does not exist"
    56  elif [[ ! -x "${JIRI_ROOT}/.jiri_root/bin/jiri" ]]; then
    57    fatal "${JIRI_ROOT}/.jiri_root/bin/jiri is not executable"
    58  fi
    59  
    60  # Add $JIRI_ROOT/.jiri_root/bin to PATH, so that the jiri binary can find the
    61  # subcommand binaries.
    62  export PATH="${JIRI_ROOT}/.jiri_root/bin:${PATH}"
    63  
    64  # Execute the jiri binary.
    65  exec "${JIRI_ROOT}/.jiri_root/bin/jiri" "$@"