|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use Getopt::Long; |
| 6 | +use FindBin; |
| 7 | +use File::Spec; |
| 8 | +use File::Basename; |
| 9 | +use Cwd qw(abs_path); |
| 10 | +use File::Path qw(make_path); |
| 11 | + |
| 12 | +$ENV{CAPI_VERSION} ||= '3.181.0'; |
| 13 | +$ENV{OPENAPI_VERSION} ||= '3.1.1'; |
| 14 | + |
| 15 | +# Get project root directory (one level up from bin/) |
| 16 | +my $script_dir = dirname(abs_path($0)); |
| 17 | +my $project_root = dirname($script_dir); |
| 18 | + |
| 19 | +my $specs = { |
| 20 | + 'capi' => { |
| 21 | + version => $ENV{CAPI_VERSION}, |
| 22 | + url => "https://v3-apidocs.cloudfoundry.org/version/${ENV{CAPI_VERSION}}/index.html", |
| 23 | + }, |
| 24 | + 'openapi' => { |
| 25 | + version => $ENV{OPENAPI_VERSION}, |
| 26 | + url => "https://spec.openapis.org/oas/${ENV{OPENAPI_VERSION}}.html", |
| 27 | + }, |
| 28 | +}; |
| 29 | + |
| 30 | +my @endpoints = (qw( |
| 31 | + capi |
| 32 | + admin |
| 33 | + app_usage_events |
| 34 | + apps |
| 35 | + audit_events |
| 36 | + buildpacks |
| 37 | + builds |
| 38 | + deployments |
| 39 | + domains |
| 40 | + droplets |
| 41 | + environment_variable_groups |
| 42 | + feature_flags |
| 43 | + info |
| 44 | + isolation_segments |
| 45 | + organization_quotas |
| 46 | + organizations |
| 47 | + packages |
| 48 | + processes |
| 49 | + resource_matches |
| 50 | + revisions |
| 51 | + roles |
| 52 | + root |
| 53 | + routes |
| 54 | + security_groups |
| 55 | + service_brokers |
| 56 | + service_credential_bindings |
| 57 | + service_instances |
| 58 | + service_offerings |
| 59 | + service_plan_visibility |
| 60 | + service_plans |
| 61 | + service_route_bindings |
| 62 | + service_usage_events |
| 63 | + sidecars |
| 64 | + space_features |
| 65 | + space_quotas |
| 66 | + spaces |
| 67 | + stacks |
| 68 | + tasks |
| 69 | + users |
| 70 | + )); |
| 71 | + |
| 72 | +sub check_deps { |
| 73 | + my @deps = qw(spruce jq openapi-generator); |
| 74 | + for my $dep (@deps) { |
| 75 | + my $cmd = "which $dep"; |
| 76 | + my $result = `$cmd`; |
| 77 | + if ($result eq '') { |
| 78 | + print "Dependency '$dep' not found. Please install it.\n"; |
| 79 | + if ($dep eq 'openapi-generator') { |
| 80 | + print "For openapi-generator, you can install it via:\n"; |
| 81 | + print 'bun install \@openapitools/openapi-generator -g', "\n"; |
| 82 | + } |
| 83 | + exit 1; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +sub fetch_spec { |
| 89 | + my ($spec_name, $spec_version, $spec_url) = @_; |
| 90 | + my $specs_path = "${project_root}/specs"; |
| 91 | + my $spec_file = "${specs_path}/${spec_name}/${spec_version}.html"; |
| 92 | + |
| 93 | + if (-e $spec_file) { |
| 94 | + print "Spec file '${spec_file}' already downloaded.\n"; |
| 95 | + return |
| 96 | + } else { |
| 97 | + print "Fetching spec from '${spec_url}' to '${spec_file}'\n"; |
| 98 | + my $cmd = "curl -sL '${spec_url}' -o '${spec_file}'"; |
| 99 | + printf("Running command: '%s'\n", $cmd); |
| 100 | + system($cmd); |
| 101 | + if ($? == -1) { |
| 102 | + print "Failed to execute: $!\n"; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +sub create_capi_files { |
| 108 | + my ($capi_version) = @_; |
| 109 | + for my $endpoint (@endpoints) { |
| 110 | + my $endpoint_file = "${project_root}/capi/${capi_version}/${endpoint}.yml"; |
| 111 | + `touch ${endpoint_file}`; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +sub prepare { |
| 116 | + for my $spec (keys %$specs) { |
| 117 | + fetch_spec($spec, $specs->{$spec}->{version}, $specs->{$spec}->{url}); |
| 118 | + } |
| 119 | + create_capi_files($specs->{'capi'}->{version}); |
| 120 | +} |
| 121 | + |
| 122 | +sub gen_capi_openapi_spec { |
| 123 | + my ($capi_version) = @_; |
| 124 | + |
| 125 | + my @yamls = (); |
| 126 | + unshift @endpoints, 'capi' unless $endpoints[0] eq 'capi'; |
| 127 | + for my $endpoint (@endpoints) { |
| 128 | + my $endpoint_file = "${project_root}/capi/${capi_version}/${endpoint}.yml"; |
| 129 | + push @yamls, $endpoint_file; |
| 130 | + } |
| 131 | + |
| 132 | + my $capi_openapi_prefix = "${project_root}/capi/${capi_version}.openapi"; |
| 133 | + my $cmd = "spruce merge -m '".join("' '", @yamls)."' > ${capi_openapi_prefix}.yml"; |
| 134 | + printf("Running command: '%s'\n", $cmd); |
| 135 | + system($cmd); |
| 136 | + if ($? == -1) { |
| 137 | + print "Failed to execute: $!\n"; |
| 138 | + } |
| 139 | + $cmd = "spruce json ${capi_openapi_prefix}.yml | jq > ${capi_openapi_prefix}.json"; |
| 140 | + printf("Running command: '%s'\n", $cmd); |
| 141 | + system($cmd); |
| 142 | + if ($? == -1) { |
| 143 | + print "Failed to execute: $!\n"; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +sub generate_go_client { |
| 148 | + my ($capi_version) = @_; |
| 149 | + |
| 150 | + my $spec_file = "${project_root}/capi/${capi_version}.openapi.json"; |
| 151 | + my $output_dir = "${project_root}/clients/go"; |
| 152 | + make_path($output_dir) unless -d $output_dir; |
| 153 | + |
| 154 | + my $cmd = "openapi-generator generate " . |
| 155 | + "-i $spec_file " . |
| 156 | + "-g go " . |
| 157 | + "-o $output_dir " . |
| 158 | + "--additional-properties=packageName=capiclient,isGoSubmodule=true,generateInterfaces=true"; |
| 159 | + # TODO: investigate what additional properties we want here. |
| 160 | + |
| 161 | + printf("Generating Go client...\n"); |
| 162 | + printf("Running command: '%s'\n", $cmd); |
| 163 | + system($cmd); |
| 164 | + if ($? == -1) { |
| 165 | + print "Failed to execute: $!\n"; |
| 166 | + exit 1; |
| 167 | + } |
| 168 | + print "Go client generated successfully in $output_dir\n"; |
| 169 | +} |
| 170 | + |
| 171 | +sub convert { |
| 172 | + print "Convert functionality not implemented yet\n"; |
| 173 | + exit 1; |
| 174 | +} |
| 175 | + |
| 176 | +sub main { |
| 177 | + my $command = shift @ARGV || ''; |
| 178 | + my $subcommand = shift @ARGV || ''; |
| 179 | + my $type = shift @ARGV || ''; |
| 180 | + |
| 181 | + if ($command eq 'prepare') { |
| 182 | + prepare(); |
| 183 | + } |
| 184 | + elsif ($command eq 'convert') { |
| 185 | + convert(); |
| 186 | + } |
| 187 | + elsif ($command eq 'gen' && $subcommand eq 'openapi' && $type eq 'spec') { |
| 188 | + check_deps(); |
| 189 | + gen_capi_openapi_spec($specs->{'capi'}->{version}); |
| 190 | + } |
| 191 | + elsif ($command eq 'gen' && $subcommand eq 'go' && $type eq 'client') { |
| 192 | + check_deps(); |
| 193 | + generate_go_client($specs->{'capi'}->{version}); |
| 194 | + } |
| 195 | + else { |
| 196 | + print "Usage: $0 <command> [subcommand] [type]\n"; |
| 197 | + print "Available commands:\n"; |
| 198 | + print " prepare - Fetch specs and create CAPI files\n"; |
| 199 | + print " convert - Convert spec sections from published api to openapi. (Not implemented yet - still manual)\n"; |
| 200 | + print " gen openapi spec - Generate CAPI OpenAPI specs by merging prepared and converted stubs.\n"; |
| 201 | + print " gen go client - Generate Go client from OpenAPI spec\n"; |
| 202 | + exit 1; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +main(); |
| 207 | + |
| 208 | +__END__ |
| 209 | +
|
0 commit comments