diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/AddComponentCommand.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/AddComponentCommand.java index 697cb403..3eea75c8 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/AddComponentCommand.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/AddComponentCommand.java @@ -1,6 +1,5 @@ package io.airlift.airship.configbundler; -import com.google.common.base.Preconditions; import io.airlift.airline.Arguments; import io.airlift.airline.Command; import org.eclipse.jgit.api.Git; @@ -8,6 +7,9 @@ import java.io.File; import java.util.concurrent.Callable; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + @Command(name = "add", description = "Add config for a component") public class AddComponentCommand implements Callable @@ -19,13 +21,13 @@ public class AddComponentCommand public Void call() throws Exception { - Preconditions.checkNotNull(component, "component is null"); + checkNotNull(component, "component is null"); Git git = Git.open(new File(".")); Model model = new Model(git); - Preconditions.checkArgument(model.getBundle(component) == null, "Component already exists: %s", component); + checkArgument(model.getBundle(component) == null, "Component already exists: %s", component); Bundle bundle = model.createBundle(component); model.activateBundle(bundle); diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Bundle.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Bundle.java index 75446cc3..f3cc197f 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Bundle.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Bundle.java @@ -1,6 +1,6 @@ package io.airlift.airship.configbundler; -import com.google.common.base.Preconditions; +import static com.google.common.base.Preconditions.checkNotNull; class Bundle { @@ -10,9 +10,7 @@ class Bundle public Bundle(String name, int version, boolean snapshot) { - Preconditions.checkNotNull(name, "name is null"); - - this.name = name; + this.name = checkNotNull(name, "name is null"); this.version = version; this.snapshot = snapshot; } @@ -29,11 +27,7 @@ public int getVersion() public String getVersionString() { - if (snapshot) { - return version + "-SNAPSHOT"; - } - - return Integer.toString(version); + return Integer.toString(version) + (snapshot ? "-SNAPSHOT" : ""); } public boolean isSnapshot() @@ -41,7 +35,6 @@ public boolean isSnapshot() return snapshot; } - @Override public boolean equals(Object o) { diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ConfigBundler.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ConfigBundler.java index e8a87efe..990bc319 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ConfigBundler.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ConfigBundler.java @@ -18,12 +18,13 @@ public static void main(String[] args) { initializeLogging(false); - Cli> cli = Cli.>builder("configgy") + Cli> cli = Cli.>builder("asconfig") .withDefaultCommand(Help.class) .withCommand(ReleaseCommand.class) .withCommand(InitCommand.class) .withCommand(AddComponentCommand.class) .withCommand(SnapshotCommand.class) + .withCommand(InfoCommand.class) .withCommand(Help.class) .build(); diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Generator.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Generator.java deleted file mode 100644 index 84dce995..00000000 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Generator.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.airlift.airship.configbundler; - -import java.io.IOException; -import java.io.OutputStream; - -interface Generator -{ - void write(OutputStream out) - throws IOException; -} diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/GitUtils.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/GitUtils.java index 8ff33195..80e3a0d5 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/GitUtils.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/GitUtils.java @@ -4,7 +4,7 @@ import com.google.common.base.Preconditions; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableSortedMap; -import com.google.common.io.InputSupplier; +import com.google.common.io.ByteSource; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; @@ -30,7 +30,6 @@ public static Ref getBranch(Repository repository, String name) return repository.getRefDatabase().getRefs(Constants.R_HEADS).get(name); } - public static RevCommit getCommit(Repository repository, Ref ref) { RevWalk revWalk = new RevWalk(repository); @@ -45,14 +44,14 @@ public static RevCommit getCommit(Repository repository, Ref ref) } } - public static InputSupplier getBlob(final Repository repository, final ObjectId objectId) + public static ByteSource getBlob(final Repository repository, final ObjectId objectId) { Preconditions.checkArgument(repository.hasObject(objectId), "object id '%s' not found in git repository", objectId.getName()); - return new InputSupplier() + return new ByteSource() { @Override - public InputStream getInput() + public InputStream openStream() throws IOException { return repository.open(objectId).openStream(); @@ -60,7 +59,6 @@ public InputStream getInput() }; } - public static Map getEntries(Repository repository, RevTree tree) throws IOException { @@ -94,11 +92,11 @@ public static ObjectId findFileObject(Repository repository, RevCommit commit, S return null; } - public static Function> inputStreamSupplierFunction(final Repository repository) + public static Function byteSourceFunction(final Repository repository) { - return new Function>() + return new Function() { - public InputSupplier apply(ObjectId input) + public ByteSource apply(ObjectId input) { return getBlob(repository, input); } diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/IgnoreHiddenFilter.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/IgnoreHiddenFilter.java index aff59eff..71f9017c 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/IgnoreHiddenFilter.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/IgnoreHiddenFilter.java @@ -6,7 +6,7 @@ import java.io.IOException; class IgnoreHiddenFilter - extends TreeFilter + extends TreeFilter { @Override public boolean include(TreeWalk walker) @@ -26,5 +26,4 @@ public TreeFilter clone() { return this; } - } diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/InfoCommand.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/InfoCommand.java new file mode 100644 index 00000000..e9af9d6a --- /dev/null +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/InfoCommand.java @@ -0,0 +1,53 @@ +package io.airlift.airship.configbundler; + +import io.airlift.airline.Arguments; +import io.airlift.airline.Command; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.RepositoryBuilder; +import org.eclipse.jgit.util.FS; + +import java.util.concurrent.Callable; + +import static com.google.common.base.Preconditions.checkNotNull; + +@Command(name = "info", description = "Show metadata ") +public class InfoCommand + implements Callable +{ + @Arguments + public String component; + + @Override + public Void call() + throws Exception + { + Model model = new Model(Git.wrap(new RepositoryBuilder().findGitDir().setFS(FS.DETECTED).build())); + Metadata metadata = model.readMetadata(); + + String groupId = checkNotNull(metadata.getGroupId(), "GroupId missing from .metadata file"); + + System.out.printf("Metadata Group Id: %s%n", metadata.getGroupId()); + + if (metadata.getReleasesRepository() == null) { + System.out.println("No releases repository configured, only local install possible."); + } + else { + System.out.printf("Release Repository Id: %s%n", metadata.getReleasesRepository().getId()); + System.out.printf("Release Repository URL: %s%n", metadata.getReleasesRepository().getUri()); + } + + if (metadata.getSnapshotsRepository() == null) { + System.out.println("No snapshots repository configured, only local install possible."); + } + else { + System.out.printf("Snapshot Repository Id: %s%n", metadata.getSnapshotsRepository().getId()); + System.out.printf("Snapshot Repository URL: %s%n", metadata.getSnapshotsRepository().getUri()); + } + + if (model.isDirty()) { + System.out.println("Local repository is modified."); + } + + return null; + } +} \ No newline at end of file diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/InitCommand.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/InitCommand.java index 2bc8fda8..0de3deaf 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/InitCommand.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/InitCommand.java @@ -1,6 +1,5 @@ package io.airlift.airship.configbundler; -import com.google.common.base.Preconditions; import io.airlift.airline.Command; import io.airlift.airline.Option; import org.eclipse.jgit.api.Git; @@ -9,6 +8,8 @@ import java.io.File; import java.util.concurrent.Callable; +import static com.google.common.base.Preconditions.checkState; + @Command(name = "init", description = "Initialize a git config repository") public class InitCommand implements Callable @@ -46,13 +47,33 @@ public Void call() exists = false; } - Preconditions.checkState(!exists, "A git repository already exists in the current directory"); + checkState(!exists, "A git repository already exists in the current directory"); Model model = new Model(Git.init().call()); + if (releasesRepositoryId != null) { + checkState(releasesRepositoryUri != null, "releaseRepositoryId requires releaseRepositoryUri!"); + + // If the same id is given for release repository and snapshot repository, then the snapshot uri can + // be empty (in that case, the release repository uri is used). + if (releasesRepositoryId.equals(snapshotsRepositoryId) && snapshotsRepositoryUri == null) { + snapshotsRepositoryUri = releasesRepositoryUri; + } + } + else { + System.out.println("No release repository id given! Releases can only be used locally!"); + } + + if (snapshotsRepositoryId != null) { + checkState(snapshotsRepositoryId != null, "snapshotsRepositoryId requires snapshotsRepositoryUri!"); + } + else { + System.out.println("No snapshot repository id given! Snapshots can only be used locally!"); + } + Metadata metadata = new Metadata(groupId, - new Metadata.Repository(snapshotsRepositoryId, snapshotsRepositoryUri), - new Metadata.Repository(releasesRepositoryId, releasesRepositoryUri)); + Metadata.Repository.getRepository(snapshotsRepositoryId, snapshotsRepositoryUri), + Metadata.Repository.getRepository(releasesRepositoryId, releasesRepositoryUri)); model.initialize(metadata); model.checkoutTemplateBranch(); diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Maven.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Maven.java index 91b07d0c..1ebc7fdc 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Maven.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Maven.java @@ -1,7 +1,10 @@ package io.airlift.airship.configbundler; -import com.google.common.base.Preconditions; +import static com.google.common.base.Preconditions.checkState; + import com.google.common.collect.ImmutableList; +import com.google.common.io.ByteSource; + import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader; import org.apache.maven.repository.internal.DefaultVersionRangeResolver; import org.apache.maven.repository.internal.DefaultVersionResolver; @@ -39,6 +42,8 @@ import java.io.File; import java.io.FileOutputStream; +import static com.google.common.base.Preconditions.checkNotNull; + class Maven { private static final String USER_DIR = System.getProperty("user.dir", ""); @@ -60,7 +65,7 @@ public Maven(@Nullable Metadata.Repository snapshotsRepositoryInfo, { validateRepositoryMetadata(snapshotsRepositoryInfo, "snapshots"); validateRepositoryMetadata(releasesRepositoryInfo, "releases"); - + final SettingsBuildingRequest request = new DefaultSettingsBuildingRequest() .setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE) .setUserSettingsFile(DEFAULT_USER_SETTINGS_FILE) @@ -88,32 +93,42 @@ public Maven(@Nullable Metadata.Repository snapshotsRepositoryInfo, session = new MavenRepositorySystemSession() .setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(new LocalRepository(localRepository))); - releasesRepository = makeRemoteRepository(releasesRepositoryInfo, settings.getServer(releasesRepositoryInfo.getId()), false); - snapshotsRepository = makeRemoteRepository(snapshotsRepositoryInfo, settings.getServer(snapshotsRepositoryInfo.getId()), true); + releasesRepository = makeRemoteRepository(releasesRepositoryInfo, settings, false); + snapshotsRepository = makeRemoteRepository(snapshotsRepositoryInfo, settings, true); } - private static void validateRepositoryMetadata(Metadata.Repository info, String name) + private static void validateRepositoryMetadata(@Nullable Metadata.Repository info, String name) { - Preconditions.checkNotNull(info.getId(), "%s repository id is null", name); - Preconditions.checkNotNull(info.getUri(), "%s repository uri is null", name); + if (info != null) { + checkNotNull(info.getId(), "%s repository id is null", name); + checkNotNull(info.getUri(), "%s repository uri is null", name); + } } - - private static RemoteRepository makeRemoteRepository(Metadata.Repository info, Server server, boolean snapshot) + + private static RemoteRepository makeRemoteRepository(@Nullable Metadata.Repository info, Settings settings, boolean snapshot) { + if (info == null) { + return null; + } + + Server server = settings.getServer(info.getId()); + + checkState(server != null, "Could not locate Server for repository %s! (missing maven settings.xml file?)", info.getId()); + return new RemoteRepository(info.getId(), "default", info.getUri()) .setPolicy(true, new RepositoryPolicy(snapshot, RepositoryPolicy.UPDATE_POLICY_ALWAYS, RepositoryPolicy.CHECKSUM_POLICY_FAIL)) .setPolicy(false, new RepositoryPolicy(!snapshot, RepositoryPolicy.UPDATE_POLICY_ALWAYS, RepositoryPolicy.CHECKSUM_POLICY_FAIL)) .setAuthentication(new Authentication(server.getUsername(), server.getPassword())); } - public void upload(String groupId, String artifactId, String version, String extension, Generator writer) + public boolean upload(String groupId, String artifactId, String version, String extension, ByteSource source) throws Exception { File file = File.createTempFile(String.format("%s-%s-%s", groupId, artifactId, version), "." + extension); try { FileOutputStream out = new FileOutputStream(file); try { - writer.write(out); + source.copyTo(out); } finally { out.close(); @@ -124,19 +139,21 @@ public void upload(String groupId, String artifactId, String version, String ext // Install the artifact locally repositorySystem.install(session, new InstallRequest().addArtifact(artifact)); - // Deploy the artifact remotely - DeployRequest deployRequest = new DeployRequest() - .addArtifact(artifact); + // Deploy the artifact remotely if a repository is configured. - if (artifact.isSnapshot()) { - Preconditions.checkNotNull(snapshotsRepository, "snapshots repository uri is null"); - deployRequest.setRepository(snapshotsRepository); + RemoteRepository repository = artifact.isSnapshot() ? snapshotsRepository : releasesRepository; + + if (repository != null) { + DeployRequest deployRequest = new DeployRequest() + .addArtifact(artifact) + .setRepository(repository); + + repositorySystem.deploy(session, deployRequest); + return true; // Uploaded successfully. } else { - Preconditions.checkNotNull(releasesRepository, "releases repository uri is null"); - deployRequest.setRepository(releasesRepository); + return false; // Only installed, not uploaded. } - repositorySystem.deploy(session, deployRequest); } finally { file.delete(); @@ -156,5 +173,4 @@ public boolean contains(String groupId, String artifactId, String version, Strin return true; } - } diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Metadata.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Metadata.java index c779ea91..7acf28a7 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Metadata.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Metadata.java @@ -3,13 +3,14 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.base.Charsets; -import com.google.common.base.Preconditions; import com.google.common.io.Files; import io.airlift.json.JsonCodec; import java.io.File; import java.io.IOException; +import static com.google.common.base.Preconditions.checkNotNull; + public class Metadata { private final String groupId; @@ -57,12 +58,19 @@ public static class Repository private final String uri; @JsonCreator - public Repository(@JsonProperty("id") String id, @JsonProperty("uri") String uri) + public static Repository getRepository(@JsonProperty("id") String id, @JsonProperty("uri") String uri) { - Preconditions.checkNotNull(id, "id is null"); + if (id == null) { + return null; + } + + return new Repository(id, uri); + } - this.uri = uri; - this.id = id; + private Repository(@JsonProperty("id") String id, @JsonProperty("uri") String uri) + { + this.id = checkNotNull(id, "id is null"); + this.uri = checkNotNull(uri, "uri is null"); } @JsonProperty("id") diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Model.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Model.java index acb1940b..594a6b86 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Model.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/Model.java @@ -4,9 +4,8 @@ import com.google.common.base.Predicate; import com.google.common.collect.Maps; import com.google.common.collect.Ordering; -import com.google.common.io.CharStreams; +import com.google.common.io.ByteSource; import com.google.common.io.Files; -import com.google.common.io.InputSupplier; import com.google.common.primitives.Ints; import io.airlift.json.JsonCodec; import org.eclipse.jgit.api.Git; @@ -27,15 +26,13 @@ import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.util.Comparator; import java.util.Map; import static com.google.common.base.Charsets.UTF_8; -import static com.google.common.io.CharStreams.newReaderSupplier; +import static io.airlift.airship.configbundler.GitUtils.byteSourceFunction; import static io.airlift.airship.configbundler.GitUtils.getBlob; import static io.airlift.airship.configbundler.GitUtils.getBranch; -import static io.airlift.airship.configbundler.GitUtils.inputStreamSupplierFunction; class Model { @@ -60,7 +57,7 @@ public Metadata readMetadata() ObjectId objectId = GitUtils.findFileObject(repository, head, METADATA_FILE); - String json = CharStreams.toString(newReaderSupplier(getBlob(repository, objectId), UTF_8)); + String json = getBlob(repository, objectId).asCharSource(UTF_8).read(); Metadata metadata = JsonCodec.jsonCodec(Metadata.class).fromJson(json); Preconditions.checkNotNull(metadata, ".metadata file not found in master branch"); @@ -151,7 +148,7 @@ public void activateBundle(Bundle bundle) git.checkout().setName(bundle.getName()).call(); } - public Map> getEntries(Bundle bundle) + public Map getEntries(Bundle bundle) throws IOException { Ref ref; @@ -166,7 +163,7 @@ public Map> getEntries(Bundle bundle) RevCommit commit = GitUtils.getCommit(git.getRepository(), ref); - return Maps.transformValues(GitUtils.getEntries(git.getRepository(), commit.getTree()), inputStreamSupplierFunction(git.getRepository())); + return Maps.transformValues(GitUtils.getEntries(git.getRepository(), commit.getTree()), byteSourceFunction(git.getRepository())); } public Bundle createNewVersion(Bundle bundle) @@ -249,5 +246,4 @@ public int compare(String tag1, String tag2) return Ints.compare(extractVersion(tag1), extractVersion(tag2)); } } - } diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ReleaseCommand.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ReleaseCommand.java index 42d8f609..43515c86 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ReleaseCommand.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ReleaseCommand.java @@ -1,20 +1,19 @@ package io.airlift.airship.configbundler; -import com.google.common.base.Preconditions; -import com.google.common.io.InputSupplier; +import com.google.common.io.ByteSource; import io.airlift.airline.Arguments; import io.airlift.airline.Command; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.RepositoryBuilder; import org.eclipse.jgit.util.FS; -import java.io.InputStream; import java.util.Map; import java.util.concurrent.Callable; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; import static java.lang.String.format; - @Command(name = "release", description = "Build and release a config bundle") public class ReleaseCommand implements Callable @@ -30,13 +29,10 @@ public Void call() { Model model = new Model(Git.wrap(new RepositoryBuilder().findGitDir().setFS(FS.DETECTED).build())); Metadata metadata = model.readMetadata(); - Metadata.Repository releasesRepository = metadata.getReleasesRepository(); - String groupId = metadata.getGroupId(); - Preconditions.checkNotNull(releasesRepository, "Releases repository missing from .metadata file"); - Preconditions.checkNotNull(groupId, "GroupId missing from .metadata file"); + String groupId = checkNotNull(metadata.getGroupId(), "GroupId missing from .metadata file"); - Preconditions.checkState(!model.isDirty(), "Cannot release with a dirty working tree"); + checkState(!model.isDirty(), "Cannot deploy release with a dirty working tree"); Bundle bundle; @@ -52,7 +48,7 @@ public Void call() if (!bundle.isSnapshot()) { if (maven.contains(groupId, bundle.getName(), bundle.getVersionString(), ARTIFACT_TYPE)) { - throw new RuntimeException(format("%s-%s has already been released", bundle.getName(), bundle.getVersionString())); + throw new RuntimeException(format("%s:%s-%s has already been released", groupId, bundle.getName(), bundle.getVersionString())); } // re-publish version that has already been tagged @@ -62,18 +58,19 @@ public Void call() } // get entries from tag - final Map> entries = model.getEntries(bundle); + final Map entries = model.getEntries(bundle); if (entries.isEmpty()) { throw new RuntimeException("Cannot build an empty config package"); } - maven.upload(groupId, bundle.getName(), bundle.getVersionString(), ARTIFACT_TYPE, new ZipGenerator(entries)); - - System.out.println(format("Uploaded %s-%s", bundle.getName(), bundle.getVersionString())); + if (maven.upload(groupId, bundle.getName(), bundle.getVersionString(), ARTIFACT_TYPE, new ZipPackager(entries))) { + System.out.printf("Uploaded %s:%s:%s%n", groupId, bundle.getName(), bundle.getVersionString()); + } + else { + System.out.printf("Installed %s:%s:%s locally%n", groupId, bundle.getName(), bundle.getVersionString()); + } return null; } - - } diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/SnapshotCommand.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/SnapshotCommand.java index 29472933..7fa350f8 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/SnapshotCommand.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/SnapshotCommand.java @@ -1,18 +1,17 @@ package io.airlift.airship.configbundler; -import com.google.common.base.Preconditions; -import com.google.common.io.InputSupplier; +import com.google.common.io.ByteSource; import io.airlift.airline.Arguments; import io.airlift.airline.Command; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.RepositoryBuilder; import org.eclipse.jgit.util.FS; -import java.io.InputStream; import java.util.Map; import java.util.concurrent.Callable; -import static java.lang.String.format; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; @Command(name = "snapshot", description = "Deploy a snapshot config bundle") public class SnapshotCommand @@ -28,10 +27,9 @@ public Void call() Model model = new Model(Git.wrap(new RepositoryBuilder().findGitDir().setFS(FS.DETECTED).build())); Metadata metadata = model.readMetadata(); - String groupId = metadata.getGroupId(); - Preconditions.checkNotNull(groupId, "GroupId missing from metadata file"); + String groupId = checkNotNull(metadata.getGroupId(), "GroupId missing from .metadata file"); - Preconditions.checkState(!model.isDirty(), "Cannot deploy with a dirty working tree"); + checkState(!model.isDirty(), "Cannot deploy snapshot with a dirty working tree"); Bundle bundle; @@ -42,21 +40,25 @@ public Void call() bundle = model.getBundle(component); } - Preconditions.checkState(bundle.isSnapshot(), "There are not pending changes for bundle %s. Use released version %s:%s instead", - bundle.getName(), bundle.getName(), bundle.getVersionString()); + Maven maven = new Maven(metadata.getSnapshotsRepository(), metadata.getReleasesRepository()); + + checkState(bundle.isSnapshot(), "There are no pending changes for bundle %s. Use released version %s:%s:%s instead", + groupId, bundle.getName(), bundle.getName(), bundle.getVersionString()); - final Map> entries = model.getEntries(bundle); + // get entries from tag + final Map entries = model.getEntries(bundle); if (entries.isEmpty()) { throw new RuntimeException("Cannot build an empty config package"); } - Maven maven = new Maven(metadata.getSnapshotsRepository(), metadata.getReleasesRepository()); - maven.upload(groupId, bundle.getName(), bundle.getVersionString(), ReleaseCommand.ARTIFACT_TYPE, new ZipGenerator(entries)); - - System.out.println(format("Uploaded %s-%s", bundle.getName(), bundle.getVersionString())); + if (maven.upload(groupId, bundle.getName(), bundle.getVersionString(), ReleaseCommand.ARTIFACT_TYPE, new ZipPackager(entries))) { + System.out.printf("Uploaded %s:%s:%s%n", groupId, bundle.getName(), bundle.getVersionString()); + } + else { + System.out.printf("Installed %s:%s:%s locally%n", groupId, bundle.getName(), bundle.getVersionString()); + } return null; - } } diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ZipGenerator.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ZipGenerator.java deleted file mode 100644 index 1c485a16..00000000 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ZipGenerator.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.airlift.airship.configbundler; - -import com.google.common.io.InputSupplier; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Map; - -class ZipGenerator - implements Generator -{ - private final Map> entries; - - public ZipGenerator(Map> entries) - { - this.entries = entries; - } - - @Override - public void write(OutputStream out) - throws IOException - { - ZipPackager.packageEntries(out, entries); - - } -} diff --git a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ZipPackager.java b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ZipPackager.java index d024e415..38eced67 100644 --- a/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ZipPackager.java +++ b/airship-config-bundler/src/main/java/io/airlift/airship/configbundler/ZipPackager.java @@ -1,29 +1,44 @@ package io.airlift.airship.configbundler; +import com.google.common.io.ByteSource; import com.google.common.io.ByteStreams; -import com.google.common.io.InputSupplier; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import static com.google.common.base.Preconditions.checkNotNull; + class ZipPackager + extends ByteSource { - public static void packageEntries(OutputStream output, Map> entries) + public final Map entries; + + ZipPackager(Map entries) + { + this.entries = checkNotNull(entries, "entries is null"); + } + + @Override + public InputStream openStream() throws IOException { - ZipOutputStream out = new ZipOutputStream(output); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + ZipOutputStream out = new ZipOutputStream(baos); - for (Map.Entry> entry : entries.entrySet()) { + for (Map.Entry entry : entries.entrySet()) { String path = entry.getKey(); ZipEntry fileEntry = new ZipEntry(path); out.putNextEntry(fileEntry); ByteStreams.copy(entry.getValue(), out); } out.finish(); - } + return new ByteArrayInputStream(baos.toByteArray()); + } }