@@ -22,12 +22,14 @@ my $command;
22
22
my $capi_version ;
23
23
my $language ;
24
24
my $output_path ;
25
+ my $generator ;
25
26
my $help ;
26
27
27
28
GetOptions(
28
29
' version=s' => \$capi_version ,
29
30
' language=s' => \$language ,
30
31
' output=s' => \$output_path ,
32
+ ' generator=s' => \$generator ,
31
33
' help' => \$help ,
32
34
) or die usage();
33
35
@@ -51,8 +53,20 @@ if ($command eq 'prepare') {
51
53
merge_openapi_spec($capi_version );
52
54
} elsif ($capi_version && $language ) {
53
55
# 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
+
54
63
# 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
+ }
56
70
57
71
# Validate inputs
58
72
validate_inputs();
@@ -79,6 +93,8 @@ SDK Generation Options:
79
93
80
94
Optional options:
81
95
--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)
82
98
--help Show this help message
83
99
84
100
Supported languages:
@@ -100,7 +116,8 @@ Supported languages:
100
116
Examples:
101
117
$0 prepare --version=3.195.0
102
118
$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
104
121
$0 --version=3.181.0 --language=python --output=/tmp/capi-python-sdk
105
122
EOF
106
123
}
@@ -113,12 +130,21 @@ sub validate_inputs {
113
130
" Please run './bin/gen merge --version=$capi_version ' first to generate the specification.\n " ;
114
131
}
115
132
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
+ }
122
148
}
123
149
}
124
150
@@ -128,36 +154,73 @@ sub generate_sdk {
128
154
# Create output directory if it doesn't exist
129
155
make_path($output_path ) unless -d $output_path ;
130
156
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 ;
142
159
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
+ }
157
217
158
218
if ($result == 0) {
159
219
say " \n SDK generated successfully!" ;
160
220
say " Output directory: $output_path " ;
221
+ if ($generator eq ' oapi-codegen' ) {
222
+ say " Generated file: " . File::Spec-> catfile($output_path , ' client.go' );
223
+ }
161
224
} else {
162
225
die " \n Error: Failed to generate SDK. Exit code: " . ($result >> 8) . " \n " ;
163
226
}
@@ -168,7 +231,7 @@ sub get_additional_properties {
168
231
169
232
# Language-specific additional properties
170
233
my %lang_props = (
171
- ' go' => ' packageName=capiclient,isGoSubmodule=true, generateInterfaces=true' ,
234
+ ' go' => ' packageName=capiclient,generateInterfaces=true' ,
172
235
' python' => ' packageName=capi_client,projectName=capi-client' ,
173
236
' java' => ' groupId=org.cloudfoundry,artifactId=capi-client,artifactVersion=' . $capi_version ,
174
237
' javascript' => ' npmName=\@cloudfoundry/capi-client,npmVersion=' . $capi_version ,
@@ -182,6 +245,55 @@ sub get_additional_properties {
182
245
return $lang_props {$lang } // ' ' ;
183
246
}
184
247
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
+
185
297
sub check_deps {
186
298
my @deps = qw( spruce jq) ;
187
299
for my $dep (@deps ) {
0 commit comments