This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.18.x by this push:
     new d612c6132d8 CAMEL-18388: camel-jbang - add directory option to init to 
save to this folder
d612c6132d8 is described below

commit d612c6132d826147e684f7e653df76d00693b9f9
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Aug 13 10:09:25 2022 +0200

    CAMEL-18388: camel-jbang - add directory option to init to save to this 
folder
---
 .../dsl/jbang/core/commands/CommandHelper.java     | 41 +++++++++++++++++++++
 .../dsl/jbang/core/commands/ExportQuarkus.java     | 12 +-----
 .../apache/camel/dsl/jbang/core/commands/Init.java | 43 ++++++++++++++++++----
 3 files changed, 78 insertions(+), 18 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CommandHelper.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CommandHelper.java
new file mode 100644
index 00000000000..1ce40f1a4f8
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CommandHelper.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.io.File;
+
+import org.apache.camel.util.FileUtil;
+
+public final class CommandHelper {
+
+    private CommandHelper() {
+    }
+
+    public static void cleanExportDir(String dir) {
+        File target = new File(dir);
+        File[] files = target.listFiles();
+        if (files != null) {
+            for (File f : files) {
+                if (!f.isHidden() && f.isDirectory()) {
+                    FileUtil.removeDir(f);
+                } else if (!f.isHidden() && f.isFile()) {
+                    FileUtil.deleteFile(f);
+                }
+            }
+        }
+    }
+}
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
index 98dd6eaed79..eaffe07dcb7 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
@@ -113,16 +113,8 @@ class ExportQuarkus extends Export {
             copyMavenWrapper();
         }
 
-        if (exportDir.equals(".")) {
-            // we export to current dir so prepare for this by cleaning up 
existing files
-            File target = new File(exportDir);
-            for (File f : target.listFiles()) {
-                if (!f.isHidden() && f.isDirectory()) {
-                    FileUtil.removeDir(f);
-                } else if (!f.isHidden() && f.isFile()) {
-                    f.delete();
-                }
-            }
+        if (!exportDir.equals(".")) {
+            CommandHelper.cleanExportDir(exportDir);
         }
         // copy to export dir and remove work dir
         FileUtils.copyDirectory(new File(BUILD_DIR), new File(exportDir));
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
index c002c9db9c4..17d4313445a 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.dsl.jbang.core.commands;
 
+import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.util.StringJoiner;
@@ -40,13 +41,18 @@ import static 
org.apache.camel.dsl.jbang.core.common.GitHubHelper.fetchGithubUrl
 @Command(name = "init", description = "Initialize empty Camel integration")
 class Init extends CamelCommand {
 
-    @CommandLine.Parameters(description = "Name of integration file", arity = 
"1")
+    @CommandLine.Parameters(description = "Name of integration file (or a 
github link)", arity = "1")
     private String file;
 
     @Option(names = { "--integration" },
             description = "When creating a yaml file should it be created as a 
Camel K Integration CRD")
     private boolean integration;
 
+    @CommandLine.Option(names = {
+            "-dir",
+            "--directory" }, description = "Directory where the project will 
be saved", defaultValue = ".")
+    private String directory;
+
     public Init(CamelJBangMain main) {
         super(main);
     }
@@ -76,8 +82,15 @@ class Init extends CamelCommand {
         String context = IOHelper.loadText(is);
         IOHelper.close(is);
 
+        if (!directory.equals(".")) {
+            File dir = new File(directory);
+            CommandHelper.cleanExportDir(directory);
+            // ensure target dir is created after clean
+            dir.mkdirs();
+        }
+        File target = new File(directory, file);
         context = context.replaceFirst("\\{\\{ \\.Name }}", name);
-        IOHelper.writeText(context, new FileOutputStream(file, false));
+        IOHelper.writeText(context, new FileOutputStream(target, false));
         return 0;
     }
 
@@ -95,6 +108,14 @@ class Init extends CamelCommand {
         }
 
         if (all.length() > 0) {
+            // okay we downloaded something so prepare export dir
+            if (!directory.equals(".")) {
+                File dir = new File(directory);
+                CommandHelper.cleanExportDir(directory);
+                // ensure target dir is created after clean
+                dir.mkdirs();
+            }
+
             CamelContext tiny = new LightweightCamelContext();
             GitHubResourceResolver resolver = new GitHubResourceResolver();
             resolver.setCamelContext(tiny);
@@ -103,11 +124,10 @@ class Init extends CamelCommand {
                 if (!resource.exists()) {
                     throw new ResourceDoesNotExist(resource);
                 }
-
                 String loc = resource.getLocation();
                 String name = FileUtil.stripPath(loc);
-
-                try (FileOutputStream fo = new FileOutputStream(name)) {
+                File target = new File(directory, name);
+                try (FileOutputStream fo = new FileOutputStream(target)) {
                     IOUtils.copy(resource.getInputStream(), fo);
                 }
             }
@@ -122,6 +142,14 @@ class Init extends CamelCommand {
         fetchGistUrls(file, all);
 
         if (all.length() > 0) {
+            // okay we downloaded something so prepare export dir
+            if (!directory.equals(".")) {
+                File dir = new File(directory);
+                CommandHelper.cleanExportDir(directory);
+                // ensure target dir is created after clean
+                dir.mkdirs();
+            }
+
             CamelContext tiny = new LightweightCamelContext();
             GistResourceResolver resolver = new GistResourceResolver();
             resolver.setCamelContext(tiny);
@@ -130,11 +158,10 @@ class Init extends CamelCommand {
                 if (!resource.exists()) {
                     throw new ResourceDoesNotExist(resource);
                 }
-
                 String loc = resource.getLocation();
                 String name = FileUtil.stripPath(loc);
-
-                try (FileOutputStream fo = new FileOutputStream(name)) {
+                File target = new File(directory, name);
+                try (FileOutputStream fo = new FileOutputStream(target)) {
                     IOUtils.copy(resource.getInputStream(), fo);
                 }
             }

Reply via email to