-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli][docker] Better expose version/sha information of builds (#5736)
* [cli] Some CLI improvements… * Introduce --version * Introduce --help * Add --sha to version command for short SHA display * Output Version and SHA details * In new --version output, display repo and doc site Additional cleanup to suppress warnings and code quality. * [docker] Adds labels for metadata This adds image labels to store metadata on the online and cli docker images, using standard labels: * org.opencontainers.image.created * org.opencontainers.image.revision * org.opencontainers.image.title * org.opencontainers.image.version These can be inspected via 'docker inspect IMAGE_NAME' and may be useful in tooling/automation or bug reports submitted by users. For more details on these labels, see: https://github.com/opencontainers/image-spec/blob/master/annotations.md * Include version --full for equiv to --version
- Loading branch information
1 parent
4623ec8
commit e14e5fc
Showing
18 changed files
with
297 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.openapitools.codegen; | ||
|
||
public class Constants { | ||
private Constants(){ } | ||
|
||
public static final String CLI_NAME = "openapi-generator-cli"; | ||
public static final String GIT_REPO = "https://github.com/openapitools/openapi-generator"; | ||
public static final String SITE = "https://openapi-generator.tech/"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/BuildInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.openapitools.codegen.cmd; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Locale; | ||
import java.util.Properties; | ||
|
||
import static org.openapitools.codegen.Constants.*; | ||
|
||
/** | ||
* Presents build-time information | ||
*/ | ||
@SuppressWarnings({"java:S108"}) | ||
public class BuildInfo { | ||
private static final String VERSION_PLACEHOLDER = "${project.version}"; | ||
private static final String UNSET = "unset"; | ||
private static final String UNKNOWN = "unknown"; | ||
|
||
private static final Properties properties = new Properties(); | ||
|
||
static { | ||
try (InputStream is = BuildInfo.class.getResourceAsStream("/version.properties")) { | ||
Properties versionProps = new Properties(); | ||
versionProps.load(is); | ||
properties.putAll(versionProps); | ||
} catch (IOException ignored) { | ||
} | ||
try (InputStream is = BuildInfo.class.getResourceAsStream("/openapi-generator-git.properties")) { | ||
Properties gitProps = new Properties(); | ||
gitProps.load(is); | ||
properties.putAll(gitProps); | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
|
||
/** | ||
* Gets the version of the toolset. | ||
* | ||
* @return A semver string | ||
*/ | ||
public String getVersion() { | ||
String version = (String) properties.getOrDefault("version", UNKNOWN); | ||
if (VERSION_PLACEHOLDER.equals(version)) { | ||
return UNSET; | ||
} else { | ||
return version; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the git commit SHA1 hash. Useful for differentiating between SNAPSHOT builds. | ||
* | ||
* @return A short git SHA | ||
*/ | ||
public String getSha() { | ||
return (String) properties.getOrDefault("git.commit.id.abbrev", UNKNOWN); | ||
} | ||
|
||
/** | ||
* Gets the time when this tool was built. | ||
* | ||
* @return The time as {@link OffsetDateTime}, or {@link OffsetDateTime#MIN} if metadata cannot be parsed. | ||
*/ | ||
public OffsetDateTime getBuildTime() { | ||
try { | ||
String time = (String) properties.getOrDefault("git.build.time", ""); | ||
return OffsetDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ROOT)); | ||
} catch (DateTimeParseException e) { | ||
return OffsetDateTime.MIN; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the full version display text, as one would expect from a '--version' CLI option | ||
* | ||
* @return Human-readable version display information | ||
*/ | ||
public String versionDisplayText() { | ||
StringBuilder sb = new StringBuilder(CLI_NAME); | ||
sb.append(" ").append(this.getVersion()).append(System.lineSeparator()); | ||
sb.append(" commit : ").append(this.getSha()).append(System.lineSeparator()); | ||
sb.append(" built : ").append(this.getBuildTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)).append(System.lineSeparator()); | ||
sb.append(" source : ").append(GIT_REPO).append(System.lineSeparator()); | ||
sb.append(" docs : ").append(SITE).append(System.lineSeparator()); | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/GlobalOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.openapitools.codegen.cmd; | ||
|
||
import io.airlift.airline.Option; | ||
|
||
import static io.airlift.airline.OptionType.GLOBAL; | ||
|
||
public class GlobalOptions { | ||
@Option(type = GLOBAL, name = "--version", description = "Display full version output", hidden = true) | ||
public boolean version; | ||
|
||
@Option(type = GLOBAL, name = "--help", description = "Display help about the tool", hidden = true) | ||
public boolean help; | ||
} |
18 changes: 18 additions & 0 deletions
18
modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/HelpCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.openapitools.codegen.cmd; | ||
|
||
import io.airlift.airline.Command; | ||
import io.airlift.airline.Help; | ||
|
||
import javax.inject.Inject; | ||
|
||
@Command(name = "help", description = "Display help information about openapi-generator") | ||
public class HelpCommand extends OpenApiGeneratorCommand { | ||
|
||
@Inject | ||
public Help help; | ||
|
||
@Override | ||
public void execute() { | ||
help.call(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.