NewCreateCmd creates and returns a create command for Crawlers.
(f *cmdutil.Factory, runF func(*CreateOptions) error)
| 27 | |
| 28 | // NewCreateCmd creates and returns a create command for Crawlers. |
| 29 | func NewCreateCmd(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command { |
| 30 | opts := &CreateOptions{ |
| 31 | IO: f.IOStreams, |
| 32 | Config: f.Config, |
| 33 | CrawlerClient: f.CrawlerClient, |
| 34 | PrintFlags: cmdutil.NewPrintFlags(), |
| 35 | } |
| 36 | |
| 37 | var configFile string |
| 38 | |
| 39 | cmd := &cobra.Command{ |
| 40 | Use: "create <name> -F <file>", |
| 41 | Aliases: []string{"new", "n", "c"}, |
| 42 | Args: cobra.ExactArgs(1), |
| 43 | Short: "Create a crawler", |
| 44 | Long: heredoc.Doc(` |
| 45 | Create a new crawler from the given configuration. |
| 46 | `), |
| 47 | Example: heredoc.Doc(` |
| 48 | # Create a crawler named "my-crawler" with the configuration in the file "config.json" |
| 49 | $ algolia crawler create my-crawler -F config.json |
| 50 | |
| 51 | # Create a crawler from another crawler's configuration |
| 52 | $ algolia crawler get another-crawler --config-only | algolia crawler create my-crawler -F - |
| 53 | `), |
| 54 | RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | opts.Name = args[0] |
| 56 | |
| 57 | b, err := cmdutil.ReadFile(configFile, opts.IO.In) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | err = json.Unmarshal(b, &opts.config) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | if runF != nil { |
| 67 | return runF(opts) |
| 68 | } |
| 69 | |
| 70 | return runCreateCmd(opts) |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | cmd.Flags(). |
| 75 | StringVarP(&configFile, "file", "F", "", "Path to the configuration file (use \"-\" to read from standard input)") |
| 76 | _ = cmd.MarkFlagRequired("file") |
| 77 | cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "Validate and preview the create request without sending it") |
| 78 | opts.PrintFlags.AddFlags(cmd) |
| 79 | |
| 80 | return cmd |
| 81 | } |
| 82 | |
| 83 | func runCreateCmd(opts *CreateOptions) error { |
| 84 | if opts.DryRun { |