From f149d6b1281f683e4f09d84d1dfcd2cde307afe3 Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Fri, 22 Sep 2023 16:33:14 -0500 Subject: [PATCH] Fixes #212 - Add support for Bash shell scripts (#213) --- .../com/manorrock/sphynx/cli/BashCLI.java | 93 +++++++++++++++++++ .../java/com/manorrock/sphynx/cli/Cli.java | 3 +- cli/src/test/scripts/Echo.bash | 3 + 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 cli/src/main/java/com/manorrock/sphynx/cli/BashCLI.java create mode 100644 cli/src/test/scripts/Echo.bash diff --git a/cli/src/main/java/com/manorrock/sphynx/cli/BashCLI.java b/cli/src/main/java/com/manorrock/sphynx/cli/BashCLI.java new file mode 100644 index 0000000..88c0802 --- /dev/null +++ b/cli/src/main/java/com/manorrock/sphynx/cli/BashCLI.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.manorrock.sphynx.cli; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * The Bash CLI. + * + * @author Manfred Riem (mriem@manorrock.com) + */ +class BashCli { + + /** + * Stores the script file. + */ + private String bashFile; + + /** + * Stores the run arguments. + */ + private String runArguments; + + /** + * Execute. + * + * @param arguments the arguments. + */ + void execute(String[] arguments) { + parseArguments(arguments); + run(); + } + + /** + * Parse the arguments. + * + * @param arguments the arguments. + */ + private void parseArguments(String[] arguments) { + for(int i=1; i command = new ArrayList<>(); + command.add("bash"); + command.add(runArguments); + Process process = new ProcessBuilder() + .command(command) + .inheritIO() + .start(); + System.exit(process.waitFor()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/cli/src/main/java/com/manorrock/sphynx/cli/Cli.java b/cli/src/main/java/com/manorrock/sphynx/cli/Cli.java index 05607c6..219643e 100644 --- a/cli/src/main/java/com/manorrock/sphynx/cli/Cli.java +++ b/cli/src/main/java/com/manorrock/sphynx/cli/Cli.java @@ -60,7 +60,7 @@ private void addWorkflow(String[] arguments) { System.exit(100); } if (filename == null) { - System.out.println("MIssing --filename parameter"); + System.out.println("Missing --filename parameter"); } File baseDir = new File(System.getProperty("user.home"), ".manorrock/sphynx"); if (!baseDir.exists()) { @@ -93,6 +93,7 @@ private void execute(String[] arguments) { if (arguments.length > 0) { switch (arguments[0]) { case "add" -> addWorkflow(arguments); + case "bash" -> new BashCli().execute(arguments); case "execute" -> executeWorkflow(arguments); case "java" -> new JavaCli().execute(arguments); case "list" -> listWorkflows(); diff --git a/cli/src/test/scripts/Echo.bash b/cli/src/test/scripts/Echo.bash new file mode 100644 index 0000000..fbb0a54 --- /dev/null +++ b/cli/src/test/scripts/Echo.bash @@ -0,0 +1,3 @@ +#!/bin/bash + +echo $*