Skip to content

The complexity of hosts_export should be reduced #7

@sudoforge

Description

@sudoforge

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions