Skip to content

Commit

Permalink
Merge pull request #5 from ruedigerp/develop
Browse files Browse the repository at this point in the history
fix: fix some issue and make code more readable
  • Loading branch information
ruedigerp authored Nov 13, 2024
2 parents 864db9b + 9a5f90d commit 669f487
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 131 deletions.
123 changes: 83 additions & 40 deletions cmd/addRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,57 @@ var addRecordCmd = &cobra.Command{
Long: `Add Cloudflare DNS record`,
ValidArgs: []string{"--domain", "-d", "--ip", "-i", "-rtype", "-r", "--proxied", "-p", "--zone", "-z"},
Run: func(cmd *cobra.Command, args []string) {

domain, _ := cmd.Flags().GetString("domain")
rtype, _ := cmd.Flags().GetString("rtype")
ip, _ := cmd.Flags().GetString("ip")
proxied, _ := cmd.Flags().GetBool("proxied")
zone, _ := cmd.Flags().GetString("zone")
serviceprovider, _ := cmd.Flags().GetString("serviceprovider")
isvalid := dnsapi.IsValidIP(ip)
if !isvalid {
fmt.Printf("IP: %s is no a valid ip address\n", ip)

if dnsapi.CheckEmpty(ip, "ip address", "-i|--ip 123.123.123.123") ||
dnsapi.CheckEmpty(domain, "domain", "-d|--domain test.domain.tld") {
return
}
if dnsapi.CheckEmpty(domain, "domain", "-d|--domain test.domain.tld") {

if !dnsapi.IsValidIP(ip) {
fmt.Printf("IP: %s is no a valid ip address\n", ip)
return
// }
// if domain == "" {
// fmt.Printf("No domain/subdomain user -d|--domain test.domain.tld")
} else {
if serviceprovider == "cloudflare" {
zoneID := dnsconfig.Cloudflare.ZoneId
token := dnsconfig.Cloudflare.Token
resp, _, _ := dnsapi.GetRecordId(zoneID, token, domain)
}

if serviceprovider == "cloudflare" {

zoneID := dnsconfig.Cloudflare.ZoneId
token := dnsconfig.Cloudflare.Token

resp, id, _ := dnsapi.GetRecordId(zoneID, token, domain)
if resp {

fmt.Printf("Record exists. (%s)\n", id)
return

} else {

dnsapi.AddRecord(zoneID, token, domain, rtype, ip, proxied)

resp, recordID, err := dnsapi.GetRecordId(zoneID, token, domain)
if resp {
fmt.Printf("Record exists.\n")
return
fmt.Printf("RecordID: %s\n", recordID)
} else {
dnsapi.AddRecord(zoneID, token, domain, rtype, ip, proxied)
resp, recordID, err := dnsapi.GetRecordId(zoneID, token, domain)
if resp {
fmt.Printf("RecordID: %s\n", recordID)
} else {
fmt.Printf("Fehler beim abrufen des Records: %v\n", err)
}
fmt.Printf("Fehler beim abrufen des Records: %v\n", err)
}
} else if serviceprovider == "bind" {
if dnsapi.CheckEmpty(zone, "zone", "-z|--zone example.com") ||
dnsapi.CheckEmpty(ip, "ip address", "-i|--ip 123.123.123.123") ||
dnsapi.CheckEmpty(rtype, "record type", "-r|--rtype (A|MX|TXT|...)") {
return
}
// if zone == "" {
// fmt.Printf("Please provide zone. (--zone|-z example.com)\n")
// return
// }
// if ip == "" {
// fmt.Printf("Please provide ip address. (--ip|-i 123.123.123.123)\n")
// return
// }
// if rtype == "" {
// fmt.Printf("Please provide zone. (--rytpe|-r (A|MX|TXT|...))\n")
// return
// }
dnsapi.BindInsertRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, zone, domain, ip, rtype)

}

} else if serviceprovider == "bind" {

if dnsapi.CheckEmpty(zone, "zone", "-z|--zone example.com") ||
dnsapi.CheckEmpty(rtype, "record type", "-r|--rtype (A|MX|TXT|...)") {
return
}

dnsapi.BindInsertRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, zone, domain, ip, rtype)

}

},
Expand All @@ -74,3 +72,48 @@ var addRecordCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(addRecordCmd)
}

// switch serviceprovider {
// case "cloudflare":
// handleCloudflareRecord(domain, rtype, ip, proxied)
// case "bind":
// if dnsapi.CheckEmpty(zone, "zone", "-z|--zone example.com") {
// return
// }
// dnsapi.BindInsertRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, zone, domain, ip, rtype)
// default:
// fmt.Println("Unsupported service provider. Please specify either 'cloudflare' or 'bind'.")
// }

// Funktion zur zentralen Validierung und Abruf von Flags
// func getFlag(cmd *cobra.Command, name, usageHint string) string {
// value, _ := cmd.Flags().GetString(name)
// if dnsapi.CheckEmpty(value, name, usageHint) {
// return ""
// }
// return value
// }

// // Separater Handler für Cloudflare-Logik
// func handleCloudflareRecord(domain, rtype, ip string, proxied bool) {
// zoneID := dnsconfig.Cloudflare.ZoneId
// token := dnsconfig.Cloudflare.Token

// recordExists, _, err := dnsapi.GetRecordId(zoneID, token, domain)
// if err != nil {
// fmt.Printf("Error checking record: %v\n", err)
// return
// }
// if recordExists {
// fmt.Println("Record exists.")
// return
// }

// dnsapi.AddRecord(zoneID, token, domain, rtype, ip, proxied)
// _, recordID, err := dnsapi.GetRecordId(zoneID, token, domain)
// if err != nil {
// fmt.Printf("Error retrieving record ID: %v\n", err)
// } else {
// fmt.Printf("RecordID: %s\n", recordID)
// }
// }
54 changes: 41 additions & 13 deletions cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,101 @@ var batchCmd = &cobra.Command{
Use: "batch",
Short: "update DNS record",
Long: `update DNS record`,
// ValidArgs: []string{},
Run: func(cmd *cobra.Command, args []string) {
// serviceprovider, _ := cmd.Flags().GetString("serviceprovider")
// if serviceprovider == "cloudflare" {

zoneID := dnsconfig.Cloudflare.ZoneId
token := dnsconfig.Cloudflare.Token

if dnsconfig.Batch.Command == "add" {

fmt.Printf("Add command\n")
fmt.Printf("provider: %s\nzone: %s\nip: %s\n", dnsconfig.Batch.Provider, dnsconfig.Batch.Zone, dnsconfig.Batch.IP)
isvalid := dnsapi.IsValidIP(dnsconfig.Batch.IP)
if !isvalid {

if !dnsapi.IsValidIP(dnsconfig.Batch.IP) {
fmt.Printf("IP: %s is no a valid ip address\n", dnsconfig.Batch.IP)
return
}

for _, domain := range dnsconfig.Batch.Domains {

fmt.Printf("Add domain: %s\n", domain)

if dnsconfig.Batch.Provider == "cloudflare" {

dnsapi.AddRecord(zoneID, token, domain, dnsconfig.Batch.IP, dnsconfig.Batch.IP, dnsconfig.Batch.Proxied)

} else if dnsconfig.Batch.Provider == "bind" {

dnsapi.BindInsertRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, dnsconfig.Batch.Zone, domain, dnsconfig.Batch.IP, dnsconfig.Batch.Rtype)

}

}

} else if dnsconfig.Batch.Command == "update" {

fmt.Printf("update command\n")
fmt.Printf("provider: %s\nzone: %s\nip: %s\n", dnsconfig.Batch.Provider, dnsconfig.Batch.Zone, dnsconfig.Batch.IP)
isvalid := dnsapi.IsValidIP(dnsconfig.Batch.IP)
if !isvalid {

if !dnsapi.IsValidIP(dnsconfig.Batch.IP) {
fmt.Printf("IP: %s is no a valid ip address\n", dnsconfig.Batch.IP)
return
}
isvalid = dnsapi.IsValidIP(dnsconfig.Batch.Oldip)
if !isvalid {

if !dnsapi.IsValidIP(dnsconfig.Batch.Oldip) {
fmt.Printf("Old-IP: %s is no a valid ip address\n", dnsconfig.Batch.Oldip)
return
}

for _, domain := range dnsconfig.Batch.Domains {

fmt.Printf("Update domain: %s\n", domain)

if dnsconfig.Batch.Provider == "cloudflare" {

dnsapi.UpdateRecord(zoneID, token, domain, dnsconfig.Batch.IP, dnsconfig.Batch.IP, dnsconfig.Batch.Proxied)

} else if dnsconfig.Batch.Provider == "bind" {

dnsapi.BindUpdateRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, dnsconfig.Batch.Zone, domain, dnsconfig.Batch.IP, dnsconfig.Batch.Oldip, dnsconfig.Batch.Rtype)

}

}
} else if dnsconfig.Batch.Command == "delete" {

fmt.Printf("delete command\n")
fmt.Printf("provider: %s\nzone: %s\nip: %s\n", dnsconfig.Batch.Provider, dnsconfig.Batch.Zone, dnsconfig.Batch.IP)

for _, domain := range dnsconfig.Batch.Domains {

fmt.Printf("Delete domain: %s\n", domain)

if dnsconfig.Batch.Provider == "cloudflare" {

_, recordID, _ := dnsapi.GetRecordId(zoneID, token, domain)
dnsapi.DeleteRecord(zoneID, token, recordID)

} else if dnsconfig.Batch.Provider == "bind" {
isvalid := dnsapi.IsValidIP(dnsconfig.Batch.IP)
if !isvalid {

if !dnsapi.IsValidIP(dnsconfig.Batch.IP) {
fmt.Printf("IP: %s is no a valid ip address\n", dnsconfig.Batch.IP)
return
}

dnsapi.BindDeleteRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, dnsconfig.Batch.Zone, domain, dnsconfig.Batch.IP, dnsconfig.Batch.Rtype)

}

}
// } else if serviceprovider == "bind" {

}
// }

},
}

func init() {

rootCmd.AddCommand(batchCmd)

}
72 changes: 32 additions & 40 deletions cmd/deleteRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,47 @@ var deleteRecordCmd = &cobra.Command{
ValidArgs: []string{"--domain", "-d", "--ip", "-i", "--zone", "-z", "--rtype", "-r", "--serviceprovider", "-s"},

Run: func(cmd *cobra.Command, args []string) {

domain, _ := cmd.Flags().GetString("domain")
rtype, _ := cmd.Flags().GetString("rtype")
ip, _ := cmd.Flags().GetString("ip")
zone, _ := cmd.Flags().GetString("zone")
serviceprovider, _ := cmd.Flags().GetString("serviceprovider")
isvalid := dnsapi.IsValidIP(ip)
if !isvalid {
fmt.Printf("IP: %s is no a valid ip address\n", ip)

if dnsapi.CheckEmpty(ip, "ip address", "-i|--ip 123.123.123.123") ||
dnsapi.CheckEmpty(domain, "domain", "-d|--domain test.domain.tld") {
return
}
if dnsapi.CheckEmpty(domain, "domain", "-d|--domain test.domain.tld") {

if !dnsapi.IsValidIP(ip) {
fmt.Printf("IP: %s is no a valid ip address\n", ip)
return
} else {
// if domain == "" {
// fmt.Printf("No domain/subdomain user -d|--domain test.domain.tld")

// } else {
if serviceprovider == "cloudflare" {
zoneID := dnsconfig.Cloudflare.ZoneId
token := dnsconfig.Cloudflare.Token
resp, recordID, _ := dnsapi.GetRecordId(zoneID, token, domain)
if resp {
fmt.Printf("RecordID: %s\n", recordID)
dnsapi.DeleteRecord(zoneID, token, recordID)
return
} else {
fmt.Printf("Record not exists.\n")
return
}
} else if serviceprovider == "bind" {
if dnsapi.CheckEmpty(zone, "zone", "-z|--zone example.com") ||
dnsapi.CheckEmpty(ip, "ip address", "-i|--ip 123.123.123.123") ||
dnsapi.CheckEmpty(rtype, "record type", "-r|--rtype (A|MX|TXT|...)") {
return
}
// if zone == "" {
// fmt.Printf("Please provide zone. (--zone|-z example.com)\n")
// return
// }
// if ip == "" {
// fmt.Printf("Please provide ip address. (--ip|-i 123.123.123.123)\n")
// return
// }
// if rtype == "" {
// fmt.Printf("Please provide zone. (--rytpe|-r (A|MX|TXT|...))\n")
// return
// }
dnsapi.BindDeleteRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, zone, domain, ip, rtype)
}

if serviceprovider == "cloudflare" {

zoneID := dnsconfig.Cloudflare.ZoneId
token := dnsconfig.Cloudflare.Token

resp, recordID, _ := dnsapi.GetRecordId(zoneID, token, domain)
if resp {
fmt.Printf("RecordID: %s\n", recordID)
dnsapi.DeleteRecord(zoneID, token, recordID)
return
} else {
fmt.Printf("Record not exists.\n")
return
}

} else if serviceprovider == "bind" {

if dnsapi.CheckEmpty(zone, "zone", "-z|--zone example.com") ||
dnsapi.CheckEmpty(rtype, "record type", "-r|--rtype (A|MX|TXT|...)") {
return
}

dnsapi.BindDeleteRecord(dnsconfig.Bind.Server, dnsconfig.Bind.Keyname, dnsconfig.Bind.Hmackey, zone, domain, ip, rtype)

}

},
Expand Down
24 changes: 14 additions & 10 deletions cmd/getRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ var getRecordCmd = &cobra.Command{
Long: `Get Cloudflare DSN Record`,
ValidArgs: []string{"--domain", "-d"},
Run: func(cmd *cobra.Command, args []string) {

domain, _ := cmd.Flags().GetString("domain")

if dnsapi.CheckEmpty(domain, "domain", "-d|--domain test.domain.tld") {
return
}

zoneID := dnsconfig.Cloudflare.ZoneId
token := dnsconfig.Cloudflare.Token

// dnsapi.GetRecord(zoneID, token, domain)

resp, recordID, msg, _ := dnsapi.GetRecord(zoneID, token, domain)
if resp {
fmt.Printf("RecordID: %s\n", recordID)
fmt.Printf("%s\n", msg)
} else {
zoneID := dnsconfig.Cloudflare.ZoneId
token := dnsconfig.Cloudflare.Token
dnsapi.GetRecord(zoneID, token, domain)
resp, recordID, msg, _ := dnsapi.GetRecord(zoneID, token, domain)
if resp {
fmt.Printf("RecordID: %s\n", recordID)
fmt.Printf("%s\n", msg)
} else {
fmt.Printf("Record not found\n")
}
fmt.Printf("Record not found\n")
}

},
Expand Down
Loading

0 comments on commit 669f487

Please sign in to comment.