Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/cmd/apikeys/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type CreateOptions struct {
Indices []string
Referers []string
Validity time.Duration

PrintFlags *cmdutil.PrintFlags
}

// NewCreateCmd returns a new instance of CreateCmd
Expand All @@ -35,6 +37,7 @@ func NewCreateCmd(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
IO: f.IOStreams,
config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags(),
}
cmd := &cobra.Command{
Use: "create",
Expand Down Expand Up @@ -138,6 +141,8 @@ func NewCreateCmd(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
"seeUnretrievableAttributes": "retrieve unretrievableAttributes for all operations that return records",
}, "can"))

opts.PrintFlags.AddFlags(cmd)

return cmd
}

Expand Down Expand Up @@ -165,6 +170,14 @@ func runCreateCmd(opts *CreateOptions) error {
return err
}

if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil {
p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
return p.Print(opts.IO, res)
}

cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s API key created: %s\n", cs.SuccessIcon(), res.Key)
Expand Down
17 changes: 16 additions & 1 deletion pkg/cmd/dictionary/entries/clear/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type ClearOptions struct {
All bool

DoConfirm bool

PrintFlags *cmdutil.PrintFlags
}

// NewClearCmd creates and returns a clear command for dictionaries' entries.
Expand All @@ -38,6 +40,7 @@ func NewClearCmd(f *cmdutil.Factory, runF func(*ClearOptions) error) *cobra.Comm
IO: f.IOStreams,
Config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags(),
}
cmd := &cobra.Command{
Use: "clear {<dictionary>... | --all} [--confirm]",
Expand Down Expand Up @@ -100,6 +103,8 @@ func NewClearCmd(f *cmdutil.Factory, runF func(*ClearOptions) error) *cobra.Comm
BoolVarP(&confirm, "confirm", "y", false, "Skip the clear dictionary entry confirmation prompt")
cmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Clear all dictionaries")

opts.PrintFlags.AddFlags(cmd)

return cmd
}

Expand Down Expand Up @@ -153,17 +158,27 @@ func runClearCmd(opts *ClearOptions) error {
}
}

responses := make([]search.UpdatedAtResponse, 0, len(dictionaries))
for _, dict := range dictionaries {
batchParams := search.NewBatchDictionaryEntriesParams(
[]search.BatchDictionaryEntriesRequest{},
search.WithBatchDictionaryEntriesParamsClearExistingDictionaryEntries(true),
)
_, err := client.BatchDictionaryEntries(
res, err := client.BatchDictionaryEntries(
client.NewApiBatchDictionaryEntriesRequest(dict, batchParams),
)
if err != nil {
return err
}
responses = append(responses, *res)
}

if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil {
p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
return p.Print(opts.IO, responses)
}

if opts.IO.IsStdoutTTY() {
Expand Down
41 changes: 41 additions & 0 deletions pkg/cmd/dictionary/entries/clear/clear_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clear

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -207,3 +208,43 @@ func Test_runDeleteCmd(t *testing.T) {
})
}
}

// Test_runClearCmd_outputJSON checks that the --output json path emits one
// response per cleared dictionary, in order — i.e. the collection loop.
func Test_runClearCmd_outputJSON(t *testing.T) {
r := httpmock.Registry{}
dicts := []search.DictionaryType{
search.DICTIONARY_TYPE_PLURALS,
search.DICTIONARY_TYPE_COMPOUNDS,
}
for i, d := range dicts {
r.Register(
httpmock.REST("POST", fmt.Sprintf("1/dictionaries/%s/search", d)),
httpmock.JSONResponse(search.SearchDictionaryEntriesResponse{
Hits: []search.DictionaryEntry{
{
ObjectID: "1",
Language: search.SUPPORTED_LANGUAGE_EN.Ptr(),
Words: []string{"foo"},
Type: search.DICTIONARY_ENTRY_TYPE_CUSTOM.Ptr(),
},
},
}),
)
r.Register(
httpmock.REST("POST", fmt.Sprintf("1/dictionaries/%s/batch", d)),
httpmock.JSONResponse(search.UpdatedAtResponse{TaskID: int64((i + 1) * 100)}),
)
}

f, out := test.NewFactory(false, &r, nil, "")
cmd := NewClearCmd(f, nil)
out, err := test.Execute(cmd, "plurals compounds --confirm --output json", out)
require.NoError(t, err)

var got []search.UpdatedAtResponse
require.NoError(t, json.Unmarshal([]byte(out.String()), &got))
assert.Len(t, got, 2)
assert.Equal(t, int64(100), got[0].TaskID)
assert.Equal(t, int64(200), got[1].TaskID)
}
13 changes: 13 additions & 0 deletions pkg/cmd/dictionary/settings/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type SetOptions struct {
DisableStandardEntries []string
EnableStandardEntries []string
ResetStandardEntries bool

PrintFlags *cmdutil.PrintFlags
}

// NewSetCmd creates and returns a set command for dictionaries' settings.
Expand All @@ -29,6 +31,7 @@ func NewSetCmd(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
IO: f.IOStreams,
Config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags(),
}
cmd := &cobra.Command{
Use: "set --disable-standard-entries <languages...> --enable-standard-entries <languages...> [--reset-standard-entries]",
Expand Down Expand Up @@ -112,6 +115,8 @@ func NewSetCmd(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
cmdutil.StringCompletionFunc(supportedLanguages),
)

opts.PrintFlags.AddFlags(cmd)

return cmd
}

