Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DeleteById function #112

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ func (ipt *IPTables) DeleteIfExists(table, chain string, rulespec ...string) err
return err
}

// DeleteById deletes the rule with the specified ID in the given table and chain.
func (ipt *IPTables) DeleteById(table, chain string, id int) (string, error) {
rules, err := ipt.List(table, chain)
if err != nil {
return "", err
}

for _, rule := range rules {
Adam0Brien marked this conversation as resolved.
Show resolved Hide resolved
args := []string{"-t", table, "-D", chain, strconv.Itoa(id)}
_, err := ipt.executeList(args)
if err != nil {
return "", fmt.Errorf("rule %s with ID %d not found: %v", rule, id, err)
}
}

return "Rule deleted successfully", nil
}

// List rules in specified table/chain
func (ipt *IPTables) ListById(table, chain string, id int) (string, error) {
args := []string{"-t", table, "-S", chain, strconv.Itoa(id)}
Expand Down
Loading