dyndns/cmd/nft-update.go

39 lines
1.3 KiB
Go
Raw Permalink Normal View History

package cmd
import (
"log"
"gitea.nehmer.net/torben/dyndns/service"
"github.com/spf13/cobra"
)
var cmdNFTUpdate = &cobra.Command{
Use: "nft-update",
Short: "Dynamically update a NFT firewall",
Long: `Dynamically updates the given NFT Firewall sets based on the given v4 and/or v6 addresses.
At least one IP must be specified, the call fails if both IPs are empty.
The call will flush the sets given in the table before adding the IPs.`,
// Run is defined inline in init to capture Flag variables in the closure
}
func init() {
rootCmd.AddCommand(cmdNFTUpdate)
ip4 := cmdNFTUpdate.Flags().IPP("ipv4", "4", nil, "IPv4 Address to add to the set")
ip6 := cmdNFTUpdate.Flags().IPP("ipv6", "6", nil, "IPv6 Address to add to the set")
table := cmdNFTUpdate.Flags().StringP("table", "t", "", "The name of the NFT table to modifiy")
cmdNFTUpdate.MarkFlagRequired("table")
set4 := cmdNFTUpdate.Flags().StringP("set4", "s", "", "The IPv4 NFT set name in the given table.")
set6 := cmdNFTUpdate.Flags().StringP("set6", "r", "", "The IPv6 NFT Set name in the given table.")
cmdNFTUpdate.Run = func(cmd *cobra.Command, args []string) {
service.LoadConfig()
err := service.NFTUpdateSetsCmd(*table, *set4, *ip4, *set6, *ip6)
if err != nil {
log.Fatalf("Could not update NFT: %s", err)
}
log.Println("NFT has been successfully updated.")
}
}