Skip to content

Commit

Permalink
prompt when creating and dropping index
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Schmidt committed Jun 25, 2024
1 parent 02034c1 commit 8e7ee4e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
19 changes: 15 additions & 4 deletions cmd/createIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var createIndexFlags = &struct {
func newCreateIndexFlagSet() *pflag.FlagSet {
flagSet := &pflag.FlagSet{} //nolint:lll // For readability
flagSet.StringVarP(&createIndexFlags.namespace, flags.Namespace, "n", "", commonFlags.DefaultWrapHelpString("The namespace for the index.")) //nolint:lll // For readability
flagSet.StringArrayVarP(&createIndexFlags.sets, flags.Sets, "s", nil, commonFlags.DefaultWrapHelpString("The sets for the index.")) //nolint:lll // For readability
flagSet.StringSliceVarP(&createIndexFlags.sets, flags.Sets, "s", nil, commonFlags.DefaultWrapHelpString("The sets for the index.")) //nolint:lll // For readability
flagSet.StringVarP(&createIndexFlags.indexName, flags.IndexName, "i", "", commonFlags.DefaultWrapHelpString("The name of the index.")) //nolint:lll // For readability
flagSet.StringVarP(&createIndexFlags.vectorField, flags.VectorField, "f", "", commonFlags.DefaultWrapHelpString("The name of the vector field.")) //nolint:lll // For readability
flagSet.Uint32VarP(&createIndexFlags.dimensions, flags.Dimension, "d", 0, commonFlags.DefaultWrapHelpString("The dimension of the vector field.")) //nolint:lll // For readability
Expand Down Expand Up @@ -131,9 +131,6 @@ func newCreateIndexCmd() *cobra.Command {
}
defer adminClient.Close()

ctx, cancel := context.WithTimeout(context.Background(), createIndexFlags.timeout)
defer cancel()

// Inverted to make it easier to understand
var hnswBatchDisabled *bool
if createIndexFlags.hnswBatchEnabled.Val != nil {
Expand All @@ -157,6 +154,20 @@ func newCreateIndexCmd() *cobra.Command {
},
}

if !confirm(fmt.Sprintf(
"Are you sure you want to create the index %s field %s?",
nsAndSetString(
createIndexFlags.namespace,
createIndexFlags.sets,
),
createIndexFlags.vectorField,
)) {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), createIndexFlags.timeout)
defer cancel()

err = adminClient.IndexCreate(
ctx,
createIndexFlags.namespace,
Expand Down
13 changes: 12 additions & 1 deletion cmd/dropIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var dropIndexFlags = &struct {
func newDropIndexFlagSet() *pflag.FlagSet {
flagSet := &pflag.FlagSet{}
flagSet.StringVarP(&dropIndexFlags.namespace, flags.Namespace, "n", "", commonFlags.DefaultWrapHelpString("The namespace for the index.")) //nolint:lll // For readability
flagSet.StringArrayVarP(&dropIndexFlags.sets, flags.Sets, "s", nil, commonFlags.DefaultWrapHelpString("The sets for the index.")) //nolint:lll // For readability
flagSet.StringSliceVarP(&dropIndexFlags.sets, flags.Sets, "s", nil, commonFlags.DefaultWrapHelpString("The sets for the index.")) //nolint:lll // For readability
flagSet.StringVarP(&dropIndexFlags.indexName, flags.IndexName, "i", "", commonFlags.DefaultWrapHelpString("The name of the index.")) //nolint:lll // For readability
flagSet.DurationVar(&dropIndexFlags.timeout, flags.Timeout, time.Second*5, commonFlags.DefaultWrapHelpString("The distance metric for the index.")) //nolint:lll // For readability
flagSet.AddFlagSet(dropIndexFlags.clientFlags.NewClientFlagSet())
Expand Down Expand Up @@ -78,6 +78,17 @@ func newDropIndexCommand() *cobra.Command {
}
defer adminClient.Close()

if !confirm(fmt.Sprintf(
"Are you sure you want to drop the index %s on field %s?",
nsAndSetString(
createIndexFlags.namespace,
createIndexFlags.sets,
),
createIndexFlags.vectorField,
)) {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), dropIndexFlags.timeout)
defer cancel()

Expand Down
24 changes: 24 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log/slog"
"os"
"strings"
"time"

"golang.org/x/term"

Check failure on line 15 in cmd/utils.go

View workflow job for this annotation

GitHub Actions / tests

missing go.sum entry for module providing package golang.org/x/term (imported by asvec/cmd); to add:
Expand Down Expand Up @@ -165,3 +166,26 @@ func parseBothHostSeedsFlag(seeds *flags.SeedsSliceFlag, host *flags.HostPortFla

return hosts, isLoadBalancer
}

func nsAndSetString(namespace string, sets []string) string {
var setStr string

if len(sets) == 0 {
setStr = "*"
} else if len(sets) == 1 {
setStr = sets[0]
} else {
setStr = fmt.Sprintf("%v", sets)
}

return fmt.Sprintf("%s.%s", namespace, setStr)
}

func confirm(prompt string) bool {
var confirm string

fmt.Print(prompt + " (y/n): ")
fmt.Scanln(&confirm)

return strings.ToLower(confirm) == "y"
}

0 comments on commit 8e7ee4e

Please sign in to comment.