NewImportCmd creates and returns an import command for settings
(f *cmdutil.Factory)
| 28 | |
| 29 | // NewImportCmd creates and returns an import command for settings |
| 30 | func NewImportCmd(f *cmdutil.Factory) *cobra.Command { |
| 31 | opts := &ImportOptions{ |
| 32 | IO: f.IOStreams, |
| 33 | Config: f.Config, |
| 34 | SearchClient: f.SearchClient, |
| 35 | } |
| 36 | |
| 37 | var settingsFile string |
| 38 | |
| 39 | cmd := &cobra.Command{ |
| 40 | Use: "import <index> -F <file>", |
| 41 | Args: validators.ExactArgs(1), |
| 42 | ValidArgsFunction: cmdutil.IndexNames(opts.SearchClient), |
| 43 | Annotations: map[string]string{ |
| 44 | "acls": "editSettings", |
| 45 | }, |
| 46 | Short: "Import index settings from a file.", |
| 47 | Example: heredoc.Doc(` |
| 48 | # Import the settings from "settings.json" to the "MOVIES" index |
| 49 | $ algolia settings import MOVIES -F settings.json |
| 50 | `), |
| 51 | RunE: func(cmd *cobra.Command, args []string) error { |
| 52 | opts.Index = args[0] |
| 53 | b, err := cmdutil.ReadFile(settingsFile, opts.IO.In) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | err = json.Unmarshal(b, &opts.Settings) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | return runImportCmd(opts) |
| 62 | }, |
| 63 | } |
| 64 | cmd.Flags(). |
| 65 | StringVarP(&settingsFile, "file", "F", "", "Import settings from a `file` (use \"-\" to read from standard input)") |
| 66 | _ = cmd.MarkFlagRequired("file") |
| 67 | cmd.Flags(). |
| 68 | BoolVarP(&opts.ForwardToReplicas, "forward-to-replicas", "f", false, "Forward the settings to the replicas") |
| 69 | cmd.Flags().BoolVarP(&opts.Wait, "wait", "w", false, "wait for the operation to complete") |
| 70 | |
| 71 | return cmd |
| 72 | } |
| 73 | |
| 74 | func runImportCmd(opts *ImportOptions) error { |
| 75 | client, err := opts.SearchClient() |