|
| 1 | +///usr/bin/env jbang "$0" "$@" ; exit $? |
| 2 | + |
| 3 | +//DEPS info.picocli:picocli:4.6.3 |
| 4 | +//DEPS https://github.com/jmini/gitlab4j-api/tree/5.3.0-pr1025 |
| 5 | +//JAVA 17 |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileInputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Properties; |
| 16 | +import java.util.concurrent.Callable; |
| 17 | + |
| 18 | +import org.gitlab4j.api.GitLabApi; |
| 19 | +import org.gitlab4j.api.models.*; |
| 20 | + |
| 21 | +import picocli.CommandLine; |
| 22 | +import picocli.CommandLine.Command; |
| 23 | +import picocli.CommandLine.Option; |
| 24 | +import picocli.CommandLine.Parameters; |
| 25 | + |
| 26 | +@Command(name = "MetadataScript", mixinStandardHelpOptions = true, version = "MetadataScript 0.1", description = "Tests for GitLab4J") |
| 27 | +class MetadataScript implements Callable<Integer> { |
| 28 | + |
| 29 | + private static final String CONFIG_FILE_INITIAL_CONTENT = """ |
| 30 | + GITLAB_URL=https://gitlab.com |
| 31 | + GITLAB_AUTH_VALUE= |
| 32 | + """; |
| 33 | + |
| 34 | + @Option(names = { "-c", "--config" }, description = "configuration file location") |
| 35 | + String configFile; |
| 36 | + |
| 37 | + |
| 38 | + @Override |
| 39 | + public Integer call() throws Exception { |
| 40 | + Path file; |
| 41 | + if (configFile != null) { |
| 42 | + file = Paths.get(configFile); |
| 43 | + } else { |
| 44 | + file = configFile(Paths.get("")); |
| 45 | + } |
| 46 | + System.out.println("Reading config: " + file.toAbsolutePath()); |
| 47 | + Properties prop = configProperties(file); |
| 48 | + String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com"); |
| 49 | + String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE"); |
| 50 | + |
| 51 | + try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) { |
| 52 | + Metadata m = gitLabApi.getMetadataApi().getMetadata(); |
| 53 | + System.out.println(m); |
| 54 | + } |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + public static Properties configProperties(Path configFile) { |
| 59 | + try (InputStream is = new FileInputStream(configFile.toFile())) { |
| 60 | + Properties properties = new Properties(); |
| 61 | + properties.load(is); |
| 62 | + return properties; |
| 63 | + } catch (IOException e) { |
| 64 | + throw new IllegalStateException("Can not read config file", e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static Path configFile(Path root) { |
| 69 | + Path configFile = root.toAbsolutePath() |
| 70 | + .resolve("gitlab-config.properties"); |
| 71 | + if (!Files.isRegularFile(configFile)) { |
| 72 | + try { |
| 73 | + Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT); |
| 74 | + throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath())); |
| 75 | + } catch (IOException e) { |
| 76 | + throw new IllegalStateException("Can not write initial config file", e); |
| 77 | + } |
| 78 | + } |
| 79 | + return configFile; |
| 80 | + } |
| 81 | + |
| 82 | + public static String readProperty(Properties p, String key) { |
| 83 | + if (!p.containsKey(key)) { |
| 84 | + throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key)); |
| 85 | + } |
| 86 | + String value = p.getProperty(key); |
| 87 | + if (value == null || value.isBlank()) { |
| 88 | + throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key)); |
| 89 | + } |
| 90 | + return value; |
| 91 | + } |
| 92 | + |
| 93 | + public static String readProperty(Properties p, String key, String defaultValue) { |
| 94 | + return p.getProperty(key, defaultValue); |
| 95 | + } |
| 96 | + |
| 97 | + public static void main(String... args) { |
| 98 | + int exitCode = new CommandLine(new MetadataScript()).execute(args); |
| 99 | + System.exit(exitCode); |
| 100 | + } |
| 101 | +} |
0 commit comments