openshift-evangelists / oc-cluster-wrapper

oc cluster up bash wrapper
Apache License 2.0
144 stars 72 forks source link

profiles fail to be read during bash command completion #97

Open loleary opened 6 years ago

loleary commented 6 years ago

When using command completion, errors will appear in bash command completion due to oc-cluster.bash using the absolute and hard coded path from the user who executed oc-cluster completion bash.

For example, as root:

# ./oc-cluster completion bash 
...
  commands="up down destroy list status show ssh console plugin-list plugin-install plugin-uninstall -h --help"

  profiles=$(ls -d /root/.oc/profiles/*/ | xargs -L1 basename)

  boolean_args="--create-machine= --forward-ports= --metrics= --skip-registry-check= --use-existing-config="
...

This is because the assignment of profiles in the heredoc of the completion function uses the following assignment:

profiles=\$(ls -d $OPENSHIFT_PROFILES_DIR/*/ | xargs -L1 basename)

This assignment carries 2 problems.

  1. $OPENSHIFT_PROFILES_DIR/*/ is expanded to the value of the variable as it is defined by the oc-cluster command itself. By default, this is the user's home directory.
  2. If the profiles directory is empty, the ls command will fail and xargs will pass 0 arguments to basename.

The profiles assignment should instead define OPENSHIFT_PROFILES_DIR in the same manner that oc-cluster does so that it is more generic along with setting profiles to an empty string in the event that no profiles are found. For example:

ls -d "\$OPENSHIFT_PROFILES_DIR"/*/ >/dev/null 2>&1 && profiles=\$(ls -d "\$OPENSHIFT_PROFILES_DIR"/*/ | xargs -L1 basename) || profiles=''

I submitted #96 to address both of these problem.