From 440dcde50a19f741b806f96f8ac17a9ccb0f86df Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 23 Oct 2023 23:13:59 +0000 Subject: [PATCH] jpictl: introduce `jpictl dns show` command to list dns entries Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 18 ++++++++++++++++++ pkg/dns/show.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 pkg/dns/show.go diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go index c3788e0..9023730 100644 --- a/cmd/jpictl/dns.go +++ b/cmd/jpictl/dns.go @@ -132,9 +132,27 @@ var dnsSyncCmd = &cobra.Command{ }, } +var dnsShowCmd = &cobra.Command{ + Use: "show [...]", + Short: "dns show lists entries on DNS for our domain", + PreRun: setVerbosity, + RunE: func(cmd *cobra.Command, args []string) error { + mgr, err := newDNSManagerCommand(cmd, true, true) + if err != nil { + return err + } + + ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout) + defer cancel() + + return mgr.Show(ctx, args...) + }, +} + func init() { rootCmd.AddCommand(dnsCmd) dnsCmd.AddCommand(dnsWriteCmd) dnsCmd.AddCommand(dnsSyncCmd) + dnsCmd.AddCommand(dnsShowCmd) } diff --git a/pkg/dns/show.go b/pkg/dns/show.go new file mode 100644 index 0000000..24cba56 --- /dev/null +++ b/pkg/dns/show.go @@ -0,0 +1,29 @@ +package dns + +import ( + "context" + "fmt" + "time" + + "darvaza.org/core" +) + +// Show shows current DNS entries +func (mgr *Manager) Show(ctx context.Context, names ...string) error { + recs, err := mgr.GetRecords(ctx, names...) + if err != nil { + return core.Wrap(err, "GetRecords") + } + + for _, rr := range recs { + _, _ = fmt.Printf("%s\t%v\tIN\t%s\t%s\t; %s\n", + rr.Name, + int(rr.TTL/time.Second), + rr.Type, + rr.Value, + rr.ID) + } + + _, _ = fmt.Printf("; %v records\n", len(recs)) + return nil +}