@@ -22,12 +22,14 @@ my $command;
2222my $capi_version ;
2323my $language ;
2424my $output_path ;
25+ my $generator ;
2526my $help ;
2627
2728GetOptions(
2829 ' version=s' => \$capi_version ,
2930 ' language=s' => \$language ,
3031 ' output=s' => \$output_path ,
32+ ' generator=s' => \$generator ,
3133 ' help' => \$help ,
3234) or die usage();
3335
@@ -51,8 +53,20 @@ if ($command eq 'prepare') {
5153 merge_openapi_spec($capi_version );
5254} elsif ($capi_version && $language ) {
5355 # Generate SDK (original functionality)
56+ # Set default generator based on language
57+ if ($language eq ' go' && !$generator ) {
58+ $generator = ' oapi-codegen' ;
59+ } else {
60+ $generator //= ' openapi-generator' ;
61+ }
62+
5463 # Set default output path if not provided
55- $output_path //= File::Spec-> catfile($project_root , ' sdk' , $capi_version , $language );
64+ if ($language eq ' go' ) {
65+ # For Go, append the package name to the path
66+ $output_path //= File::Spec-> catfile($project_root , ' sdk' , $capi_version , $language , ' capiclient' );
67+ } else {
68+ $output_path //= File::Spec-> catfile($project_root , ' sdk' , $capi_version , $language );
69+ }
5670
5771 # Validate inputs
5872 validate_inputs();
@@ -79,6 +93,8 @@ SDK Generation Options:
7993
8094Optional options:
8195 --output=PATH Output directory (default: ./sdk/VERSION/LANGUAGE/)
96+ --generator=GEN Generator to use: openapi-generator, oapi-codegen
97+ (default: oapi-codegen for Go, openapi-generator for others)
8298 --help Show this help message
8399
84100Supported languages:
@@ -100,7 +116,8 @@ Supported languages:
100116Examples:
101117 $0 prepare --version=3.195.0
102118 $0 merge --version=3.195.0
103- $0 --version=3.195.0 --language=go
119+ $0 --version=3.195.0 --language=go # Uses oapi-codegen by default
120+ $0 --version=3.195.0 --language=go --generator=openapi-generator
104121 $0 --version=3.181.0 --language=python --output=/tmp/capi-python-sdk
105122EOF
106123}
@@ -113,12 +130,21 @@ sub validate_inputs {
113130 " Please run './bin/gen merge --version=$capi_version ' first to generate the specification.\n " ;
114131 }
115132
116- # Check if openapi-generator is available
117- my $generator_check = ` which openapi-generator 2>/dev/null || which openapi-generator-cli 2>/dev/null` ;
118- chomp $generator_check ;
119- unless ($generator_check ) {
120- die " Error: openapi-generator-cli not found.\n " .
121- " Please run 'make deps' to install dependencies.\n " ;
133+ # Check if selected generator is available
134+ if ($generator eq ' oapi-codegen' ) {
135+ my $generator_check = ` which oapi-codegen 2>/dev/null` ;
136+ chomp $generator_check ;
137+ unless ($generator_check ) {
138+ die " Error: oapi-codegen not found.\n " .
139+ " Please install it with: go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen\@ latest\n " ;
140+ }
141+ } else {
142+ my $generator_check = ` which openapi-generator 2>/dev/null || which openapi-generator-cli 2>/dev/null` ;
143+ chomp $generator_check ;
144+ unless ($generator_check ) {
145+ die " Error: openapi-generator-cli not found.\n " .
146+ " Please run 'make deps' to install dependencies.\n " ;
147+ }
122148 }
123149}
124150
@@ -128,36 +154,73 @@ sub generate_sdk {
128154 # Create output directory if it doesn't exist
129155 make_path($output_path ) unless -d $output_path ;
130156
131- # Determine which command to use (openapi-generator or openapi-generator-cli)
132- my $generator_cmd = ` which openapi-generator 2>/dev/null` ;
133- chomp $generator_cmd ;
134- $generator_cmd = ' openapi-generator-cli' unless $generator_cmd ;
135-
136- # Build the command
137- my $cmd = " $generator_cmd generate " .
138- " -i '$spec_file ' " .
139- " -g '$language ' " .
140- " -o '$output_path ' " .
141- " --skip-validate-spec" ;
157+ my $cmd ;
158+ my $result ;
142159
143- # Add GitHub configuration
144- my $github_org = ' cloudfoundry-community' ;
145- my $github_repo = " capi-openapi-${language} -client" ;
146- $cmd .= " --git-user-id='$github_org ' --git-repo-id='$github_repo '" ;
147-
148- # Add language-specific options
149- my $additional_props = get_additional_properties($language );
150- $cmd .= " --additional-properties='$additional_props '" if $additional_props ;
151-
152- # Execute the command
153- say " Generating $language SDK for CAPI $capi_version ..." ;
154- say " Command: $cmd " ;
155-
156- my $result = system ($cmd );
160+ if ($generator eq ' oapi-codegen' ) {
161+ # Use oapi-codegen for Go
162+ if ($language ne ' go' ) {
163+ die " Error: oapi-codegen only supports Go language generation.\n " ;
164+ }
165+
166+ # Build output file path
167+ my $output_file = File::Spec-> catfile($output_path , ' client.go' );
168+
169+ # Build the command
170+ $cmd = " oapi-codegen -generate types,client -package capiclient '$spec_file ' > '$output_file '" ;
171+
172+ # Execute the command
173+ say " Generating $language SDK for CAPI $capi_version using oapi-codegen..." ;
174+ say " Command: $cmd " ;
175+
176+ $result = system ($cmd );
177+
178+ # Generate go.mod file if successful
179+ if ($result == 0) {
180+ generate_go_mod($output_path , $capi_version );
181+ }
182+ } else {
183+ # Use openapi-generator
184+ # Determine which command to use (openapi-generator or openapi-generator-cli)
185+ my $generator_cmd = ` which openapi-generator 2>/dev/null` ;
186+ chomp $generator_cmd ;
187+ $generator_cmd = ' openapi-generator-cli' unless $generator_cmd ;
188+
189+ # Build the command
190+ $cmd = " $generator_cmd generate " .
191+ " -i '$spec_file ' " .
192+ " -g '$language ' " .
193+ " -o '$output_path ' " .
194+ " --skip-validate-spec" ;
195+
196+ # Add config file if it exists for Go
197+ my $config_file = File::Spec-> catfile($project_root , ' openapi-generator-config.yml' );
198+ if ($language eq ' go' && -f $config_file ) {
199+ $cmd .= " -c '$config_file '" ;
200+ }
201+
202+ # Add GitHub configuration
203+ my $github_org = ' cloudfoundry-community' ;
204+ my $github_repo = " capi-openapi-${language} -client" ;
205+ $cmd .= " --git-user-id='$github_org ' --git-repo-id='$github_repo '" ;
206+
207+ # Add language-specific options
208+ my $additional_props = get_additional_properties($language );
209+ $cmd .= " --additional-properties='$additional_props '" if $additional_props ;
210+
211+ # Execute the command
212+ say " Generating $language SDK for CAPI $capi_version using openapi-generator..." ;
213+ say " Command: $cmd " ;
214+
215+ $result = system ($cmd );
216+ }
157217
158218 if ($result == 0) {
159219 say " \n SDK generated successfully!" ;
160220 say " Output directory: $output_path " ;
221+ if ($generator eq ' oapi-codegen' ) {
222+ say " Generated file: " . File::Spec-> catfile($output_path , ' client.go' );
223+ }
161224 } else {
162225 die " \n Error: Failed to generate SDK. Exit code: " . ($result >> 8) . " \n " ;
163226 }
@@ -168,7 +231,7 @@ sub get_additional_properties {
168231
169232 # Language-specific additional properties
170233 my %lang_props = (
171- ' go' => ' packageName=capiclient,isGoSubmodule=true, generateInterfaces=true' ,
234+ ' go' => ' packageName=capiclient,generateInterfaces=true' ,
172235 ' python' => ' packageName=capi_client,projectName=capi-client' ,
173236 ' java' => ' groupId=org.cloudfoundry,artifactId=capi-client,artifactVersion=' . $capi_version ,
174237 ' javascript' => ' npmName=\@cloudfoundry/capi-client,npmVersion=' . $capi_version ,
@@ -182,6 +245,55 @@ sub get_additional_properties {
182245 return $lang_props {$lang } // ' ' ;
183246}
184247
248+ sub generate_go_mod {
249+ my ($output_dir , $version ) = @_ ;
250+
251+ my $go_mod_path = File::Spec-> catfile($output_dir , ' go.mod' );
252+
253+ # Extract major version from version string (e.g., 3.195.0 -> v3)
254+ my $major_version = ' ' ;
255+ if ($version =~ / ^(\d +)\. / ) {
256+ $major_version = " /v$1 " if $1 >= 2; # Go modules use /v2, /v3, etc for v2+
257+ }
258+
259+ my $go_mod_content = <<EOF ;
260+ module github.com/cloudfoundry-community/capi-openapi-go-client/capiclient${major_version}
261+
262+ go 1.21
263+
264+ require (
265+ \t github.com/oapi-codegen/runtime v1.1.1
266+ \t gopkg.in/yaml.v2 v2.4.0
267+ )
268+
269+ require (
270+ \t github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
271+ \t github.com/google/uuid v1.5.0 // indirect
272+ )
273+ EOF
274+
275+ say " Creating go.mod file..." ;
276+ open (my $fh , ' >' , $go_mod_path ) or die " Cannot create go.mod: $! " ;
277+ print $fh $go_mod_content ;
278+ close ($fh );
279+
280+ # Run go mod tidy to ensure dependencies are correct
281+ my $original_dir = ` pwd` ;
282+ chomp $original_dir ;
283+ chdir ($output_dir );
284+
285+ say " Running go mod tidy..." ;
286+ my $tidy_result = system (" go mod tidy" );
287+
288+ chdir ($original_dir );
289+
290+ if ($tidy_result == 0) {
291+ say " go.mod created successfully!" ;
292+ } else {
293+ say " Warning: go mod tidy failed. You may need to run it manually." ;
294+ }
295+ }
296+
185297sub check_deps {
186298 my @deps = qw( spruce jq) ;
187299 for my $dep (@deps ) {
0 commit comments