#!/usr/bin/env bash
set -euo pipefail
source "$SHELLOPS_DIR/.envrc"
import "$SHELLOPS_DIR/lib/rust/api.sh"

USAGE="$(cat <<EOF
Runs Rust security checks.

Usage: action rust security [-h,--help] [project-dir]

Arguments:
  project-dir    Directory containing the Cargo workspace. Defaults to current directory.

Flags:
  -h, --help     Show this help text.
EOF
)"

function main() {
  local project_dir="."

  parse_args project_dir "$@"
  cd "$project_dir"

  rust_audit
}

function parse_args() {
  local -n _project_dir=$1
  shift

  while [[ $# -gt 0 ]]; do
    case "$1" in
      -h|--help) log "$USAGE"; exit 0 ;;
      -*)
        error "Unknown option: $1"
        exit 1
        ;;
      *)
        _project_dir="$1"
        ;;
    esac
    shift
  done
}

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  main "$@"
fi
