refactor: move cmd handlers to cmd/cli/commands/ following tag_file architecture
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/output"
|
||||
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func Cmd(prov *provider.Provider, out **output.Printer) *cobra.Command {
|
||||
domainCmd := &cobra.Command{
|
||||
Use: "domain",
|
||||
Short: "Manage domains",
|
||||
}
|
||||
|
||||
domainListCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List domains",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
svc := (*prov).Domain()
|
||||
if svc == nil {
|
||||
return fmt.Errorf("domain management not supported by current provider")
|
||||
}
|
||||
domains, err := svc.List(cmd.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return (*out).Print(domains)
|
||||
},
|
||||
}
|
||||
|
||||
dnsCmd := &cobra.Command{
|
||||
Use: "dns <domain>",
|
||||
Short: "Manage DNS records",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
}
|
||||
|
||||
dnsListCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List DNS records",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
svc := (*prov).Domain()
|
||||
if svc == nil {
|
||||
return fmt.Errorf("domain management not supported")
|
||||
}
|
||||
records, err := svc.DNSList(cmd.Context(), args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return (*out).Print(records)
|
||||
},
|
||||
}
|
||||
|
||||
dnsAddCmd := &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add a DNS record",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
svc := (*prov).Domain()
|
||||
if svc == nil {
|
||||
return fmt.Errorf("domain management not supported")
|
||||
}
|
||||
recType, _ := cmd.Flags().GetString("type")
|
||||
name, _ := cmd.Flags().GetString("name")
|
||||
value, _ := cmd.Flags().GetString("value")
|
||||
ttl, _ := cmd.Flags().GetInt("ttl")
|
||||
return svc.DNSAdd(cmd.Context(), args[0],
|
||||
provider.DNSRecord{Type: recType, Name: name, Value: value, TTL: ttl})
|
||||
},
|
||||
}
|
||||
|
||||
dnsDeleteCmd := &cobra.Command{
|
||||
Use: "delete <record-id>",
|
||||
Short: "Delete a DNS record",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
svc := (*prov).Domain()
|
||||
if svc == nil {
|
||||
return fmt.Errorf("domain management not supported")
|
||||
}
|
||||
return svc.DNSDelete(cmd.Context(), args[0], args[1])
|
||||
},
|
||||
}
|
||||
|
||||
dnsAddCmd.Flags().String("type", "A", "Record type (A, CNAME, TXT)")
|
||||
dnsAddCmd.Flags().String("name", "@", "Record name")
|
||||
dnsAddCmd.Flags().String("value", "", "Record value")
|
||||
dnsAddCmd.Flags().Int("ttl", 600, "TTL in seconds")
|
||||
dnsAddCmd.MarkFlagRequired("value")
|
||||
|
||||
dnsCmd.AddCommand(dnsListCmd)
|
||||
dnsCmd.AddCommand(dnsAddCmd)
|
||||
dnsCmd.AddCommand(dnsDeleteCmd)
|
||||
domainCmd.AddCommand(domainListCmd)
|
||||
domainCmd.AddCommand(dnsCmd)
|
||||
|
||||
return domainCmd
|
||||
}
|
||||
Reference in New Issue
Block a user