func record_del(args ...string) (string, error) { shell.ShowPrompt(false) defer shell.ShowPrompt(true) zone, errs := pdns.GetZone(args[0]) if errs != nil { for err := range errs { shell.Println(err) } return "", nil } record_id, err := strconv.Atoi(args[1]) if err != nil { return "", err } if record_id < 0 || record_id >= len(zone.Records) { return "", errors.New("Index out of bounds") } record := zone.Records[record_id] records := make([]*pdns.Record, 0, 5) for _, rec := range zone.Records { if rec != record && rec.Name == record.Name && rec.Type == record.Type { records = append(records, rec) } } shell.Print("Do you really want to delete this record?? [y/n] ") confirm := shell.ReadLine() if confirm != "y" && confirm != "Y" { return "", nil } if len(records) > 1 { errs = zone.UpdateRecords(records) } else { errs = zone.DeleteRecord(record) } if errs != nil { for err := range errs { shell.Println(err) } } return "", nil }
func zone_show(args ...string) (string, error) { const zone_template = ` {{with .Zone}}{{.Name}} Comments : {{.Comments}} DNSSEC : {{.DNSSEC}} Kind : {{.Kind}} LastCheck : {{.LastCheck}} Masters : {{.Masters}} Nameservers : {{.Nameservers}} NotifiedSerial : {{.NotifiedSerial}} Serial : {{.Serial}} Type : {{.Type}} URL : {{.URL}}{{end}} {{$fmt := printf "%%-%ds" .MaxLength}} {{$fmt2 := printf "%%-%ds" .MaxTypeLength}} Records: {{range $i, $record := .Zone.Records}}{{if eq $.Filter "" $record.Type}}[{{printf "%3d" $i}}] {{printf $fmt $record.Name}} {{printf "%5d" $record.TTL}} IN {{printf $fmt2 $record.Type}} {{printf "%3d" $record.Priority}} {{$record.Content}} {{if $record.Disabled}}(Disabled){{end}} {{end}}{{end}} ` zone, errs := pdns.GetZone(args[0]) if errs != nil { for err := range errs { shell.Println(err) } } max_length := 0 max_type_length := 0 for _, record := range zone.Records { if len(record.Name) > max_length { max_length = len(record.Name) } if len(record.Type) > max_type_length { max_type_length = len(record.Type) } } data := struct { Zone *pdns.Zone MaxLength int MaxTypeLength int Filter string }{ zone, max_length, max_type_length, "", } if len(args) > 1 && args[1] != "ANY" { data.Filter = args[1] } tmpl, err := template.New("zone").Parse(zone_template) if err != nil { return "", err } tmpl.Execute(os.Stdout, data) return "", nil }
func record_upd(args ...string) (string, error) { shell.ShowPrompt(false) defer shell.ShowPrompt(true) zone, errs := pdns.GetZone(args[0]) if errs != nil { for err := range errs { shell.Println(err) } return "", nil } record_id, err := strconv.Atoi(args[1]) if err != nil { return "", err } if record_id < 0 || record_id >= len(zone.Records) { return "", errors.New("Index out of bounds") } record := zone.Records[record_id] records := make([]*pdns.Record, 0, 5) for _, rec := range zone.Records { if rec.Name == record.Name && rec.Type == record.Type { records = append(records, rec) } } shell.Println(fmt.Sprintf("Name: %s", record.Name)) shell.Println(fmt.Sprintf("Type: %s", record.Type)) shell.Print(fmt.Sprintf("New content [%s]: ", record.Content)) content := shell.ReadLine() if content != "" { record.Content = content } shell.Print(fmt.Sprintf("New TTL [%d]: ", record.TTL)) ttl := shell.ReadLine() if ttl != "" { record.TTL, _ = strconv.Atoi(ttl) } shell.Print(fmt.Sprintf("New priority [%d]: ", record.Priority)) prio := shell.ReadLine() if prio != "" { record.Priority, _ = strconv.Atoi(prio) } shell.Print(fmt.Sprintf("Disabled [%t]?: ", record.Disabled)) dis := shell.ReadLine() if dis != "" { record.Disabled = (dis == "y") } shell.Print("Do you really want to update this record?? [y/n] ") confirm := shell.ReadLine() if confirm != "y" && confirm != "Y" { return "", nil } errs = zone.UpdateRecords(records) if errs != nil { for err := range errs { shell.Println(err) } } return "", nil }
func record_add(args ...string) (string, error) { shell.ShowPrompt(false) defer shell.ShowPrompt(true) zone, errs := pdns.GetZone(args[0]) if errs != nil { for err := range errs { shell.Println(err) } return "", nil } record := new(pdns.Record) record.TTL = 300 record.Priority = 0 record.Disabled = false if len(args) < 4 { shell.Print("Name: ") name := shell.ReadLine() if name == "" { return "name can't be empty", nil } record.Name = name shell.Print("Type: ") typ := shell.ReadLine() if typ == "" { return "type can't be empty", nil } record.Type = typ shell.Print("Content: ") content := shell.ReadLine() if content == "" { return "content can't be empty", nil } record.Content = content shell.Print("TTL [300]: ") ttl := shell.ReadLine() if ttl != "" { record.TTL, _ = strconv.Atoi(ttl) } shell.Print("Priority [0]: ") prio := shell.ReadLine() if prio != "" { record.Priority, _ = strconv.Atoi(prio) } shell.Print("Disabled [false]?: ") dis := shell.ReadLine() record.Disabled = (dis == "y") } else { record.Name = args[1] record.Type = args[2] record.Content = args[3] if len(args) > 4 { record.TTL, _ = strconv.Atoi(args[4]) } if len(args) > 5 { record.Priority, _ = strconv.Atoi(args[5]) } if len(args) > 6 { record.Disabled = (args[6] == "y") } } shell.Print("Do you really want to add this record?? [y/n] ") confirm := shell.ReadLine() if confirm != "y" && confirm != "Y" { return "", nil } records := make([]*pdns.Record, 0, 5) for _, rec := range zone.Records { if rec.Name == record.Name && rec.Type == record.Type { records = append(records, rec) } } records = append(records, record) errs = zone.UpdateRecords(records) if errs != nil { for err := range errs { shell.Println(err) } } return "", nil }