|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "regexp" |
| 8 | + |
| 9 | + "github.com/speakeasy-api/parser/apipackage" |
| 10 | + "github.com/speakeasy-api/parser/services/parser" |
| 11 | + "github.com/urfave/cli/v2" |
| 12 | + "gopkg.in/yaml.v2" |
| 13 | +) |
| 14 | + |
| 15 | +type SpeakeasyConfig struct { |
| 16 | + Name string |
| 17 | + Spec struct { |
| 18 | + Version string |
| 19 | + Schemas struct { |
| 20 | + Type string |
| 21 | + Version string |
| 22 | + Output string |
| 23 | + } |
| 24 | + } |
| 25 | + Root string |
| 26 | +} |
| 27 | + |
| 28 | +const configOutputRegexp = "(yaml|json)" |
| 29 | + |
| 30 | +var buildFlags = []cli.Flag{ |
| 31 | + &cli.StringFlag{ |
| 32 | + Name: searchDirFlag, |
| 33 | + Aliases: []string{"d"}, |
| 34 | + Value: "./", |
| 35 | + Usage: "Directories you want to parse,comma separated and general-info file must be in the first one", |
| 36 | + }, |
| 37 | + &cli.StringFlag{ |
| 38 | + Name: excludeFlag, |
| 39 | + Usage: "Exclude directories and files when searching, comma separated", |
| 40 | + }, |
| 41 | + &cli.StringFlag{ |
| 42 | + Name: propertyStrategyFlag, |
| 43 | + Aliases: []string{"p"}, |
| 44 | + Value: parser.CamelCase, |
| 45 | + Usage: "Property Naming Strategy like " + parser.SnakeCase + "," + parser.CamelCase + "," + parser.PascalCase, |
| 46 | + }, |
| 47 | + &cli.StringFlag{ |
| 48 | + Name: outputFlag, |
| 49 | + Aliases: []string{"o"}, |
| 50 | + Value: "./docs", |
| 51 | + Usage: "Output directory for all the generated files(opeanapi.json, opeanapi.yaml)", |
| 52 | + }, |
| 53 | + &cli.StringFlag{ |
| 54 | + Name: configFileFlag, |
| 55 | + Aliases: []string{"c"}, |
| 56 | + Value: speakeasyConfigFileName, |
| 57 | + Usage: "Yaml file to load speakeasy configuration from", |
| 58 | + }, |
| 59 | + &cli.BoolFlag{ |
| 60 | + Name: parseVendorFlag, |
| 61 | + Usage: "Parse go files in 'vendor' folder, disabled by default", |
| 62 | + }, |
| 63 | + &cli.BoolFlag{ |
| 64 | + Name: parseDependencyFlag, |
| 65 | + Aliases: []string{"pd"}, |
| 66 | + Usage: "Parse go files inside dependency folder, disabled by default", |
| 67 | + }, |
| 68 | + &cli.StringFlag{ |
| 69 | + Name: markdownFilesFlag, |
| 70 | + Aliases: []string{"md"}, |
| 71 | + Value: "", |
| 72 | + Usage: "Parse folder containing markdown files to use as description, disabled by default", |
| 73 | + }, |
| 74 | + &cli.StringFlag{ |
| 75 | + Name: codeExampleFilesFlag, |
| 76 | + Aliases: []string{"cef"}, |
| 77 | + Value: "", |
| 78 | + Usage: "Parse folder containing code example files to use for the x-codeSamples extension, disabled by default", |
| 79 | + }, |
| 80 | + &cli.BoolFlag{ |
| 81 | + Name: parseInternalFlag, |
| 82 | + Usage: "Parse go files in internal packages, disabled by default", |
| 83 | + }, |
| 84 | + &cli.BoolFlag{ |
| 85 | + Name: generatedTimeFlag, |
| 86 | + Usage: "Generate timestamp at the top of docs.go, disabled by default", |
| 87 | + }, |
| 88 | + &cli.IntFlag{ |
| 89 | + Name: parseDepthFlag, |
| 90 | + Value: 100, |
| 91 | + Usage: "Dependency parse depth", |
| 92 | + }, |
| 93 | + &cli.StringFlag{ |
| 94 | + Name: instanceNameFlag, |
| 95 | + Value: "", |
| 96 | + Usage: "This parameter can be used to name different schema(openapi) document instances. It is optional.", |
| 97 | + }, |
| 98 | + &cli.StringFlag{ |
| 99 | + Name: overridesFileFlag, |
| 100 | + Value: apipackage.DefaultOverridesFile, |
| 101 | + Usage: "File to read global type overrides from.", |
| 102 | + }, |
| 103 | +} |
| 104 | + |
| 105 | +func readConfig(fileName string) ([]SpeakeasyConfig, error) { |
| 106 | + content, err := os.ReadFile(fileName) |
| 107 | + if err != nil { |
| 108 | + return []SpeakeasyConfig{}, err |
| 109 | + } |
| 110 | + |
| 111 | + reader := bytes.NewReader(content) |
| 112 | + decoder := yaml.NewDecoder(reader) |
| 113 | + |
| 114 | + var configs []SpeakeasyConfig |
| 115 | + config := SpeakeasyConfig{} |
| 116 | + for decoder.Decode(&config) == nil { |
| 117 | + configs = append(configs, config) |
| 118 | + } |
| 119 | + return configs, nil |
| 120 | +} |
| 121 | + |
| 122 | +func buildAction(c *cli.Context) error { |
| 123 | + strategy := c.String(propertyStrategyFlag) |
| 124 | + |
| 125 | + switch strategy { |
| 126 | + case parser.CamelCase, parser.SnakeCase, parser.PascalCase: |
| 127 | + default: |
| 128 | + return fmt.Errorf("not supported %s propertyStrategy", strategy) |
| 129 | + } |
| 130 | + |
| 131 | + var configs, err = readConfig(c.String(configFileFlag)) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + if len(configs) == 0 { |
| 137 | + return fmt.Errorf("no valid configurations found") |
| 138 | + } |
| 139 | + |
| 140 | + for _, config := range configs { |
| 141 | + r, err := regexp.Compile(configOutputRegexp) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + outputTypes := r.FindAllString(config.Spec.Schemas.Output, -1) |
| 147 | + if len(outputTypes) == 0 { |
| 148 | + return fmt.Errorf("no valid output types specified") |
| 149 | + } |
| 150 | + |
| 151 | + err = apipackage.New().Build(&apipackage.Config{ |
| 152 | + SearchDir: c.String(searchDirFlag), |
| 153 | + Excludes: c.String(excludeFlag), |
| 154 | + MainAPIFile: config.Root, |
| 155 | + PropNamingStrategy: strategy, |
| 156 | + // TODO: use the -o flag for the output dir and add property "OutputFile" to |
| 157 | + // parser config to determine the output-file name. |
| 158 | + OutputDir: fmt.Sprintf("schemas/%s", config.Name), |
| 159 | + OutputTypes: outputTypes, |
| 160 | + ParseVendor: c.Bool(parseVendorFlag), |
| 161 | + ParseDependency: c.Bool(parseDependencyFlag), |
| 162 | + MarkdownFilesDir: c.String(markdownFilesFlag), |
| 163 | + ParseInternal: c.Bool(parseInternalFlag), |
| 164 | + GeneratedTime: c.Bool(generatedTimeFlag), |
| 165 | + CodeExampleFilesDir: c.String(codeExampleFilesFlag), |
| 166 | + ParseDepth: c.Int(parseDepthFlag), |
| 167 | + InstanceName: c.String(instanceNameFlag), |
| 168 | + OverridesFile: c.String(overridesFileFlag), |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
0 commit comments