Skip to content

Commit

Permalink
Adds JavaScript script engine argument shorthand.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Sep 11, 2021
1 parent 0a6f8d9 commit 3bf7432
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 62 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand Down
55 changes: 23 additions & 32 deletions cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"gatehill.io/imposter/fileutil"
"gatehill.io/imposter/impostermodel"
"gatehill.io/imposter/openapi"
Expand Down Expand Up @@ -45,7 +45,7 @@ If CONFIG_DIR is not specified, the current working directory is used.`,
} else {
configDir, _ = filepath.Abs(args[0])
}
scriptEngine := parseScriptEngine(flagScriptEngine)
scriptEngine := impostermodel.ParseScriptEngine(flagScriptEngine)
createMockConfig(configDir, flagGenerateResources, flagForceOverwrite, scriptEngine)
},
}
Expand All @@ -57,16 +57,6 @@ func init() {
rootCmd.AddCommand(scaffoldCmd)
}

func parseScriptEngine(scriptEngine string) impostermodel.ScriptEngine {
engine := impostermodel.ScriptEngine(scriptEngine)
switch engine {
case impostermodel.ScriptEngineNone, impostermodel.ScriptEngineGroovy, impostermodel.ScriptEngineJavaScript:
return engine
default:
panic(fmt.Errorf("unsupported script engine: %v", flagScriptEngine))
}
}

func createMockConfig(configDir string, generateResources bool, forceOverwrite bool, scriptEngine impostermodel.ScriptEngine) {
openApiSpecs := openapi.DiscoverOpenApiSpecs(configDir)
logrus.Infof("found %d OpenAPI spec(s)", len(openApiSpecs))
Expand All @@ -77,10 +67,19 @@ func createMockConfig(configDir string, generateResources bool, forceOverwrite b
}

func writeMockConfig(specFilePath string, generateResources bool, forceOverwrite bool, scriptEngine impostermodel.ScriptEngine) {
scriptFilePath := writeScriptFile(specFilePath, scriptEngine, forceOverwrite)
scriptFileName := filepath.Base(scriptFilePath)
var scriptFileName string
if impostermodel.IsScriptEngineEnabled(scriptEngine) {
scriptFilePath := writeScriptFile(specFilePath, scriptEngine, forceOverwrite)
scriptFileName = filepath.Base(scriptFilePath)
}

var resources []impostermodel.Resource
if generateResources {
resources = buildResources(specFilePath, scriptEngine, scriptFileName)
} else {
logrus.Debug("skipping resource generation")
}

resources := buildResources(generateResources, specFilePath, scriptEngine, scriptFileName)
config := impostermodel.GenerateConfig(specFilePath, resources, impostermodel.ConfigGenerationOptions{
ScriptEngine: scriptEngine,
ScriptFileName: scriptFileName,
Expand All @@ -100,24 +99,7 @@ func writeMockConfig(specFilePath string, generateResources bool, forceOverwrite
logrus.Infof("wrote Imposter config: %v", configFilePath)
}

func buildResources(generateResources bool, specFilePath string, scriptEngine impostermodel.ScriptEngine, scriptFileName string) []impostermodel.Resource {
var resources []impostermodel.Resource
if generateResources {
resources = impostermodel.GenerateResourcesFromSpec(specFilePath, impostermodel.ResourceGenerationOptions{
ScriptEngine: scriptEngine,
ScriptFileName: scriptFileName,
})
logrus.Debugf("generated %d resources from spec", len(resources))
} else {
logrus.Debug("skipping resource generation")
}
return resources
}

func writeScriptFile(specFilePath string, engine impostermodel.ScriptEngine, forceOverwrite bool) string {
if engine == impostermodel.ScriptEngineNone {
return ""
}
scriptFilePath := impostermodel.BuildScriptFilePath(specFilePath, engine, forceOverwrite)
scriptFile, err := os.Create(scriptFilePath)
if err != nil {
Expand All @@ -140,3 +122,12 @@ logger.debug('headers: ' + context.request.headers);
logrus.Infof("wrote script file: %v", scriptFilePath)
return scriptFilePath
}

func buildResources(specFilePath string, scriptEngine impostermodel.ScriptEngine, scriptFileName string) []impostermodel.Resource {
resources := impostermodel.GenerateResourcesFromSpec(specFilePath, impostermodel.ResourceGenerationOptions{
ScriptEngine: scriptEngine,
ScriptFileName: scriptFileName,
})
logrus.Debugf("generated %d resources from spec", len(resources))
return resources
}
1 change: 1 addition & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand Down
1 change: 1 addition & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
Expand Down
1 change: 1 addition & 0 deletions engine/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package engine

type StartOptions struct {
Expand Down
1 change: 1 addition & 0 deletions engine/docker/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package docker

import (
Expand Down
1 change: 1 addition & 0 deletions fileutil/dirwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fileutil

import (
Expand Down
1 change: 1 addition & 0 deletions fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fileutil

import (
Expand Down
33 changes: 3 additions & 30 deletions impostermodel/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package impostermodel

import (
"gatehill.io/imposter/fileutil"
"gatehill.io/imposter/openapi"
"github.com/sirupsen/logrus"
"path/filepath"
"sigs.k8s.io/yaml"
"strings"
)

type ScriptEngine string

const (
ScriptEngineNone ScriptEngine = "none"
ScriptEngineGroovy ScriptEngine = "groovy"
ScriptEngineJavaScript ScriptEngine = "javascript"
)

type ResourceGenerationOptions struct {
ScriptEngine ScriptEngine
ScriptFileName string
Expand All @@ -50,7 +42,7 @@ func GenerateConfig(specFilePath string, resources []Resource, options ConfigGen
if len(resources) > 0 {
pluginConfig.Resources = resources
} else {
if options.ScriptEngine != ScriptEngineNone {
if IsScriptEngineEnabled(options.ScriptEngine) {
pluginConfig.Response = &ResponseConfig{
ScriptFile: options.ScriptFileName,
}
Expand All @@ -64,25 +56,6 @@ func GenerateConfig(specFilePath string, resources []Resource, options ConfigGen
return config
}

func BuildScriptFilePath(specFilePath string, scriptEngine ScriptEngine, forceOverwrite bool) string {
var scriptFilePath string
if scriptEngine != ScriptEngineNone {
var scriptEngineExt string
switch scriptEngine {
case ScriptEngineJavaScript:
scriptEngineExt = ".js"
break
case ScriptEngineGroovy:
scriptEngineExt = ".groovy"
break
default:
logrus.Fatal("script engine is disabled")
}
scriptFilePath = fileutil.GenerateFilePathAdjacentToFile(specFilePath, scriptEngineExt, forceOverwrite)
}
return scriptFilePath
}

func GenerateResourcesFromSpec(specFilePath string, options ResourceGenerationOptions) []Resource {
var resources []Resource
partialSpec, err := openapi.Parse(specFilePath)
Expand All @@ -96,7 +69,7 @@ func GenerateResourcesFromSpec(specFilePath string, options ResourceGenerationOp
Path: path,
Method: strings.ToUpper(verb),
}
if options.ScriptEngine != ScriptEngineNone {
if IsScriptEngineEnabled(options.ScriptEngine) {
resource.Response = &ResponseConfig{
ScriptFile: options.ScriptFileName,
}
Expand Down
1 change: 1 addition & 0 deletions impostermodel/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package impostermodel

type ResponseConfig struct {
Expand Down
71 changes: 71 additions & 0 deletions impostermodel/scripting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright © 2021 Pete Cornish <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package impostermodel

import (
"fmt"
"gatehill.io/imposter/fileutil"
"github.com/sirupsen/logrus"
)

type ScriptEngine string

const (
ScriptEngineNone ScriptEngine = "none"
ScriptEngineGroovy ScriptEngine = "groovy"
ScriptEngineJavaScript ScriptEngine = "javascript"
)

// shorthand for ScriptEngineJavaScript
const jsScriptEngineAlias = "js"

func ParseScriptEngine(scriptEngine string) ScriptEngine {
engine := ScriptEngine(scriptEngine)
switch engine {
case ScriptEngineNone, ScriptEngineGroovy, ScriptEngineJavaScript:
return engine
case jsScriptEngineAlias:
return ScriptEngineJavaScript
case "":
return ScriptEngineNone
default:
panic(fmt.Errorf("unsupported script engine: %v", scriptEngine))
}
}

func BuildScriptFilePath(specFilePath string, scriptEngine ScriptEngine, forceOverwrite bool) string {
var scriptFilePath string
if IsScriptEngineEnabled(scriptEngine) {
var scriptEngineExt string
switch scriptEngine {
case ScriptEngineJavaScript:
scriptEngineExt = ".js"
break
case ScriptEngineGroovy:
scriptEngineExt = ".groovy"
break
default:
logrus.Fatal("script engine is disabled")
}
scriptFilePath = fileutil.GenerateFilePathAdjacentToFile(specFilePath, scriptEngineExt, forceOverwrite)
}
return scriptFilePath
}

func IsScriptEngineEnabled(engine ScriptEngine) bool {
return len(engine) > 0 && engine != ScriptEngineNone
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand Down
1 change: 1 addition & 0 deletions openapi/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package openapi

import (
Expand Down
1 change: 1 addition & 0 deletions openapi/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package openapi

import (
Expand Down

0 comments on commit 3bf7432

Please sign in to comment.