Skip to content

Commit

Permalink
Merge pull request quarkiverse#39 from iocanel/undeclared-build-item
Browse files Browse the repository at this point in the history
CLI commands requiring scm report an error in its abscence
  • Loading branch information
iocanel authored Sep 25, 2024
2 parents 143acbb + cef0d17 commit a1a4870
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import io.quarkiverse.argocd.cli.handlers.GetArgoCDApplicationHandler;
import io.quarkiverse.argocd.deployment.utils.Serialization;
import io.quarkiverse.argocd.spi.ArgoCDApplicationListBuildItem;
import io.quarkiverse.argocd.v1alpha1.Application;
import io.quarkiverse.argocd.v1alpha1.ApplicationList;
import io.quarkus.bootstrap.BootstrapException;
import io.quarkus.bootstrap.app.AugmentAction;
import io.quarkus.bootstrap.app.CuratedApplication;
import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import picocli.CommandLine.Command;
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Parameters;

@Command(name = "generate", sortOptions = false, mixinStandardHelpOptions = false, header = "Generate ArgoCD Application.", headerHeading = "%n", commandListHeading = "%nCommands:%n", synopsisHeading = "%nUsage: ", optionListHeading = "%nOptions:%n")
Expand All @@ -28,66 +16,6 @@ public class GenerateCommand extends GenerationBaseCommand {
@Parameters(arity = "0..1", paramLabel = "GENERATION_PATH", description = " The path to generate the ArgoCD Application")
Optional<String> generationPath = Optional.of(".argocd");

public Integer call() {
Path projectRoot = getWorkingDirectory();
BuildTool buildTool = QuarkusProjectHelper.detectExistingBuildTool(projectRoot);
if (buildTool == null) {
System.out.println("Unable to determine the build tool used for the project at " + projectRoot);
return ExitCode.USAGE;
}

Path targetDirecotry = projectRoot.resolve(buildTool.getBuildDirectory());
QuarkusBootstrap quarkusBootstrap = QuarkusBootstrap.builder()
.setMode(QuarkusBootstrap.Mode.PROD)
.setBuildSystemProperties(getBuildSystemProperties())
.setApplicationRoot(getWorkingDirectory())
.setProjectRoot(getWorkingDirectory())
.setTargetDirectory(targetDirecotry)
.setLocalProjectDiscovery(true)
.setIsolateDeployment(false)
.setBaseClassLoader(ClassLoader.getSystemClassLoader())
.build();

List<String> resultBuildItemFQCNs = new ArrayList<>();
resultBuildItemFQCNs.add(ArgoCDApplicationListBuildItem.class.getName());

// Checking
try (CuratedApplication curatedApplication = quarkusBootstrap.bootstrap()) {
AugmentAction action = curatedApplication.createAugmentor();

action.performCustomBuild(GetArgoCDApplicationHandler.class.getName(), new Consumer<ApplicationList>() {
@Override
public void accept(ApplicationList applicationList) {
if (applicationList.getItems().isEmpty()) {
System.out.println("No ArgoCD Application generated.");
return;
}
Path outputDir = generationPath.map(Paths::get).orElse(Paths.get(".argocd"));
if (outputDir.toFile().exists() && !outputDir.toFile().isDirectory()) {
System.err.println("Output directory is not a directory: " + outputDir);
return;
}
if (!outputDir.toFile().exists() && !outputDir.toFile().mkdirs()) {
System.err.println("Failed to create output directory: " + outputDir);
return;
}

System.out.println("ArgoCD Applications generated:");
for (Application application : applicationList.getItems()) {
String content = Serialization.asYaml(application);
Path p = outputDir.resolve(application.getMetadata().getName() + ".yaml");
writeStringSafe(p, content);
System.out.println(application.getMetadata().getName());
}
}
}, resultBuildItemFQCNs.toArray(new String[resultBuildItemFQCNs.size()]));

} catch (BootstrapException e) {
throw new RuntimeException(e);
}
return ExitCode.OK;
}

@Override
public void process(ApplicationList applicationList) {
Path outputDir = generationPath.map(Paths::get).orElse(Paths.get(".argocd"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.function.Consumer;

import io.quarkiverse.argocd.cli.handlers.GetArgoCDApplicationHandler;
import io.quarkiverse.argocd.cli.utils.Git;
import io.quarkiverse.argocd.spi.ArgoCDApplicationListBuildItem;
import io.quarkiverse.argocd.v1alpha1.ApplicationList;
import io.quarkus.bootstrap.BootstrapException;
Expand Down Expand Up @@ -40,6 +41,13 @@ public Properties getBuildSystemProperties() {

public Integer call() {
Path projectRoot = getWorkingDirectory();

Optional<Path> scmRoot = Git.getScmRoot();
if (scmRoot.isEmpty()) {
System.out.println("Unable to determine the SCM root for the project at " + projectRoot + ". Aborting.");
return ExitCode.USAGE;
}

BuildTool buildTool = QuarkusProjectHelper.detectExistingBuildTool(projectRoot);
if (buildTool == null) {
System.out.println("Unable to determine the build tool used for the project at " + projectRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,99 +1,39 @@
package io.quarkiverse.argocd.cli.application;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.quarkiverse.argocd.cli.handlers.GetArgoCDApplicationHandler;
import io.quarkiverse.argocd.spi.ArgoCDApplicationListBuildItem;
import io.quarkiverse.argocd.v1alpha1.Application;
import io.quarkiverse.argocd.v1alpha1.ApplicationList;
import io.quarkus.bootstrap.BootstrapException;
import io.quarkus.bootstrap.app.AugmentAction;
import io.quarkus.bootstrap.app.CuratedApplication;
import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import picocli.CommandLine.Command;
import picocli.CommandLine.ExitCode;

@Command(name = "uninstall", sortOptions = false, mixinStandardHelpOptions = false, header = "Uninstall ArgoCD Application.", headerHeading = "%n", commandListHeading = "%nCommands:%n", synopsisHeading = "%nUsage: ", optionListHeading = "%nOptions:%n")
public class UninstallCommand implements Callable<Integer> {
public class UninstallCommand extends GenerationBaseCommand {

Optional<String> generationPath = Optional.of(".argocd");

public Integer call() {
Path projectRoot = getWorkingDirectory();
BuildTool buildTool = QuarkusProjectHelper.detectExistingBuildTool(projectRoot);
if (buildTool == null) {
System.out.println("Unable to determine the build tool used for the project at " + projectRoot);
return ExitCode.USAGE;
@Override
void process(ApplicationList applicationList) {
if (applicationList.getItems().isEmpty()) {
System.out.println("No ArgoCD Application detected.");
return;
}
Path targetDirecotry = projectRoot.resolve(buildTool.getBuildDirectory());
QuarkusBootstrap quarkusBootstrap = QuarkusBootstrap.builder()
.setMode(QuarkusBootstrap.Mode.PROD)
.setApplicationRoot(getWorkingDirectory())
.setProjectRoot(getWorkingDirectory())
.setTargetDirectory(targetDirecotry)
.setLocalProjectDiscovery(true)
.setIsolateDeployment(false)
.setBaseClassLoader(ClassLoader.getSystemClassLoader())
.build();

List<String> resultBuildItemFQCNs = new ArrayList<>();
resultBuildItemFQCNs.add(ArgoCDApplicationListBuildItem.class.getName());
ApplicationListTable table = new ApplicationListTable();
List<ApplicationListItem> items = new ArrayList<>();
KubernetesClient kubernetesClient = new KubernetesClientBuilder().build();

// Checking
try (CuratedApplication curatedApplication = quarkusBootstrap.bootstrap()) {
AugmentAction action = curatedApplication.createAugmentor();

action.performCustomBuild(GetArgoCDApplicationHandler.class.getName(), new Consumer<ApplicationList>() {
@Override
public void accept(ApplicationList applicationList) {
if (applicationList.getItems().isEmpty()) {
System.out.println("No ArgoCD Application detected.");
return;
}

ApplicationListTable table = new ApplicationListTable();
List<ApplicationListItem> items = new ArrayList<>();
KubernetesClient kubernetesClient = new KubernetesClientBuilder().build();

for (Application application : applicationList.getItems()) {
kubernetesClient.resources(Application.class)
.inNamespace(application.getMetadata().getNamespace())
.withName(application.getMetadata().getName())
.delete();
items.add(ApplicationListItem.from(application));
}
System.out.println("Uninstalled ArgoCD applications:");
System.out.println(table.getContent(items));
}
}, resultBuildItemFQCNs.toArray(new String[resultBuildItemFQCNs.size()]));

} catch (BootstrapException e) {
throw new RuntimeException(e);
for (Application application : applicationList.getItems()) {
kubernetesClient.resources(Application.class)
.inNamespace(application.getMetadata().getNamespace())
.withName(application.getMetadata().getName())
.delete();
items.add(ApplicationListItem.from(application));
}
return ExitCode.OK;
}

private void writeStringSafe(Path p, String content) {
try {
Files.writeString(p, content);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private Path getWorkingDirectory() {
return Paths.get(System.getProperty("user.dir"));
System.out.println("Uninstalled ArgoCD applications:");
System.out.println(table.getContent(items));
}
}
2 changes: 1 addition & 1 deletion cli/src/main/java/io/quarkiverse/argocd/cli/utils/Git.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static Optional<String> getRemoteName(String url) {
});
}

private static Optional<Path> getScmRoot() {
public static Optional<Path> getScmRoot() {
Path dir = Paths.get("").toAbsolutePath();
while (dir != null && !dir.resolve(DOT_GIT).toFile().exists()) {
dir = dir.getParent();
Expand Down

0 comments on commit a1a4870

Please sign in to comment.