package cmd import ( "log" "gitea.nehmer.net/torben/dyndns/service" "github.com/spf13/cobra" "github.com/spf13/viper" ) 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) { log.Printf("Configuration in use: %v", viper.AllSettings()) service.LoadConfig() err := service.DNSUpdateEntry(*domain, *name, *ip4, *ip6) if err != nil { log.Fatalf("Could not update DNS: %s", err) } log.Println("DNS has been successfully updated.") } }