WIP: domain listing with Gandi

Rethinking the interface already.
This commit is contained in:
2021-10-18 22:16:29 +02:00
parent 7ea3df45d4
commit 597b4b586e
10 changed files with 161 additions and 145 deletions

View File

@ -8,6 +8,7 @@ import (
"coopcloud.tech/abra/cli/app"
"coopcloud.tech/abra/cli/catalogue"
"coopcloud.tech/abra/cli/domain"
"coopcloud.tech/abra/cli/recipe"
"coopcloud.tech/abra/cli/server"
"coopcloud.tech/abra/pkg/config"
@ -59,6 +60,7 @@ func RunApp(version, commit string) {
server.ServerCommand,
recipe.RecipeCommand,
catalogue.CatalogueCommand,
domain.DomainCommand,
VersionCommand,
UpgradeCommand,
},

32
cli/domain/domain.go Normal file
View File

@ -0,0 +1,32 @@
package domain
import (
"github.com/urfave/cli/v2"
)
// DomainCommand supports managing DNS entries.
var DomainCommand = &cli.Command{
Name: "domain",
Usage: "Manage domains via 3rd party providers",
Aliases: []string{"d"},
ArgsUsage: "<domain>",
Description: `
This command supports managing DNS records via 3rd party providers such as
Gandi DNS. It supports listing, creating, updating and removing all types of
DNS records that you might need to manage for managing Co-op Cloud apps.
The following providers are supported:
Gandi DNS https://www.gandi.net
Any new provider can be integrated, we welcome change sets. See the underlying
DNS library documentation for more. It supports many existing providers and
allows to implement new provider support easily.
https://pkg.go.dev/github.com/libdns/libdns
`,
Subcommands: []*cli.Command{
DomainListCommand,
},
}

70
cli/domain/list.go Normal file
View File

@ -0,0 +1,70 @@
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: `
`,
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
},
}

View File

@ -61,3 +61,16 @@ var ChaosFlag = &cli.BoolFlag{
Usage: "Deploy uncommitted recipes changes. Use with care!",
Destination: &Chaos,
}
// DNSProvider specifies a DNS provider.
var DNSProvider string
// DNSProviderFlag selects a DNS provider.
var DNSProviderFlag = &cli.StringFlag{
Name: "provider",
Value: "",
Aliases: []string{"p"},
Usage: "DNS provider",
Destination: &DNSProvider,
Required: true,
}

View File

@ -1,91 +0,0 @@
package server
import (
"github.com/urfave/cli/v2"
)
var provider string
var providerFlag = &cli.StringFlag{
Name: "provider",
Value: "",
Aliases: []string{"p"},
Usage: "Choose a DNS provider (options: Gandi)",
Destination: &provider,
}
var serverDnsListCommand = &cli.Command{
Name: "list",
Usage: "List domains for a server",
Aliases: []string{"ls"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsRemoveCommand = &cli.Command{
Name: "remove",
Usage: "Remove domains for a server",
Aliases: []string{"rm"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsAddCommand = &cli.Command{
Name: "add",
Usage: "Add domains for a server",
Aliases: []string{"a"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsUpdateCommand = &cli.Command{
Name: "update",
Usage: "Update domains for a server",
Aliases: []string{"u"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
providerFlag,
},
Description: `
`,
Action: func(c *cli.Context) error {
// domainName := internal.ValidateDomain(c)
return nil
},
}
var serverDnsCommand = &cli.Command{
Name: "dns",
Aliases: []string{"d"},
Usage: "Manage server domains",
ArgsUsage: "<command>",
Subcommands: []*cli.Command{
serverDnsListCommand,
serverDnsRemoveCommand,
serverDnsAddCommand,
serverDnsUpdateCommand,
},
}

View File

@ -22,6 +22,5 @@ the connections to those servers.
serverAddCommand,
serverListCommand,
serverRemoveCommand,
serverDnsCommand,
},
}