Skip to content

Commit

Permalink
feat: add DeleteById function
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam0Brien committed Sep 28, 2023
1 parent b9dff5a commit 4e53dea
Showing 1 changed file with 18 additions and 0 deletions.
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 {

Check failure on line 251 in iptables/iptables.go

View workflow job for this annotation

GitHub Actions / Test (1.16.x)

rule declared but not used

Check failure on line 251 in iptables/iptables.go

View workflow job for this annotation

GitHub Actions / Test (1.17.x)

rule declared but not used

Check failure on line 251 in iptables/iptables.go

View workflow job for this annotation

GitHub Actions / Test (1.19.x)

rule declared but not used

Check failure on line 251 in iptables/iptables.go

View workflow job for this annotation

GitHub Actions / Test (1.20.x)

rule declared and not used
args := []string{"-t", table, "-D", chain, strconv.Itoa(id)}
_, err := ipt.executeList(args)
if err != nil {
return "", fmt.Errorf("rule with ID %d not found: %v", 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

0 comments on commit 4e53dea

Please sign in to comment.