hostsctl icon indicating copy to clipboard operation
hostsctl copied to clipboard

The complexity of hosts_export should be reduced

Open sudoforge opened this issue 7 years ago • 0 comments

hosts_export is fairly inefficient:

hosts_export() {
  local grep_args=(-v -e '^#' -e '^$')
  # Exclude all enabled hosts from the output.
  IFS=$'\n' read -d '' -r -a enabled < "${ENABLED_HOSTS}"
  for i in "${enabled[@]}"; do
      grep_args+=(-e " ${i}")
  done;
  # Exclude all disabled hosts from the output to prevent duplicates.
  IFS=$'\n' read -d '' -r -a disabled < "${DISABLED_HOSTS}"
  for i in "${disabled[@]}"; do
      grep_args+=(-e "^${i}$")
  done;
  remote="$(grep "${grep_args[@]}" "${REMOTE_HOSTS}")"
  # Concatenate the users hosts file, disabled hosts, and the remote hosts
  # stripped of any enabled hosts.
  hosts="$(cat "${USER_HOSTS}" "${DISABLED_HOSTS}")"
  echo "${hosts}"$'\n'"${remote}"
}

This could be reduced in complexity with a single grep command piped to uniq.

sudoforge avatar Oct 20 '18 17:10 sudoforge