github.com/vmware/govmomi@v0.51.0/scripts/govc-env.bash (about)

     1  # © Broadcom. All Rights Reserved.
     2  # The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  # SPDX-License-Identifier: Apache-2.0
     4  
     5  # Provide a simple shell extension to save and load govc
     6  # environments to disk. No more running `export GOVC_ABC=xyz`
     7  # in different shells over and over again. Loading the right
     8  # govc environment variables is now only one short and
     9  # autocompleted command away!
    10  #
    11  # Usage:
    12  # * Source this file from your `~/.bashrc` or running shell.
    13  # * Execute `govc-env` to print GOVC_* variables.
    14  # * Execute `govc-env --save <name>` to save GOVC_* variables.
    15  # * Execute `govc-env <name>` to load GOVC_* variables.
    16  #
    17  
    18  _govc_env_dir=$HOME/.govmomi/env
    19  mkdir -p "${_govc_env_dir}"
    20  
    21  _govc-env-complete() {
    22    local w="${COMP_WORDS[COMP_CWORD]}"
    23    local c="$(find ${_govc_env_dir} -mindepth 1 -maxdepth 1 -type f  | sort | xargs -r -L1 basename | xargs echo)"
    24  
    25    # Only allow completion if preceding argument if the function itself
    26    if [ "$3" == "govc-env" ]; then
    27      COMPREPLY=( $(compgen -W "${c}" -- "${w}") )
    28    fi
    29  }
    30  
    31  govc-env() {
    32    # Print current environment
    33    if [ -z "$1" ]; then
    34      for VAR in $(env | grep ^GOVC_ | cut -d= -f1); do
    35        echo "export ${VAR}='${!VAR}'"
    36      done
    37  
    38      return
    39    fi
    40  
    41    # Save current environment
    42    if [ "$1" == "--save" ]; then
    43      if [ ! -z "$2" ]; then
    44      	govc-env > ${_govc_env_dir}/$2 && echo govc env has been saved to ${_govc_env_dir}/$2
    45      else
    46  	echo Usage: govc-env --save configname
    47      fi
    48      return
    49    fi
    50  
    51    # Load specified environment
    52    source ${_govc_env_dir}/$1
    53  }
    54  
    55  complete -F _govc-env-complete govc-env
    56