Skip to content

Commit

Permalink
HTTP & DNS index CLI completions (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
nt0xa authored Nov 2, 2024
1 parent faeb16b commit 61a2e24
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
40 changes: 40 additions & 0 deletions internal/actions/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions internal/actions/dns_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 5 additions & 4 deletions internal/actions/http_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 61a2e24

Please sign in to comment.