diff --git a/cli/domain/create.go b/cli/domain/create.go index c309de9af..3ca2670ea 100644 --- a/cli/domain/create.go +++ b/cli/domain/create.go @@ -30,7 +30,7 @@ This command creates a new domain name record for a specific zone. You must specify a zone (e.g. example.com) under which your domain name records are listed. This zone must already be created on your provider account. -Examples: +Example: abra domain create -p gandi foo.com A myapp 192.168.178.44 @@ -128,6 +128,7 @@ Which means you can then deploy an app against "myapp.foo.com" successfully. strconv.Itoa(createdRecord.Priority), }) + table.SetCaption(true, "record created") table.Render() return nil diff --git a/cli/domain/domain.go b/cli/domain/domain.go index 06fe05e5c..88079c75b 100644 --- a/cli/domain/domain.go +++ b/cli/domain/domain.go @@ -33,5 +33,6 @@ allows to implement new provider support easily. Subcommands: []*cli.Command{ DomainListCommand, DomainCreateCommand, + DomainRemoveCommand, }, } diff --git a/cli/domain/remove.go b/cli/domain/remove.go new file mode 100644 index 000000000..c3c7b99dd --- /dev/null +++ b/cli/domain/remove.go @@ -0,0 +1,127 @@ +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/AlecAivazis/survey/v2" + "github.com/libdns/gandi" + "github.com/libdns/libdns" + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" +) + +// DomainRemoveCommand lists domains. +var DomainRemoveCommand = &cli.Command{ + Name: "remove", + Usage: "Remove domain name records for a zone", + Aliases: []string{"rm"}, + ArgsUsage: " ", + Flags: []cli.Flag{ + internal.DNSProviderFlag, + }, + Description: ` +This command removes a new domain name record for a specific zone. + +You must specify a zone (e.g. example.com) under which your domain name records +are listed. This zone must already be created on your provider account. + +The "" can be retrieved by running "abra domain list ". This can be +used then in this command for deletion of the record. + +Example: + + abra domain remove -p gandi foo.com A myapp + +Which means that the domain name record of type A for myapp.foo.com will be deleted. +`, + Action: func(c *cli.Context) error { + zone := c.Args().First() + if zone == "" { + internal.ShowSubcommandHelpAndError(c, errors.New("no zone provided")) + } + + recordType := c.Args().Get(1) + recordName := c.Args().Get(2) + + if recordType == "" { + internal.ShowSubcommandHelpAndError(c, errors.New("no type provided")) + } + if recordName == "" { + internal.ShowSubcommandHelpAndError(c, errors.New("no name provided")) + } + + var err error + var provider gandi.Provider + switch internal.DNSProvider { + case "gandi": + provider, err = gandiPkg.New() + if err != nil { + logrus.Fatal(err) + } + default: + logrus.Fatalf("'%s' is not a supported DNS provider", internal.DNSProvider) + } + + records, err := provider.GetRecords(c.Context, zone) + if err != nil { + logrus.Fatal(err) + } + + var toDelete libdns.Record + for _, record := range records { + if record.Type == recordType && record.Name == recordName { + toDelete = record + break + } + } + + if (libdns.Record{}) == toDelete { + logrus.Fatal("provider library reports no matching record?") + } + + tableCol := []string{"type", "name", "value", "TTL", "priority"} + table := abraFormatter.CreateTable(tableCol) + + value := toDelete.Value + if len(toDelete.Value) > 30 { + value = fmt.Sprintf("%s...", toDelete.Value[:30]) + } + + table.Append([]string{ + toDelete.Type, + toDelete.Name, + value, + toDelete.TTL.String(), + strconv.Itoa(toDelete.Priority), + }) + + table.Render() + + response := false + prompt := &survey.Confirm{ + Message: "continue with record deletion?", + } + + if err := survey.AskOne(prompt, &response); err != nil { + return err + } + + if !response { + logrus.Fatal("exiting as requested") + } + + _, err = provider.DeleteRecords(c.Context, zone, []libdns.Record{toDelete}) + if err != nil { + logrus.Fatal(err) + } + + logrus.Info("record successfully deleted") + + return nil + }, +}