-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
266 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.