|
| 1 | +/* |
| 2 | +Copyright © 2021 Pete Cornish <outofcoffee@gmail.com> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "gatehill.io/imposter/fileutil" |
| 21 | + imposteropenapi "gatehill.io/imposter/openapi" |
| 22 | + "github.com/sirupsen/logrus" |
| 23 | + "gopkg.in/yaml.v2" |
| 24 | + "opendeps.org/opendeps/model" |
| 25 | + "opendeps.org/opendeps/openapi" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +var flagForceOverwrite bool |
| 34 | + |
| 35 | +// scaffoldCmd represents the scaffold command |
| 36 | +var scaffoldCmd = &cobra.Command{ |
| 37 | + Use: "scaffold DIR", |
| 38 | + Short: "Create an OpenDeps manifest based on OpenAPI files", |
| 39 | + Long: `Creates an OpenDeps manifest based on the OpenAPI specification files in a directory. |
| 40 | +
|
| 41 | +If DIR is not specified, the current working directory is used.`, |
| 42 | + Args: cobra.RangeArgs(0, 1), |
| 43 | + Run: func(cmd *cobra.Command, args []string) { |
| 44 | + var specDir string |
| 45 | + if len(args) == 0 { |
| 46 | + specDir = "." |
| 47 | + } else { |
| 48 | + specDir = args[0] |
| 49 | + } |
| 50 | + specDir, err := filepath.Abs(specDir) |
| 51 | + if err != nil { |
| 52 | + logrus.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + scaffoldManifest(specDir, flagForceOverwrite) |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +func init() { |
| 60 | + scaffoldCmd.Flags().BoolVarP(&flagForceOverwrite, "force-overwrite", "f", false, "Force overwrite of destination file(s) if already exist") |
| 61 | + rootCmd.AddCommand(scaffoldCmd) |
| 62 | +} |
| 63 | + |
| 64 | +func scaffoldManifest(specDir string, forceOverwrite bool) { |
| 65 | + openApiSpecs := imposteropenapi.DiscoverOpenApiSpecs(specDir) |
| 66 | + logrus.Infof("found %d OpenAPI spec(s)", len(openApiSpecs)) |
| 67 | + |
| 68 | + manifest := model.OpenDeps{ |
| 69 | + OpenDeps: model.OpenDepsSchemaVersion, |
| 70 | + Info: &model.Info{ |
| 71 | + Title: "OpenDeps manifest for " + filepath.Base(specDir), |
| 72 | + Version: "1.0.0", |
| 73 | + }, |
| 74 | + Dependencies: make(map[string]model.Dependency), |
| 75 | + } |
| 76 | + |
| 77 | + for _, openApiSpec := range openApiSpecs { |
| 78 | + spec, err := openapi.Parse(openApiSpec) |
| 79 | + if err != nil { |
| 80 | + logrus.Fatalf("error parsing openapi spec: %v: %v", openApiSpec, err) |
| 81 | + } |
| 82 | + |
| 83 | + depName := strings.TrimSuffix(filepath.Base(openApiSpec), filepath.Ext(openApiSpec)) |
| 84 | + manifest.Dependencies[depName] = model.Dependency{ |
| 85 | + Summary: spec.Info.Title, |
| 86 | + Spec: "./" + filepath.Base(openApiSpec), |
| 87 | + Version: spec.Info.Version, |
| 88 | + Availability: &model.Availability{ |
| 89 | + Path: determineAvailabilityPath(spec), |
| 90 | + }, |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + manifestFileName := fileutil.GenerateFilenameAdjacentToFile(specDir, filepath.Join(specDir, "opendeps"), ".yaml", forceOverwrite) |
| 95 | + manifestPath := filepath.Join(specDir, manifestFileName) |
| 96 | + |
| 97 | + file, err := os.Create(manifestPath) |
| 98 | + if err != nil { |
| 99 | + logrus.Fatalf("error creating opendeps manifest file: %v: %v", manifestPath, err) |
| 100 | + } |
| 101 | + defer file.Close() |
| 102 | + |
| 103 | + marshalled, err := yaml.Marshal(manifest) |
| 104 | + _, err = file.Write(marshalled) |
| 105 | + if err != nil { |
| 106 | + logrus.Fatalf("error writing opendeps manifest file: %v: %v", manifestPath, err) |
| 107 | + } |
| 108 | + |
| 109 | + logrus.Infof("wrote OpenDeps manifest file: %v", manifestPath) |
| 110 | +} |
| 111 | + |
| 112 | +func determineAvailabilityPath(spec *openapi.PartialModel) string { |
| 113 | + for path, operations := range spec.Paths { |
| 114 | + if _, found := operations["get"]; found { |
| 115 | + return path |
| 116 | + } |
| 117 | + } |
| 118 | + return "/" |
| 119 | +} |
0 commit comments