Skip to content

Commit 86c779c

Browse files
committed
Extract ValidateIndexNameArgs util function
1 parent cf04241 commit 86c779c

File tree

5 files changed

+31
-57
lines changed

5 files changed

+31
-57
lines changed

internal/pkg/cli/command/index/configure.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package index
22

33
import (
44
"context"
5-
"errors"
6-
"strings"
75

86
"github.com/pinecone-io/cli/internal/pkg/utils/exit"
7+
"github.com/pinecone-io/cli/internal/pkg/utils/index"
98
"github.com/pinecone-io/cli/internal/pkg/utils/msg"
109
"github.com/pinecone-io/cli/internal/pkg/utils/pcio"
1110
"github.com/pinecone-io/cli/internal/pkg/utils/presenters"
@@ -31,19 +30,7 @@ func NewConfigureIndexCmd() *cobra.Command {
3130
Use: "configure <name>",
3231
Short: "Configure an existing index with the specified configuration",
3332
Example: "",
34-
Args: func(cmd *cobra.Command, args []string) error {
35-
if len(args) == 0 {
36-
// TODO: start interactive mode. For now just return an error.
37-
return errors.New("please provide an index name")
38-
}
39-
if len(args) > 1 {
40-
return errors.New("please provide only one index name")
41-
}
42-
if strings.TrimSpace(args[0]) == "" {
43-
return errors.New("index name cannot be empty")
44-
}
45-
return nil
46-
},
33+
Args: index.ValidateIndexNameArgs,
4734
Run: func(cmd *cobra.Command, args []string) {
4835
options.name = args[0]
4936
runConfigureIndexCmd(options)

internal/pkg/cli/command/index/create.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package index
22

33
import (
44
"context"
5-
"errors"
6-
"strings"
75

86
"github.com/MakeNowJust/heredoc"
97
"github.com/pinecone-io/cli/internal/pkg/utils/docslinks"
108
"github.com/pinecone-io/cli/internal/pkg/utils/exit"
9+
"github.com/pinecone-io/cli/internal/pkg/utils/index"
1110
"github.com/pinecone-io/cli/internal/pkg/utils/log"
1211
"github.com/pinecone-io/cli/internal/pkg/utils/msg"
1312
"github.com/pinecone-io/cli/internal/pkg/utils/pcio"
@@ -90,18 +89,7 @@ func NewCreateIndexCmd() *cobra.Command {
9089
# create an integrated index
9190
$ pc index create my-index --dimension 1536 --metric cosine --cloud aws --region us-east-1 --model multilingual-e5-large --field_map text=chunk_text
9291
`),
93-
Args: func(cmd *cobra.Command, args []string) error {
94-
if len(args) == 0 {
95-
return errors.New("please provide an index name")
96-
}
97-
if len(args) > 1 {
98-
return errors.New("please provide only one index name")
99-
}
100-
if strings.TrimSpace(args[0]) == "" {
101-
return errors.New("index name cannot be empty")
102-
}
103-
return nil
104-
},
92+
Args: index.ValidateIndexNameArgs,
10593
Run: func(cmd *cobra.Command, args []string) {
10694
options.name = args[0]
10795
runCreateIndexCmd(options)

internal/pkg/cli/command/index/delete.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package index
22

33
import (
44
"context"
5-
"errors"
65
"strings"
76

87
"github.com/pinecone-io/cli/internal/pkg/utils/exit"
8+
"github.com/pinecone-io/cli/internal/pkg/utils/index"
99
"github.com/pinecone-io/cli/internal/pkg/utils/msg"
1010
"github.com/pinecone-io/cli/internal/pkg/utils/sdk"
1111
"github.com/pinecone-io/cli/internal/pkg/utils/style"
@@ -22,19 +22,7 @@ func NewDeleteCmd() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "delete <name>",
2424
Short: "Delete an index",
25-
Args: func(cmd *cobra.Command, args []string) error {
26-
if len(args) == 0 {
27-
// TODO: start interactive mode. For now just return an error.
28-
return errors.New("please provide an index name")
29-
}
30-
if len(args) > 1 {
31-
return errors.New("please provide only one index name")
32-
}
33-
if strings.TrimSpace(args[0]) == "" {
34-
return errors.New("index name cannot be empty")
35-
}
36-
return nil
37-
},
25+
Args: index.ValidateIndexNameArgs,
3826
Run: func(cmd *cobra.Command, args []string) {
3927
options.name = args[0]
4028
ctx := context.Background()

internal/pkg/cli/command/index/describe.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package index
22

33
import (
4-
"errors"
54
"strings"
65

76
"github.com/pinecone-io/cli/internal/pkg/utils/exit"
7+
"github.com/pinecone-io/cli/internal/pkg/utils/index"
88
"github.com/pinecone-io/cli/internal/pkg/utils/msg"
99
"github.com/pinecone-io/cli/internal/pkg/utils/pcio"
1010
"github.com/pinecone-io/cli/internal/pkg/utils/presenters"
@@ -25,19 +25,7 @@ func NewDescribeCmd() *cobra.Command {
2525
cmd := &cobra.Command{
2626
Use: "describe <name>",
2727
Short: "Get configuration and status information for an index",
28-
Args: func(cmd *cobra.Command, args []string) error {
29-
if len(args) == 0 {
30-
// TODO: start interactive mode. For now just return an error.
31-
return errors.New("please provide an index name")
32-
}
33-
if len(args) > 1 {
34-
return errors.New("please provide only one index name")
35-
}
36-
if strings.TrimSpace(args[0]) == "" {
37-
return errors.New("index name cannot be empty")
38-
}
39-
return nil
40-
},
28+
Args: index.ValidateIndexNameArgs,
4129
Run: func(cmd *cobra.Command, args []string) {
4230
options.name = args[0]
4331
pc := sdk.NewPineconeClient()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package index
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
// ValidateIndexNameArgs validates that exactly one non-empty index name is provided as a positional argument.
11+
// This is the standard validation used across all index commands (create, describe, delete, configure).
12+
func ValidateIndexNameArgs(cmd *cobra.Command, args []string) error {
13+
if len(args) == 0 {
14+
return errors.New("please provide an index name")
15+
}
16+
if len(args) > 1 {
17+
return errors.New("please provide only one index name")
18+
}
19+
if strings.TrimSpace(args[0]) == "" {
20+
return errors.New("index name cannot be empty")
21+
}
22+
return nil
23+
}

0 commit comments

Comments
 (0)