NewTestCmd creates and returns a crawl command for crawlers.
(f *cmdutil.Factory, runF func(*TestOptions) error)
| 30 | |
| 31 | // NewTestCmd creates and returns a crawl command for crawlers. |
| 32 | func NewTestCmd(f *cmdutil.Factory, runF func(*TestOptions) error) *cobra.Command { |
| 33 | opts := &TestOptions{ |
| 34 | IO: f.IOStreams, |
| 35 | Config: f.Config, |
| 36 | CrawlerClient: f.CrawlerClient, |
| 37 | PrintFlags: cmdutil.NewPrintFlags().WithDefaultOutput("json"), |
| 38 | } |
| 39 | |
| 40 | var configFile string |
| 41 | |
| 42 | cmd := &cobra.Command{ |
| 43 | Use: "test <crawler_id> --url <url> [-F <file>]", |
| 44 | Args: cobra.ExactArgs(1), |
| 45 | ValidArgsFunction: cmdutil.CrawlerIDs(opts.CrawlerClient), |
| 46 | Short: "Tests a URL with the crawler's configuration and shows the extracted records.", |
| 47 | Long: heredoc.Doc(` |
| 48 | You can override parts of the configuration to test your changes before updating the configuration. |
| 49 | `), |
| 50 | Example: heredoc.Doc(` |
| 51 | # Test the URL "https://www.example.com" against the crawler with the ID "my-crawler" |
| 52 | $ algolia crawler test my-crawler --url https://www.example.com |
| 53 | |
| 54 | # Test the URL "https://www.example.com" against the crawler with the ID "my-crawler" and override the configuration with the file "config.json" |
| 55 | $ algolia crawler test my-crawler --url https://www.example.com -F config.json |
| 56 | `), |
| 57 | RunE: func(cmd *cobra.Command, args []string) error { |
| 58 | opts.ID = args[0] |
| 59 | |
| 60 | if cmd.Flags().Changed("config") { |
| 61 | b, err := cmdutil.ReadFile(configFile, opts.IO.In) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | err = json.Unmarshal(b, &opts.config) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if runF != nil { |
| 72 | return runF(opts) |
| 73 | } |
| 74 | |
| 75 | return runTestCmd(opts) |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | cmd.Flags().StringVarP(&opts.URL, "url", "u", "", "The URL to test.") |
| 80 | _ = cmd.MarkFlagRequired("url") |
| 81 | |
| 82 | cmd.Flags(). |
| 83 | StringVarP(&configFile, "config", "F", "", "The configuration file to use to override the crawler's configuration. (use \"-\" to read from standard input)") |
| 84 | cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "Validate and preview the test request without sending it") |
| 85 | |
| 86 | opts.PrintFlags.AddFlags(cmd) |
| 87 | |
| 88 | return cmd |
| 89 | } |