|
| 1 | +package sortingstrategy |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/MakeNowJust/heredoc" |
| 8 | + algoliaComposition "github.com/algolia/algoliasearch-client-go/v4/algolia/composition" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + compinternal "github.com/algolia/cli/pkg/cmd/compositions/internal" |
| 12 | + "github.com/algolia/cli/pkg/cmdutil" |
| 13 | + "github.com/algolia/cli/pkg/config" |
| 14 | + "github.com/algolia/cli/pkg/interactive" |
| 15 | + "github.com/algolia/cli/pkg/iostreams" |
| 16 | + "github.com/algolia/cli/pkg/validators" |
| 17 | +) |
| 18 | + |
| 19 | +// Options holds dependencies and flags for the sorting-strategy command. |
| 20 | +type Options struct { |
| 21 | + Config config.IConfig |
| 22 | + IO *iostreams.IOStreams |
| 23 | + CompositionClient func() (*algoliaComposition.APIClient, error) |
| 24 | + Prompter interactive.Prompter |
| 25 | + CompositionID string |
| 26 | + File string |
| 27 | + Interactive bool |
| 28 | + PrintFlags *cmdutil.PrintFlags |
| 29 | +} |
| 30 | + |
| 31 | +// NewSortingStrategyCmd returns the `compositions sorting-strategy` command. |
| 32 | +func NewSortingStrategyCmd(f *cmdutil.Factory) *cobra.Command { |
| 33 | + opts := &Options{ |
| 34 | + IO: f.IOStreams, |
| 35 | + Config: f.Config, |
| 36 | + CompositionClient: f.CompositionClient, |
| 37 | + Prompter: f.Prompter, |
| 38 | + PrintFlags: cmdutil.NewPrintFlags().WithDefaultOutput("json"), |
| 39 | + } |
| 40 | + |
| 41 | + cmd := &cobra.Command{ |
| 42 | + Use: "sorting-strategy <composition-id>", |
| 43 | + Short: "Set the sorting strategy of a composition", |
| 44 | + Long: heredoc.Doc(` |
| 45 | + Replace a composition's sorting strategy: a mapping of sort labels to the |
| 46 | + indices (or replicas) that implement them. These labels are what the |
| 47 | + ` + "`sortBy`" + ` field on composition rules and search params selects at runtime. |
| 48 | + `), |
| 49 | + Args: validators.ExactArgsWithMsg(1, "compositions sorting-strategy requires a <composition-id> argument."), |
| 50 | + Annotations: map[string]string{ |
| 51 | + "acls": "editSettings", |
| 52 | + }, |
| 53 | + Example: heredoc.Doc(` |
| 54 | + # Set the sorting strategy from a JSON file |
| 55 | + $ algolia compositions sorting-strategy my-comp --file strategy.json |
| 56 | +
|
| 57 | + # Set it from stdin |
| 58 | + $ echo '{"Price (asc)":"products_price_asc"}' | algolia compositions sorting-strategy my-comp --file - |
| 59 | +
|
| 60 | + # Build the mapping interactively |
| 61 | + $ algolia compositions sorting-strategy my-comp --interactive |
| 62 | + `), |
| 63 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 64 | + opts.CompositionID = args[0] |
| 65 | + |
| 66 | + if opts.Interactive == (opts.File != "") { |
| 67 | + return cmdutil.FlagErrorf("exactly one of `--file` or `--interactive` is required") |
| 68 | + } |
| 69 | + if opts.Interactive && !opts.IO.CanPrompt() { |
| 70 | + return cmdutil.FlagErrorf("`--interactive` requires a terminal; use `--file` instead") |
| 71 | + } |
| 72 | + |
| 73 | + return runCmd(opts) |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + cmd.Flags().StringVarP(&opts.File, "file", "f", "", "JSON file path (use - for stdin)") |
| 78 | + cmd.Flags().BoolVarP(&opts.Interactive, "interactive", "i", false, "Build the sorting strategy interactively") |
| 79 | + |
| 80 | + opts.PrintFlags.AddFlags(cmd) |
| 81 | + return cmd |
| 82 | +} |
| 83 | + |
| 84 | +// buildStrategy produces the label->index map either interactively or by reading |
| 85 | +// and parsing the JSON file. |
| 86 | +func buildStrategy(opts *Options) (map[string]string, error) { |
| 87 | + if opts.Interactive { |
| 88 | + // The body is a bare map; the interactive Builder needs a struct, so wrap |
| 89 | + // it and reuse the engine's map handling (count, then key/value pairs). |
| 90 | + var doc struct { |
| 91 | + SortingStrategy map[string]string `json:"sortingStrategy"` |
| 92 | + } |
| 93 | + prompter := opts.Prompter |
| 94 | + if prompter == nil { |
| 95 | + prompter = interactive.NewSurveyPrompter(opts.IO) |
| 96 | + } |
| 97 | + if err := (&interactive.Builder{Prompter: prompter}).Build(&doc); err != nil { |
| 98 | + return nil, fmt.Errorf("building sorting strategy: %w", err) |
| 99 | + } |
| 100 | + return doc.SortingStrategy, nil |
| 101 | + } |
| 102 | + |
| 103 | + raw, err := cmdutil.ReadFile(opts.File, opts.IO.In) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("reading file: %w", err) |
| 106 | + } |
| 107 | + var strategy map[string]string |
| 108 | + if err := json.Unmarshal(raw, &strategy); err != nil { |
| 109 | + return nil, fmt.Errorf("parsing sorting strategy JSON: %w", err) |
| 110 | + } |
| 111 | + return strategy, nil |
| 112 | +} |
| 113 | + |
| 114 | +func runCmd(opts *Options) error { |
| 115 | + strategy, err := buildStrategy(opts) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + client, err := opts.CompositionClient() |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + p, err := opts.PrintFlags.ToPrinter() |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + opts.IO.StartProgressIndicatorWithLabel("Updating sorting strategy") |
| 131 | + |
| 132 | + res, err := client.UpdateSortingStrategyComposition( |
| 133 | + client.NewApiUpdateSortingStrategyCompositionRequest(opts.CompositionID, strategy), |
| 134 | + ) |
| 135 | + if err != nil { |
| 136 | + opts.IO.StopProgressIndicator() |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + opts.IO.StopProgressIndicator() |
| 141 | + |
| 142 | + if err := compinternal.WaitForTask(opts.IO, client, opts.CompositionID, res.TaskID, compinternal.PollInterval, compinternal.Timeout); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + if opts.IO.IsStdoutTTY() { |
| 147 | + cs := opts.IO.ColorScheme() |
| 148 | + fmt.Fprintf(opts.IO.Out, "%s Updated sorting strategy for composition %s\n", cs.SuccessIcon(), opts.CompositionID) |
| 149 | + } |
| 150 | + |
| 151 | + return p.Print(opts.IO, res) |
| 152 | +} |
0 commit comments