| 17 | } |
| 18 | |
| 19 | func Import(ctx context.Context, logger *zap.Logger, _ *config.Config, serviceFactory ServiceFactory, cmdConfigurator CmdConfigurator) *cobra.Command { |
| 20 | |
| 21 | var importCmd = &cobra.Command{ |
| 22 | Use: "import", |
| 23 | Short: "import postman collection to Keploy tests", |
| 24 | Example: "keploy import", |
| 25 | RunE: func(cmd *cobra.Command, _ []string) error { |
| 26 | disableAnsi, _ := (cmd.Flags().GetBool("disable-ansi")) |
| 27 | provider.PrintLogo(os.Stdout, disableAnsi) |
| 28 | return cmd.Help() |
| 29 | }, |
| 30 | } |
| 31 | |
| 32 | var postmanCmd = &cobra.Command{ |
| 33 | Use: "postman", |
| 34 | Short: "import postman collection to Keploy tests", |
| 35 | Example: "keploy import postman", |
| 36 | RunE: func(cmd *cobra.Command, _ []string) error { |
| 37 | disableAnsi, _ := (cmd.Flags().GetBool("disable-ansi")) |
| 38 | provider.PrintLogo(os.Stdout, disableAnsi) |
| 39 | path, _ := cmd.Flags().GetString("path") |
| 40 | if path == "" { |
| 41 | path = "output.json" |
| 42 | } |
| 43 | basePath, _ := cmd.Flags().GetString("base-path") |
| 44 | svc, err := serviceFactory.GetService(ctx, "import") |
| 45 | if err != nil { |
| 46 | utils.LogError(logger, err, "failed to get service", zap.String("command", cmd.Name())) |
| 47 | return nil |
| 48 | } |
| 49 | var tools toolsSvc.Service |
| 50 | var ok bool |
| 51 | if tools, ok = svc.(toolsSvc.Service); !ok { |
| 52 | utils.LogError(logger, nil, "service doesn't satisfy tools service interface") |
| 53 | return nil |
| 54 | } |
| 55 | err = tools.Import(ctx, path, basePath) |
| 56 | if err != nil { |
| 57 | utils.LogError(logger, err, "failed to import Postman collection") |
| 58 | } |
| 59 | return nil |
| 60 | }, |
| 61 | } |
| 62 | importCmd.AddCommand(postmanCmd) |
| 63 | |
| 64 | for _, subCmd := range importCmd.Commands() { |
| 65 | err := cmdConfigurator.AddFlags(subCmd) |
| 66 | if err != nil { |
| 67 | utils.LogError(logger, err, "failed to add flags to command", zap.String("command", subCmd.Name())) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return importCmd |
| 72 | } |