From 889b79f1e0692ae4431ded2ad526529a1b895091 Mon Sep 17 00:00:00 2001 From: Lennart Espe <3391295+lnsp@users.noreply.github.com> Date: Wed, 10 Aug 2022 00:13:20 +0200 Subject: [PATCH] Add domain removal --- README.md | 2 +- cmd/domains.go | 26 ++++++++++++++++++++++++++ pkg/api/client.go | 10 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b4be74..c49a3b6 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ valar domains link [domain] ([service]) valar domains unlink [domain] ([service]) ``` -#### Remove a domain and all domain links [not implemented] +#### Remove a domain ```bash valar domains delete [domain] ``` diff --git a/cmd/domains.go b/cmd/domains.go index 2daa0cf..51f277d 100644 --- a/cmd/domains.go +++ b/cmd/domains.go @@ -82,6 +82,31 @@ var domainsAddCmd = &cobra.Command{ }), } +var domainsRemoveCmd = &cobra.Command{ + Use: "remove [domain]", + Short: "Removes an existing domain from the project", + Args: cobra.ExactArgs(1), + Run: runAndHandle(func(cmd *cobra.Command, args []string) error { + var project string + // Attempt to read project from file if possible + cfg := &config.ServiceConfig{} + if err := cfg.ReadFromFile(functionConfiguration); err != nil { + // Fall back to global project + project = globalConfiguration.Project() + } else { + project = cfg.Project + } + client, err := globalConfiguration.APIClient() + if err != nil { + return err + } + if err := client.RemoveDomain(project, args[0]); err != nil { + return err + } + return nil + }), +} + var domainsVerifyCmd = &cobra.Command{ Use: "verify [domain]", Short: "Verify a newly added domain", @@ -174,5 +199,6 @@ func initDomainsCmd() { domainsCmd.AddCommand(domainsVerifyCmd) domainsCmd.AddCommand(domainsLinkCmd) domainsCmd.AddCommand(domainsUnlinkCmd) + domainsCmd.AddCommand(domainsRemoveCmd) rootCmd.AddCommand(domainsCmd) } diff --git a/pkg/api/client.go b/pkg/api/client.go index 54ecbb2..a353939 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -324,6 +324,16 @@ func (client *Client) AddDomain(project, domain string) (map[string]string, erro return records, nil } +func (client *Client) RemoveDomain(project, domain string) error { + var ( + path = fmt.Sprintf("/projects/%s/domains/%s", project, domain) + ) + if err := client.request(http.MethodDelete, path, nil, nil); err != nil { + return err + } + return nil +} + func (client *Client) LinkDomain(project, domain, service string) error { var ( path = fmt.Sprintf("/projects/%s/domains/%s/link", project, domain)