Expand Down Expand Up @@ -159,6 +164,14 @@ func runSetCmd(opts *SetOptions) error {

opts.IO.StopProgressIndicator()

if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil {
p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
return p.Print(opts.IO, res)
}

cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s Dictionary settings successfully updated\n", cs.SuccessIcon())
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/indices/clear/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type ClearOptions struct {
Index string
DoConfirm bool
Wait bool

PrintFlags *cmdutil.PrintFlags
}

// NewClearCmd creates and returns a clear command for indices
Expand All @@ -31,6 +33,7 @@ func NewClearCmd(f *cmdutil.Factory, runF func(*ClearOptions) error) *cobra.Comm
IO: f.IOStreams,
Config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags(),
}

var confirm bool
Expand Down Expand Up @@ -73,6 +76,8 @@ func NewClearCmd(f *cmdutil.Factory, runF func(*ClearOptions) error) *cobra.Comm
cmd.Flags().BoolVarP(&confirm, "confirm", "y", false, "Skip confirmation prompt")
cmd.Flags().BoolVarP(&opts.Wait, "wait", "w", false, "Wait for the operation to complete")

opts.PrintFlags.AddFlags(cmd)

return cmd
}

Expand Down Expand Up @@ -116,6 +121,14 @@ func runClearCmd(opts *ClearOptions) error {

opts.IO.StopProgressIndicator()

if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil {
p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
return p.Print(opts.IO, res)
}

cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s Cleared index %s\n", cs.SuccessIcon(), opts.Index)
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/indices/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type CopyOptions struct {
Wait bool

DoConfirm bool

PrintFlags *cmdutil.PrintFlags
}

// NewCopyCmd creates and returns a copy command for indices
Expand All @@ -37,6 +39,7 @@ func NewCopyCmd(f *cmdutil.Factory, runF func(*CopyOptions) error) *cobra.Comman
IO: f.IOStreams,
Config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags(),
}

var confirm bool
Expand Down Expand Up @@ -101,6 +104,8 @@ func NewCopyCmd(f *cmdutil.Factory, runF func(*CopyOptions) error) *cobra.Comman
"rules": "rules",
}, "copy"))

opts.PrintFlags.AddFlags(cmd)

return cmd
}

Expand Down Expand Up @@ -174,6 +179,14 @@ func runCopyCmd(opts *CopyOptions) error {

opts.IO.StopProgressIndicator()

if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil {
p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
return p.Print(opts.IO, res)
}

cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/indices/move/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type MoveOptions struct {
Wait bool

DoConfirm bool

PrintFlags *cmdutil.PrintFlags
}

// NewMoveCmd creates and returns a move command for indices
Expand All @@ -35,6 +37,7 @@ func NewMoveCmd(f *cmdutil.Factory, runF func(*MoveOptions) error) *cobra.Comman
IO: f.IOStreams,
Config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags(),
}

var confirm bool
Expand Down Expand Up @@ -78,6 +81,8 @@ func NewMoveCmd(f *cmdutil.Factory, runF func(*MoveOptions) error) *cobra.Comman
cmd.Flags().BoolVarP(&confirm, "confirm", "y", false, "Skip the move index confirmation prompt")
cmd.Flags().BoolVarP(&opts.Wait, "wait", "w", false, "Wait for the operation to complete")

opts.PrintFlags.AddFlags(cmd)

return cmd
}

Expand Down Expand Up @@ -136,6 +141,14 @@ func runMoveCmd(opts *MoveOptions) error {

opts.IO.StopProgressIndicator()

if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil {
p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
return p.Print(opts.IO, res)
}

if opts.IO.IsStdoutTTY() {
fmt.Fprintf(
opts.IO.Out,
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/objects/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ImportOptions struct {
BatchSize int
AutoObjectIDs bool
Wait bool

PrintFlags *cmdutil.PrintFlags
}

// NewImportCmd creates and returns an import command for records
Expand All @@ -34,6 +36,7 @@ func NewImportCmd(f *cmdutil.Factory) *cobra.Command {
IO: f.IOStreams,
Config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags(),
}

var file string
Expand Down Expand Up @@ -79,6 +82,9 @@ func NewImportCmd(f *cmdutil.Factory) *cobra.Command {
cmd.Flags().
BoolVarP(&opts.AutoObjectIDs, "auto-generate-object-id-if-not-exist", "a", false, "Auto-generate object IDs if they don't exist")
cmd.Flags().BoolVarP(&opts.Wait, "wait", "w", false, "wait for the operation to complete")

opts.PrintFlags.AddFlags(cmd)

return cmd
}

Expand Down Expand Up @@ -146,6 +152,14 @@ func runImportCmd(opts *ImportOptions) error {
return err
}

if opts.PrintFlags.OutputFlagSpecified() && opts.PrintFlags.OutputFormat != nil {
p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
return p.Print(opts.IO, responses)
}

cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(
Expand Down
Loading
Loading