2021-08-15 19:37:11 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"gitea.nehmer.net/torben/dyndns/service"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdDNSUpdate = &cobra.Command{
|
|
|
|
Use: "dns-update",
|
|
|
|
Short: "Dynamically update a DNS record",
|
|
|
|
Long: `Dynamically updates a given DNS FQDN based on the given v4 and/or v6 addresses.
|
|
|
|
At least one IP must be specified, the call fails if both IPs are empty.`,
|
|
|
|
// Run is defined inline in init to capture Flag variables in the closure
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(cmdDNSUpdate)
|
|
|
|
ip4 := cmdDNSUpdate.Flags().IPP("ipv4", "4", nil, "IPv4 Address to set")
|
|
|
|
ip6 := cmdDNSUpdate.Flags().IPP("ipv6", "6", nil, "IPv6 Address to set")
|
|
|
|
name := cmdDNSUpdate.Flags().StringP("name", "n", "", "Hostname to set within the domain")
|
|
|
|
cmdDNSUpdate.MarkFlagRequired("name")
|
|
|
|
domain := cmdDNSUpdate.Flags().StringP("domain", "d", "", "Domain to set the records in.")
|
|
|
|
cmdDNSUpdate.MarkFlagRequired("domain")
|
|
|
|
|
|
|
|
cmdDNSUpdate.Run = func(cmd *cobra.Command, args []string) {
|
|
|
|
service.LoadConfig()
|
|
|
|
|
2021-08-22 12:50:52 +00:00
|
|
|
err := service.DNSUpdateEntry(*domain, *name, *ip4, *ip6)
|
2021-08-15 19:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not update DNS: %s", err)
|
|
|
|
}
|
|
|
|
log.Println("DNS has been successfully updated.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|