nuno revisou este gist 1 month ago. Ir para a revisão
1 file changed, 131 insertions
gistfile1.txt(arquivo criado)
| @@ -0,0 +1,131 @@ | |||
| 1 | + | function cmd::identity::_rule() { | |
| 2 | + | local subcmd="${1:-show}" | |
| 3 | + | shift || true | |
| 4 | + | ||
| 5 | + | case "$subcmd" in | |
| 6 | + | assign) cmd::identity::_rule_assign "$@" ;; | |
| 7 | + | unassign) cmd::identity::_rule_unassign "$@" ;; | |
| 8 | + | show) cmd::identity::_rule_show "$@" ;; | |
| 9 | + | *) | |
| 10 | + | log::error "Unknown rule subcommand '${subcmd}'. Available: assign, unassign, show" | |
| 11 | + | return 1 | |
| 12 | + | ;; | |
| 13 | + | esac | |
| 14 | + | } | |
| 15 | + | function cmd::identity::_rule_assign() { | |
| 16 | + | local name="" rule="" | |
| 17 | + | while [[ $# -gt 0 ]]; do | |
| 18 | + | case "$1" in | |
| 19 | + | --name) name="$2"; shift 2 ;; | |
| 20 | + | --rule) rule="$2"; shift 2 ;; | |
| 21 | + | *) log::error "Unknown flag: $1"; return 1 ;; | |
| 22 | + | esac | |
| 23 | + | done | |
| 24 | + | ||
| 25 | + | [[ -z "$name" ]] && { log::error "Missing required flag: --name"; return 1; } | |
| 26 | + | [[ -z "$rule" ]] && { log::error "Missing required flag: --rule"; return 1; } | |
| 27 | + | identity::require_exists "$name" || return 1 | |
| 28 | + | rule::exists "$rule" || { log::error "Rule not found: ${rule}"; return 1; } | |
| 29 | + | ||
| 30 | + | local exit_code | |
| 31 | + | identity::add_rule "$name" "$rule" || exit_code=$? | |
| 32 | + | ||
| 33 | + | if [[ $exit_code -eq 2 ]]; then | |
| 34 | + | log::warn "Rule '${rule}' is already assigned to identity '${name}'" | |
| 35 | + | return 0 | |
| 36 | + | fi | |
| 37 | + | ||
| 38 | + | log::ok "Rule '${rule}' assigned to identity '${name}'" | |
| 39 | + | ||
| 40 | + | # Reapply rules if auto_apply | |
| 41 | + | local auto | |
| 42 | + | auto=$(identity::rule_flags "$name" "auto_apply") | |
| 43 | + | if [[ "$auto" != "false" ]]; then | |
| 44 | + | log::info "Reapplying rules for all peers in identity '${name}'..." | |
| 45 | + | identity::reapply_rules "$name" | |
| 46 | + | log::ok "Rules reapplied" | |
| 47 | + | fi | |
| 48 | + | ||
| 49 | + | # Warn about strict_rule | |
| 50 | + | if policy::strict_rule "$(identity::policy "$name")"; then | |
| 51 | + | log::warn "Strict rule is enabled for identity '${name}' — peer rules will not be additive" | |
| 52 | + | fi | |
| 53 | + | } | |
| 54 | + | function cmd::identity::_rule_unassign() { | |
| 55 | + | local name="" rule="" all=false | |
| 56 | + | while [[ $# -gt 0 ]]; do | |
| 57 | + | case "$1" in | |
| 58 | + | --name) name="$2"; shift 2 ;; | |
| 59 | + | --rule) rule="$2"; shift 2 ;; | |
| 60 | + | --all) all=true; shift ;; | |
| 61 | + | *) log::error "Unknown flag: $1"; return 1 ;; | |
| 62 | + | esac | |
| 63 | + | done | |
| 64 | + | ||
| 65 | + | [[ -z "$name" ]] && { log::error "Missing required flag: --name"; return 1; } | |
| 66 | + | identity::require_exists "$name" || return 1 | |
| 67 | + | ||
| 68 | + | if $all; then | |
| 69 | + | local rules | |
| 70 | + | rules=$(identity::rules "$name") | |
| 71 | + | if [[ -z "$rules" ]]; then | |
| 72 | + | log::warn "Identity '${name}' has no rules assigned" | |
| 73 | + | return 0 | |
| 74 | + | fi | |
| 75 | + | identity::clear_rules "$name" | |
| 76 | + | log::ok "All rules removed from identity '${name}'" | |
| 77 | + | cmd::identity::_reapply_after_unassign "$name" | |
| 78 | + | return 0 | |
| 79 | + | fi | |
| 80 | + | ||
| 81 | + | [[ -z "$rule" ]] && { | |
| 82 | + | log::error "Missing required flag: --rule (or use --all to remove all)" | |
| 83 | + | return 1 | |
| 84 | + | } | |
| 85 | + | ||
| 86 | + | identity::remove_rule "$name" "$rule" | |
| 87 | + | local exit_code=$? | |
| 88 | + | if [[ $exit_code -ne 0 ]]; then | |
| 89 | + | log::error "Rule '${rule}' is not assigned to identity '${name}'" | |
| 90 | + | return 1 | |
| 91 | + | fi | |
| 92 | + | ||
| 93 | + | log::ok "Rule '${rule}' removed from identity '${name}'" | |
| 94 | + | cmd::identity::_reapply_after_unassign "$name" | |
| 95 | + | } | |
| 96 | + | function cmd::identity::_rule_show() { | |
| 97 | + | local name="" | |
| 98 | + | while [[ $# -gt 0 ]]; do | |
| 99 | + | case "$1" in | |
| 100 | + | --name) name="$2"; shift 2 ;; | |
| 101 | + | *) log::error "Unknown flag: $1"; return 1 ;; | |
| 102 | + | esac | |
| 103 | + | done | |
| 104 | + | ||
| 105 | + | [[ -z "$name" ]] && { log::error "Missing required flag: --name"; return 1; } | |
| 106 | + | identity::require_exists "$name" || return 1 | |
| 107 | + | ||
| 108 | + | local rules policy strict auto | |
| 109 | + | rules=$(identity::rules "$name") | |
| 110 | + | policy=$(identity::policy "$name") | |
| 111 | + | strict=$(identity::rule_flags "$name" "strict_rule") | |
| 112 | + | auto=$(identity::rule_flags "$name" "auto_apply") | |
| 113 | + | ||
| 114 | + | echo "" | |
| 115 | + | ui::row "Identity" "$name" | |
| 116 | + | ui::row "Policy" "$policy" | |
| 117 | + | ui::row "Strict rule" "$(ui::bool "$strict")" | |
| 118 | + | ui::row "Auto apply" "$(ui::bool "$auto")" | |
| 119 | + | echo "" | |
| 120 | + | ||
| 121 | + | if [[ -z "$rules" ]]; then | |
| 122 | + | ui::row "Rules" "— none assigned" | |
| 123 | + | else | |
| 124 | + | printf " %-20s\n" "Rules:" | |
| 125 | + | while IFS= read -r rule_name; do | |
| 126 | + | [[ -z "$rule_name" ]] && continue | |
| 127 | + | printf " · %s\n" "$rule_name" | |
| 128 | + | done <<< "$rules" | |
| 129 | + | fi | |
| 130 | + | echo "" | |
| 131 | + | } | |
Próximo
Anterior