From 61a2e246ab77e18532c7f65b2ed506d6074b11d2 Mon Sep 17 00:00:00 2001 From: Anton Prokhorov Date: Sat, 2 Nov 2024 12:48:18 +0000 Subject: [PATCH] HTTP & DNS index CLI completions (#182) --- internal/actions/completion.go | 40 +++++++++++++++++++++++++++++++++ internal/actions/dns_records.go | 9 ++++---- internal/actions/http_routes.go | 9 ++++---- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/internal/actions/completion.go b/internal/actions/completion.go index ce527c1..871876a 100644 --- a/internal/actions/completion.go +++ b/internal/actions/completion.go @@ -37,6 +37,46 @@ func completePayloadName(acts *Actions) completionFunc { } } +func completeDNSRecord(acts *Actions) completionFunc { + return func( + cmd *cobra.Command, + args []string, + _ string, + ) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveError + } + + payload, err := cmd.Flags().GetString("payload") + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + records, err := (*acts).DNSRecordsList(cmd.Context(), DNSRecordsListParams{ + PayloadName: payload, + }) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + completions := make([]string, len(records)) + + for i, r := range records { + completions[i] = fmt.Sprintf( + "%d\t%s.%s %d IN %s %s", + r.Index, + r.Name, + r.PayloadSubdomain, + r.TTL, + r.Type, + strings.Join(r.Values, " "), + ) + } + + return completions, cobra.ShellCompDirectiveNoFileComp + } +} + func completeHTTPRoute(acts *Actions) completionFunc { return func( cmd *cobra.Command, diff --git a/internal/actions/dns_records.go b/internal/actions/dns_records.go index 7f635d6..49be2ae 100644 --- a/internal/actions/dns_records.go +++ b/internal/actions/dns_records.go @@ -122,10 +122,11 @@ func (r DNSRecordsDeleteResult) ResultID() string { func DNSRecordsDeleteCommand(acts *Actions, p *DNSRecordsDeleteParams, local bool) (*cobra.Command, PrepareCommandFunc) { cmd := &cobra.Command{ - Use: "del INDEX", - Short: "Delete DNS record", - Long: "Delete DNS record identified by INDEX", - Args: oneArg("INDEX"), + Use: "del INDEX", + Short: "Delete DNS record", + Long: "Delete DNS record identified by INDEX", + Args: oneArg("INDEX"), + ValidArgsFunction: completeDNSRecord(acts), } cmd.Flags().StringVarP(&p.PayloadName, "payload", "p", "", "Payload name") diff --git a/internal/actions/http_routes.go b/internal/actions/http_routes.go index 21a43a6..fe36d6d 100644 --- a/internal/actions/http_routes.go +++ b/internal/actions/http_routes.go @@ -317,10 +317,11 @@ func (r HTTPRoutesDeleteResult) ResultID() string { func HTTPRoutesDeleteCommand(acts *Actions, p *HTTPRoutesDeleteParams, local bool) (*cobra.Command, PrepareCommandFunc) { cmd := &cobra.Command{ - Use: "del INDEX", - Short: "Delete HTTP route", - Long: "Delete HTTP route identified by INDEX", - Args: oneArg("INDEX"), + Use: "del INDEX", + Short: "Delete HTTP route", + Long: "Delete HTTP route identified by INDEX", + Args: oneArg("INDEX"), + ValidArgsFunction: completeHTTPRoute(acts), } cmd.Flags().StringVarP(&p.PayloadName, "payload", "p", "", "Payload name")