@@ -17,17 +17,17 @@ limitations under the License.
1717package cmd
1818
1919import (
20- "fmt"
2120 "gatehill.io/imposter/engine"
2221 "gatehill.io/imposter/engine/docker"
22+ imposterfileutil "gatehill.io/imposter/fileutil"
23+ "gatehill.io/imposter/impostermodel"
2324 "github.com/sirupsen/logrus"
2425 "github.com/spf13/cobra"
2526 "opendeps.org/opendeps/fileutil"
2627 "opendeps.org/opendeps/model"
2728 "os"
2829 "os/signal"
2930 "path/filepath"
30- "strings"
3131 "syscall"
3232)
3333
@@ -53,8 +53,8 @@ by this tool.`,
5353 logrus .Debugf ("reading opendeps manifest: %v\n " , manifestPath )
5454 manifest := model .Parse (manifestPath )
5555
56- bundleManifest (stagingDir , manifestPath )
57- generateMockConfig (stagingDir , manifestPath , manifest )
56+ bundleManifest (stagingDir , manifestPath , flagForceOverwrite )
57+ bundleSpecs (stagingDir , manifestPath , manifest , flagForceOverwrite )
5858
5959 mockEngine := docker .BuildEngine (stagingDir , engine.StartOptions {
6060 Port : 8080 ,
@@ -68,50 +68,51 @@ by this tool.`,
6868 },
6969}
7070
71- func bundleManifest (stagingDir string , manifestPath string ) {
71+ func init () {
72+ rootCmd .AddCommand (mockCmd )
73+ }
74+
75+ func bundleManifest (stagingDir string , manifestPath string , forceOverwrite bool ) {
7276 logrus .Debugf ("bundling manifest: %v" , manifestPath )
7377 err := fileutil .CopyContent (manifestPath , filepath .Join (stagingDir , "opendeps.yaml" ))
7478 if err != nil {
7579 logrus .Fatal (err )
7680 }
77- writeManifestOpenApiSpec (stagingDir )
78- }
7981
80- func init () {
81- rootCmd .AddCommand (mockCmd )
82+ specFileName := "opendeps-openapi-gen.yaml"
83+ writeManifestSpec (stagingDir , specFileName )
84+
85+ resources := []impostermodel.Resource {
86+ {
87+ Path : "/.well-known/opendeps/manifest.yaml" ,
88+ Method : "GET" ,
89+ Response : & impostermodel.ResponseConfig {
90+ StaticFile : "opendeps.yaml" ,
91+ },
92+ },
93+ }
94+ writeMockConfig (filepath .Join (stagingDir , specFileName ), resources , forceOverwrite )
8295}
8396
84- func generateMockConfig (stagingDir string , specFile string , manifest * model.OpenDeps ) string {
97+ func bundleSpecs (stagingDir string , manifestPath string , manifest * model.OpenDeps , forceOverwrite bool ) string {
8598 for _ , dependency := range manifest .Dependencies {
86- openapiNormalisedPath := makeAbsoluteRelativeToFile (dependency .Spec , specFile )
87- logrus .Debugf ("bundling openapi spec: %v\n " , openapiNormalisedPath )
99+ specNormalisedPath := fileutil . MakeAbsoluteRelativeToFile (dependency .Spec , manifestPath )
100+ logrus .Debugf ("bundling openapi spec: %v\n " , specNormalisedPath )
88101
89- openapiFilename := filepath .Base (openapiNormalisedPath )
90- openapiDestFile := filepath .Join (stagingDir , openapiFilename )
91- err := fileutil .CopyContent (openapiNormalisedPath , openapiDestFile )
102+ specDestPath := filepath .Join (stagingDir , filepath .Base (specNormalisedPath ))
103+ err := fileutil .CopyContent (specNormalisedPath , specDestPath )
92104 if err != nil {
93105 panic (err )
94106 }
95107
96- writeMockConfig (stagingDir , openapiFilename )
108+ writeMockConfig (specDestPath , nil , forceOverwrite )
97109 }
98110 return stagingDir
99111}
100112
101- func makeAbsoluteRelativeToFile (filePath string , specFile string ) string {
102- specDir := filepath .Dir (specFile )
103-
104- var openapiNormalisedPath string
105- if strings .HasPrefix (filePath , "./" ) {
106- openapiNormalisedPath = filepath .Join (specDir , strings .TrimPrefix (filePath , "." ))
107- } else {
108- openapiNormalisedPath = filePath
109- }
110- return openapiNormalisedPath
111- }
112-
113- func writeManifestOpenApiSpec (configDir string ) {
114- specFileName := "opendeps-openapi-gen.yaml"
113+ // writeManifestSpec creates an OpenAPI spec describing the well known endpoint
114+ // that serves the manifest.
115+ func writeManifestSpec (configDir string , specFileName string ) {
115116 specFile , err := os .Create (filepath .Join (configDir , specFileName ))
116117 if err != nil {
117118 panic (err )
@@ -141,40 +142,21 @@ paths:
141142 if err != nil {
142143 panic (err )
143144 }
144-
145- err = specFile .Sync ()
146- if err != nil {
147- panic (err )
148- }
149-
150- writeMockConfig (configDir , specFileName )
151145}
152146
153- func writeMockConfig (configDir string , openapiFilename string ) {
154- configFile , err := os .Create (filepath .Join (configDir , fmt .Sprintf ("%v-config.yaml" , openapiFilename )))
147+ func writeMockConfig (specFilePath string , resources []impostermodel.Resource , forceOverwrite bool ) {
148+ configFilePath := imposterfileutil .GenerateFilePathAdjacentToFile (specFilePath , "-config.yaml" , forceOverwrite )
149+ configFile , err := os .Create (configFilePath )
155150 if err != nil {
156151 panic (err )
157152 }
158153 defer configFile .Close ()
159154
160- config := fmt .Sprintf (`---
161- plugin: openapi
162- specFile: "%v"
163-
164- resources:
165- - path: /.well-known/opendeps/manifest.yaml
166- method: GET
167- response:
168- staticFile: opendeps.yaml
169- template: true
170- ` , openapiFilename )
171-
172- _ , err = configFile .WriteString (config )
173- if err != nil {
174- panic (err )
175- }
155+ config := impostermodel .GenerateConfig (specFilePath , resources , impostermodel.ConfigGenerationOptions {
156+ ScriptEngine : impostermodel .ScriptEngineNone ,
157+ })
176158
177- err = configFile .Sync ( )
159+ _ , err = configFile .Write ( config )
178160 if err != nil {
179161 panic (err )
180162 }
0 commit comments