abra/cli/domain/list.go

76 lines
1.6 KiB
Go
Raw Normal View History

package domain
import (
"errors"
"fmt"
"strconv"
abraFormatter "coopcloud.tech/abra/cli/formatter"
"coopcloud.tech/abra/cli/internal"
gandiPkg "coopcloud.tech/abra/pkg/dns/gandi"
"github.com/libdns/gandi"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// DomainListCommand lists domains.
var DomainListCommand = &cli.Command{
Name: "list",
Usage: "List domains for a server",
Aliases: []string{"ls"},
ArgsUsage: "<zone>",
Flags: []cli.Flag{
internal.DNSProviderFlag,
},
Description: `
2021-10-18 20:20:11 +00:00
This command lists all domains managed by a 3rd party provider.
You must specify a zone (e.g. example.com) under which your DNS entries are
listed. This zone must already be created via the provider web interface or
using "abra domain create".
`,
Action: func(c *cli.Context) error {
zone := c.Args().First()
if zone == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no zone provided"))
}
var err error
var provider gandi.Provider
switch internal.DNSProvider {
case "gandi":
provider, err = gandiPkg.New()
if err != nil {
logrus.Fatal(err)
}
}
records, err := provider.GetRecords(c.Context, zone)
if err != nil {
logrus.Fatal(err)
}
tableCol := []string{"type", "name", "value", "TTL", "priority"}
table := abraFormatter.CreateTable(tableCol)
for _, record := range records {
value := record.Value
if len(record.Value) > 30 {
value = fmt.Sprintf("%s...", record.Value[:30])
}
table.Append([]string{
record.Type,
record.Name,
value,
record.TTL.String(),
strconv.Itoa(record.Priority),
})
}
table.Render()
return nil
},
}