Parent directory

lando.plugin.zsh

1557 bytes
 1# Settings
 2: ${LANDO_ZSH_SITES_DIRECTORY:="$HOME/Sites"}
 3: ${LANDO_ZSH_CONFIG_FILE:=.lando.yml}
 4: ${LANDO_ZSH_WRAPPED_COMMANDS:="
 5  artisan
 6  composer
 7  drush
 8  gulp
 9  npm
10  php
11  wp
12  yarn
13"}
14
15# Enable multiple commands with lando.
16function ${=LANDO_ZSH_WRAPPED_COMMANDS} {
17  # If the lando task is available in `lando --help`, then it means:
18  #
19  # 1. `lando` is in a project with a `.lando.yml` file.
20  # 2. The lando task is available for lando, based on the .lando.yml config file.
21  #
22  # This has a penalty of about 250ms, so we still want to check if the lando file
23  # exists before, which is the fast path. If it exists, checking help output is
24  # still faster than running the command and failing.
25  if _lando_file_exists && lando --help 2>&1 | command grep -Eq "^ +lando $0 "; then
26    command lando "$0" "$@"
27  else
28    command "$0" "$@"
29  fi
30}
31
32# Check for the file in the current and parent directories.
33_lando_file_exists() {
34  # Only bother checking for lando within the Sites directory.
35  if [[ "$PWD/" != "$LANDO_ZSH_SITES_DIRECTORY"/* ]]; then
36    # Not within $LANDO_ZSH_SITES_DIRECTORY
37    return 1
38  fi
39
40  local curr_dir="$PWD"
41  # Checking for file: $LANDO_ZSH_CONFIG_FILE within $LANDO_ZSH_SITES_DIRECTORY...
42  while [[ "$curr_dir" != "$LANDO_ZSH_SITES_DIRECTORY" ]]; do
43    if [[ -f "$curr_dir/$LANDO_ZSH_CONFIG_FILE" ]]; then
44      return 0
45    fi
46    curr_dir="${curr_dir:h}"
47  done
48
49  # Could not find $LANDO_ZSH_CONFIG_FILE in the current directory
50  # or in any of its parents up to $LANDO_ZSH_SITES_DIRECTORY.
51  return 1
52}