Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Set user-agent and package version
Browse files Browse the repository at this point in the history
The openapi-generator has a parameter to specify the default UserAgent
for the generated code so we don't have to duplicate the metal-go UA in
client projects.

The generator also supports a package version parameter, but the value
only gets written to the API.md doc, so client projects can't read the
version.  To work around this, `version.go` parses the default User-Agent
to extract the package version.
  • Loading branch information
ctreatma committed Mar 21, 2023
1 parent 6e7ef1b commit 13ec58f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ To exclude resources, and optimize response delivery, use the `exclude` query pa
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [OpenAPI-spec](https://www.openapis.org/) from a remote server, you can easily generate an API client.

- API version: 1.0.0
- Package version: 1.0.0
- Package version: 0.7.0
- Build package: org.openapitools.codegen.languages.GoClientCodegen

## Installation
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GIT_ORG=equinix-labs
GIT_REPO=metal-go
PACKAGE_PREFIX=metal
PACKAGE_MAJOR=v1
PACKAGE_VERSION=0.7.0
CRI=docker # nerdctl

OPENAPI_GENERATOR=${CRI} run --rm -u ${CURRENT_UID}:${CURRENT_GID} -v $(CURDIR):/local ${OPENAPI_IMAGE}
Expand Down Expand Up @@ -59,7 +60,9 @@ clean:
gen:
${OPENAPI_GENERATOR} generate -g go \
--package-name ${PACKAGE_MAJOR} \
--http-user-agent "${GIT_REPO}/${PACKAGE_VERSION}" \
-p isGoSubmodule=true \
-p packageVersion=${PACKAGE_VERSION} \
--model-package types \
--api-package models \
--git-user-id ${GIT_ORG} \
Expand Down
2 changes: 1 addition & 1 deletion metal/v1/configuration.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions metal/v1/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1

import "strings"

// Version of the metal-go package. The version is passed in as a parameter
// to the generator, and we parse it out of the default UserAgent at runtime
var Version = "(unused)"

func init() {
defaultConfig := NewConfiguration()
defaultUserAgent := defaultConfig.UserAgent
Version = defaultUserAgent[strings.Index(defaultUserAgent, "/")+1:]
}
19 changes: 19 additions & 0 deletions patches/post/20230321-version.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
diff --git a/metal/v1/version.go b/metal/v1/version.go
new file mode 100644
index 0000000..30f0c65
--- /dev/null
+++ b/metal/v1/version.go
@@ -0,0 +1,13 @@
+package v1
+
+import "strings"
+
+// Version of the metal-go package. The version is passed in as a parameter
+// to the generator, and we parse it out of the default UserAgent at runtime
+var Version = "(unused)"
+
+func init() {
+ defaultConfig := NewConfiguration()
+ defaultUserAgent := defaultConfig.UserAgent
+ Version = defaultUserAgent[strings.Index(defaultUserAgent, "/")+1:]
+}

0 comments on commit 13ec58f

Please sign in to comment.