Skip to content

Commit

Permalink
WIP #85 powershell support
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoss committed Sep 22, 2020
1 parent a4c4992 commit 122b072
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 103 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
--add-opens wtf.metio.ilo/wtf.metio.ilo.devcontainer=ALL-UNNAMED
--add-opens wtf.metio.ilo/wtf.metio.ilo.errors=ALL-UNNAMED
--add-opens wtf.metio.ilo/wtf.metio.ilo.model=ALL-UNNAMED
--add-opens wtf.metio.ilo/wtf.metio.ilo.os=ALL-UNNAMED
--add-opens wtf.metio.ilo/wtf.metio.ilo.shell=ALL-UNNAMED
--add-opens wtf.metio.ilo/wtf.metio.ilo.test=ALL-UNNAMED
--add-opens wtf.metio.ilo/wtf.metio.ilo.tools=ALL-UNNAMED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
* in the LICENSE file.
*/

package wtf.metio.ilo.utils;
package wtf.metio.ilo.os;

import wtf.metio.ilo.cli.Executables;
import wtf.metio.ilo.errors.RuntimeIOException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
Expand All @@ -22,11 +19,14 @@
import static wtf.metio.ilo.utils.Streams.filter;
import static wtf.metio.ilo.utils.Streams.fromList;

public final class Bash {
/**
* Support for GNU Bash
*/
final class Bash implements ParameterExpansion {

private static final Pattern BASH_NEW_STYLE = Pattern.compile("\\$\\((?<expression>.+)\\)");
private static final Pattern BASH_OLD_STYLE = Pattern.compile("`(?<expression>.+)`");
public static final String EXPRESSION = "expression";
private static final Pattern BASH_COMMAND_NEW_STYLE = Pattern.compile("\\$\\((?<expression>.+)\\)");
private static final Pattern BASH_COMMAND_OLD_STYLE = Pattern.compile("`(?<expression>.+)`");
private static final String EXPRESSION = "expression";

public static List<String> expand(final List<String> values) {
return filter(fromList(values))
Expand Down Expand Up @@ -55,10 +55,18 @@ public static String bashExpansion(final String value) {
.orElse(value);
}

@Override
public String substituteCommands(final String value) {
return Executables.of("bash")
.map(Path::toAbsolutePath)
.map(bash -> substituteCommands(bash, value))
.orElse(value);
}

// https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
private static String substituteCommands(final Path bash, final String value) {
final var newStyle = BASH_NEW_STYLE.matcher(value);
final var oldStyle = BASH_OLD_STYLE.matcher(value);
final var newStyle = BASH_COMMAND_NEW_STYLE.matcher(value);
final var oldStyle = BASH_COMMAND_OLD_STYLE.matcher(value);
var current = value;
while (newStyle.find()) {
current = substitute(bash, newStyle, current);
Expand All @@ -77,21 +85,10 @@ private static String substitute(final Path bash, final Matcher matcher, final S
return new StringBuilder(current).replace(start, end, replacement).toString();
}

public static Path passwdFile(final String runAs) {
try {
final var username = System.getProperty("user.name");
final var tempFile = Files.createTempFile("ilo", ".passwd");
tempFile.toFile().deleteOnExit();
final var content = String.format("%s:x:%s::/home/%s:/bin/bash", username, expand(runAs), username);
Files.writeString(tempFile, content);
return tempFile.toAbsolutePath();
} catch (final IOException exception) {
throw new RuntimeIOException(exception);
}
}

private Bash() {
// utility class
@Override
// https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
public String expandParameters(final String value) {
return null;
}

}
27 changes: 27 additions & 0 deletions src/main/java/wtf/metio/ilo/os/Cmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of ilo. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of ilo,
* including this file, may be copied, modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/

package wtf.metio.ilo.os;

/**
* Support for Windows CMD
*/
final class Cmd implements ParameterExpansion {

@Override
public String substituteCommands(final String value) {
// TODO: cmd impl
return null;
}

@Override
public String expandParameters(final String value) {
// TODO: cmd impl
return null;
}

}
22 changes: 22 additions & 0 deletions src/main/java/wtf/metio/ilo/os/NoOpExpansion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This file is part of ilo. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of ilo,
* including this file, may be copied, modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/

package wtf.metio.ilo.os;

public class NoOpExpansion implements ParameterExpansion {

@Override
public String substituteCommands(final String value) {
return value;
}

@Override
public String expandParameters(final String value) {
return value;
}

}
74 changes: 74 additions & 0 deletions src/main/java/wtf/metio/ilo/os/OS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of ilo. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of ilo,
* including this file, may be copied, modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/

package wtf.metio.ilo.os;

import wtf.metio.ilo.errors.RuntimeIOException;
import wtf.metio.ilo.utils.Strings;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import static java.util.stream.Collectors.toList;
import static wtf.metio.ilo.utils.Streams.filter;
import static wtf.metio.ilo.utils.Streams.fromList;

public final class OS {

public static List<String> expand(final List<String> values) {
return filter(fromList(values))
.map(OS::expand)
.collect(toList());
}

public static String expand(final String value) {
final var expansion = expansion();
return Optional.ofNullable(value)
.map(expansion::expandParameters)
.map(expansion::substituteCommands)
.orElse(value);
}

static ParameterExpansion expansion() {
return detectExpansionForOS(System.getProperty("os.name"));
}

private static ParameterExpansion detectExpansionForOS(final String osName) {
if (Strings.isNotBlank(osName)) {
final var name = osName.toLowerCase(Locale.ENGLISH);
if (name.contains("linux") || name.contains("mac")) {
return new Bash();
}
if (name.contains("win")) {
return new PowerShell();
}
}
return new NoOpExpansion();
}

public static Path passwdFile(final String runAs) {
try {
final var username = System.getProperty("user.name");
final var tempFile = Files.createTempFile("ilo", ".passwd");
tempFile.toFile().deleteOnExit();
final var content = String.format("%s:x:%s::/home/%s:/bin/bash", username, expand(runAs), username);
Files.writeString(tempFile, content);
return tempFile.toAbsolutePath();
} catch (final IOException exception) {
throw new RuntimeIOException(exception);
}
}

private OS() {
// utility class
}

}
16 changes: 16 additions & 0 deletions src/main/java/wtf/metio/ilo/os/ParameterExpansion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This file is part of ilo. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of ilo,
* including this file, may be copied, modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/

package wtf.metio.ilo.os;

interface ParameterExpansion {

String substituteCommands(String value);

String expandParameters(String value);

}
27 changes: 27 additions & 0 deletions src/main/java/wtf/metio/ilo/os/PowerShell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of ilo. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of ilo,
* including this file, may be copied, modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/

package wtf.metio.ilo.os;

/**
* Support for Windows PowerShell
*/
final class PowerShell implements ParameterExpansion {

@Override
public String substituteCommands(final String value) {
// TODO: powershell impl
return null;
}

@Override
public String expandParameters(final String value) {
// TODO: powershell impl
return null;
}

}
30 changes: 15 additions & 15 deletions src/main/java/wtf/metio/ilo/tools/DockerComposePodmanCompose.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import wtf.metio.ilo.compose.ComposeCLI;
import wtf.metio.ilo.compose.ComposeOptions;
import wtf.metio.ilo.utils.Bash;
import wtf.metio.ilo.os.OS;

import java.util.List;

Expand All @@ -23,10 +23,10 @@ public final List<String> pullArguments(final ComposeOptions options) {
if (options.pull) {
return flatten(
of(name()),
fromList(Bash.expand(options.runtimeOptions)),
withPrefix("--file", Bash.expand(options.file)),
fromList(OS.expand(options.runtimeOptions)),
withPrefix("--file", OS.expand(options.file)),
of("pull"),
fromList(Bash.expand(options.runtimePullOptions)));
fromList(OS.expand(options.runtimePullOptions)));
}
return List.of();
}
Expand All @@ -36,10 +36,10 @@ public final List<String> buildArguments(final ComposeOptions options) {
if (options.build) {
return flatten(
of(name()),
fromList(Bash.expand(options.runtimeOptions)),
withPrefix("--file", Bash.expand(options.file)),
fromList(OS.expand(options.runtimeOptions)),
withPrefix("--file", OS.expand(options.file)),
of("build"),
fromList(Bash.expand(options.runtimeBuildOptions)));
fromList(OS.expand(options.runtimeBuildOptions)));
}
return List.of();
}
Expand All @@ -48,13 +48,13 @@ public final List<String> buildArguments(final ComposeOptions options) {
public final List<String> runArguments(final ComposeOptions options) {
return flatten(
of(name()),
fromList(Bash.expand(options.runtimeOptions)),
withPrefix("--file", Bash.expand(options.file)),
fromList(OS.expand(options.runtimeOptions)),
withPrefix("--file", OS.expand(options.file)),
of("run"),
fromList(Bash.expand(options.runtimeRunOptions)),
fromList(OS.expand(options.runtimeRunOptions)),
maybe(!options.interactive, "-T"),
of(Bash.expand(options.service)),
fromList(Bash.expand(options.arguments)));
of(OS.expand(options.service)),
fromList(OS.expand(options.arguments)));
}

@Override
Expand All @@ -63,10 +63,10 @@ public final List<String> cleanupArguments(final ComposeOptions options) {
// see https://github.com/docker/compose/issues/2791
return flatten(
of(name()),
fromList(Bash.expand(options.runtimeOptions)),
withPrefix("--file", Bash.expand(options.file)),
fromList(OS.expand(options.runtimeOptions)),
withPrefix("--file", OS.expand(options.file)),
of("down"),
fromList(Bash.expand(options.runtimeCleanupOptions)));
fromList(OS.expand(options.runtimeCleanupOptions)));
}

}
Loading

0 comments on commit 122b072

Please sign in to comment.