github.com/vmware/govmomi@v0.37.1/scripts/govc-env.bash (about) 1 # Copyright (c) 2015 VMware, Inc. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 # Provide a simple shell extension to save and load govc 16 # environments to disk. No more running `export GOVC_ABC=xyz` 17 # in different shells over and over again. Loading the right 18 # govc environment variables is now only one short and 19 # autocompleted command away! 20 # 21 # Usage: 22 # * Source this file from your `~/.bashrc` or running shell. 23 # * Execute `govc-env` to print GOVC_* variables. 24 # * Execute `govc-env --save <name>` to save GOVC_* variables. 25 # * Execute `govc-env <name>` to load GOVC_* variables. 26 # 27 28 _govc_env_dir=$HOME/.govmomi/env 29 mkdir -p "${_govc_env_dir}" 30 31 _govc-env-complete() { 32 local w="${COMP_WORDS[COMP_CWORD]}" 33 local c="$(find ${_govc_env_dir} -mindepth 1 -maxdepth 1 -type f | sort | xargs -r -L1 basename | xargs echo)" 34 35 # Only allow completion if preceding argument if the function itself 36 if [ "$3" == "govc-env" ]; then 37 COMPREPLY=( $(compgen -W "${c}" -- "${w}") ) 38 fi 39 } 40 41 govc-env() { 42 # Print current environment 43 if [ -z "$1" ]; then 44 for VAR in $(env | grep ^GOVC_ | cut -d= -f1); do 45 echo "export ${VAR}='${!VAR}'" 46 done 47 48 return 49 fi 50 51 # Save current environment 52 if [ "$1" == "--save" ]; then 53 if [ ! -z "$2" ]; then 54 govc-env > ${_govc_env_dir}/$2 && echo govc env has been saved to ${_govc_env_dir}/$2 55 else 56 echo Usage: govc-env --save configname 57 fi 58 return 59 fi 60 61 # Load specified environment 62 source ${_govc_env_dir}/$1 63 } 64 65 complete -F _govc-env-complete govc-env 66