MCPcopy
hub / github.com/keploy/keploy / Generate

Function Generate

cli/contract.go:35–77  ·  view source on GitHub ↗
(ctx context.Context, logger *zap.Logger, serviceFactory ServiceFactory, cmdConfigurator CmdConfigurator)

Source from the content-addressed store, hash-verified

33}
34
35func Generate(ctx context.Context, logger *zap.Logger, serviceFactory ServiceFactory, cmdConfigurator CmdConfigurator) *cobra.Command {
36 var cmd = &cobra.Command{
37 Use: "generate",
38 Short: "Generate OpenAPI contract from recorded test cases",
39 Example: `keploy contract generate --path .`,
40 PreRunE: func(cmd *cobra.Command, _ []string) error {
41 return cmdConfigurator.Validate(ctx, cmd)
42 },
43 RunE: func(cmd *cobra.Command, _ []string) error {
44 logger.Info("`keploy contract generate` is a community-maintained OpenAPI spec generator; a fully managed, production-grade version is available as part of Keploy Enterprise")
45
46 svc, err := serviceFactory.GetService(ctx, "contract")
47 if err != nil {
48 utils.LogError(logger, err, "failed to get service", zap.String("command", cmd.Name()))
49 return nil
50 }
51 var contract contractSvc.Service
52 var ok bool
53 if contract, ok = svc.(contractSvc.Service); !ok {
54 utils.LogError(logger, nil, "service doesn't satisfy contract service interface")
55 return nil
56 }
57
58 infer, _ := cmd.Flags().GetBool("infer")
59 if infer {
60 err = contract.GenerateFromTests(ctx)
61 } else {
62 err = contract.Generate(ctx, true)
63 }
64
65 if err != nil {
66 utils.LogError(logger, err, "failed to generate contract")
67 return nil
68 }
69
70 return nil
71 },
72 }
73
74 cmd.Flags().Bool("infer", false, "Infer OpenAPI contract from recorded traffic (opt-in; the default path is service-mapping based generation)")
75
76 return cmd
77}
78
79func Download(ctx context.Context, logger *zap.Logger, serviceFactory ServiceFactory, cmdConfigurator CmdConfigurator) *cobra.Command {
80 var cmd = &cobra.Command{

Callers 1

ContractFunction · 0.85

Calls 7

GenerateFromTestsMethod · 0.95
GenerateMethod · 0.95
LogErrorFunction · 0.92
NameMethod · 0.80
ValidateMethod · 0.65
GetServiceMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected