From caeaf7a40cd9566a85c25fb0aa8254a5a6db5779 Mon Sep 17 00:00:00 2001 From: Gustavo Massaneiro Date: Tue, 24 May 2022 02:42:37 -0300 Subject: [PATCH] new temp dir flag to be able to store temporary files inside docker/k8s --- acceptanceTests/diff_test.go | 24 ++++++++++++------------ cli/diff.go | 4 +++- cli/flags.go | 6 ++++++ cli/validate.go | 8 ++++++-- validator/oas31.go | 2 +- validator/validator.go | 5 ++++- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/acceptanceTests/diff_test.go b/acceptanceTests/diff_test.go index 991d7bc..185c241 100644 --- a/acceptanceTests/diff_test.go +++ b/acceptanceTests/diff_test.go @@ -1035,7 +1035,7 @@ func (d *DiffSuite) TestSimpleDiffWithFullFilePath() { d.jsonFile1 = file.NewJsonFile(FILE1) d.jsonFile2 = file.NewJsonFile(FILE2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1053,7 +1053,7 @@ func (d *DiffSuite) TestSimpleDiff() { d.jsonFile1 = file.NewJsonFile(FILE1) d.jsonFile2 = file.NewJsonFile(FILE2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1071,7 +1071,7 @@ func (d *DiffSuite) TestSimpleLooseDiff() { d.jsonFile1 = file.NewJsonFile(FILE_LOOSE1) d.jsonFile2 = file.NewJsonFile(FILE_LOOSE2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1090,7 +1090,7 @@ func (d *DiffSuite) TestSimpleLooseDiffWithFullFilePath() { d.jsonFile1 = file.NewJsonFile(FILE_LOOSE1) d.jsonFile2 = file.NewJsonFile(FILE_LOOSE2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1109,7 +1109,7 @@ func (d *DiffSuite) TestSimpleLooseDiffWithExcludeDescriptions() { d.jsonFile1 = file.NewJsonFile(FILE_LOOSE1) d.jsonFile2 = file.NewJsonFile(FILE_LOOSE2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1128,7 +1128,7 @@ func (d *DiffSuite) TestHeadersDiff() { d.jsonFile1 = file.NewJsonFile(FILE_HEADERS) d.jsonFile2 = file.NewJsonFile(FILE_HEADERS2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1146,7 +1146,7 @@ func (d *DiffSuite) TestHeadersLooseDiff() { d.jsonFile1 = file.NewJsonFile(FILE_HEADERS) d.jsonFile2 = file.NewJsonFile(FILE_HEADERS2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1165,7 +1165,7 @@ func (d *DiffSuite) TestResponsesDiff() { d.jsonFile1 = file.NewJsonFile(FILE_RESPONSES) d.jsonFile2 = file.NewJsonFile(FILE_RESPONSES2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1183,7 +1183,7 @@ func (d *DiffSuite) TestResponsesLooseDiff() { d.jsonFile1 = file.NewJsonFile(FILE_RESPONSES) d.jsonFile2 = file.NewJsonFile(FILE_RESPONSES2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1202,7 +1202,7 @@ func (d *DiffSuite) TestOperationsDiff() { d.jsonFile1 = file.NewJsonFile(FILE_OPERATIONS) d.jsonFile2 = file.NewJsonFile(FILE_OPERATIONS2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1220,7 +1220,7 @@ func (d *DiffSuite) TestOperationsLooseDiff() { d.jsonFile1 = file.NewJsonFile(FILE_OPERATIONS) d.jsonFile2 = file.NewJsonFile(FILE_OPERATIONS2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) @@ -1239,7 +1239,7 @@ func (d *DiffSuite) TestReferencesDiff() { d.jsonFile1 = file.NewJsonFile(FILE_REFERENCES) d.jsonFile2 = file.NewJsonFile(FILE_REFERENCES2) - d.vall = validator.NewValidator() + d.vall = validator.NewValidator("") err := d.vall.InitSchemaFromFile(d.OAS31schemaFile) if err != nil { d.T().Error(err) diff --git a/cli/diff.go b/cli/diff.go index 27222de..6d3dc7b 100644 --- a/cli/diff.go +++ b/cli/diff.go @@ -23,6 +23,7 @@ func RegisterDiffCmd() *cli.Command { Usage: "Diff between two OAS 3.1 files", Action: diffCmd, Flags: []cli.Flag{ + TempDirFlag, BaseFileFlag, SecondFileFlag, TypeFilterFlag, @@ -37,6 +38,7 @@ func RegisterDiffCmd() *cli.Command { } func diffCmd(c *cli.Context) error { + tempDir := c.String(TempDirFlag.Name) baseFilePath := c.String(BaseFileFlag.Name) secondFilePath := c.String(SecondFileFlag.Name) isHtmlOutput := c.Bool(HtmlOutputFlag.Name) @@ -54,7 +56,7 @@ func diffCmd(c *cli.Context) error { return err } - val := validator.NewValidator() + val := validator.NewValidator(tempDir) diff := differentiator.NewDifferentiator(val, differentiator.DifferentiatorOptions{ TypeFilter: strings.ToLower(c.String(TypeFilterFlag.Name)), Loose: c.Bool(LooseFlag.Name), diff --git a/cli/flags.go b/cli/flags.go index 16b4593..e86a174 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -61,4 +61,10 @@ var ( Usage: "summary of endpoints based changelog output", Required: false, } + TempDirFlag = &cli.StringFlag{ + Name: "temp-dir", + Usage: "path for the temporary dir to be used to temporary store oas31 schema files", + Required: false, + Value: "", + } ) diff --git a/cli/validate.go b/cli/validate.go index e3179c1..089dbbc 100644 --- a/cli/validate.go +++ b/cli/validate.go @@ -17,11 +17,15 @@ func RegisterValidateCmd() *cli.Command { Aliases: []string{"v"}, Usage: "Validate file to OAS 3.1 schema", Action: validateCmd, - Flags: []cli.Flag{BaseFileFlag}, + Flags: []cli.Flag{ + TempDirFlag, + BaseFileFlag, + }, } } func validateCmd(c *cli.Context) error { + tempDir := c.String(TempDirFlag.Name) baseFilePath := c.String(BaseFileFlag.Name) jsonFile := file.NewJsonFile(baseFilePath) @@ -30,7 +34,7 @@ func validateCmd(c *cli.Context) error { return err } - val := validator.NewValidator() + val := validator.NewValidator(tempDir) err = val.InitSchemaFromFile(nil) if err != nil { return err diff --git a/validator/oas31.go b/validator/oas31.go index f0e0e5f..e9b012a 100644 --- a/validator/oas31.go +++ b/validator/oas31.go @@ -26,7 +26,7 @@ func (v *validator) InitSchemaFromFile(schemaFile file.JsonFile) error { } // we have to create a temp file because the lib only accepts a file or a url - tempFile, err := ioutil.TempFile("", fmt.Sprintf("*.%s", OAS31_SCHEMA_FILE)) + tempFile, err := ioutil.TempFile(v.tempDir, fmt.Sprintf("*.%s", OAS31_SCHEMA_FILE)) if err != nil { return err } diff --git a/validator/validator.go b/validator/validator.go index d79d3d6..87ed42d 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -18,13 +18,16 @@ type Validator interface { } type validator struct { + tempDir string compiler *jsonschema.Compiler schema *jsonschema.Schema jsonSchema file.JsonFile } -func NewValidator() Validator { +// passing an empty tempDir will use a temporary dir managed by OS, but it won't work inside docker/k8s +func NewValidator(tempDir string) Validator { v := &validator{ + tempDir: tempDir, compiler: jsonschema.NewCompiler(), schema: nil, jsonSchema: nil,