-
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.
feat: add support for more platforms
- Loading branch information
1 parent
1ad2d53
commit 7ae197c
Showing
18 changed files
with
207 additions
and
36 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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/shiyouping/redis/embedded/Platform.java
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,24 @@ | ||
package com.github.shiyouping.redis.embedded; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* Platform. | ||
* | ||
* @author [email protected] | ||
* @since 08/11/2022 | ||
*/ | ||
@Data | ||
public class Platform { | ||
|
||
private OS os; | ||
private ARCH arch; | ||
|
||
public enum OS { | ||
MACOS, DEBIAN, REDHAT, UBUNTU | ||
} | ||
|
||
public enum ARCH { | ||
X86_64, ARM64 | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/shiyouping/redis/embedded/util/CommandLine.java
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,33 @@ | ||
package com.github.shiyouping.redis.embedded.util; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
import static com.github.shiyouping.redis.embedded.util.Preconditions.checkNotBlank; | ||
|
||
/** | ||
* CommandLine. | ||
* | ||
* @author [email protected] | ||
* @since 08/11/2022 | ||
*/ | ||
@Slf4j | ||
public final class CommandLine { | ||
|
||
|
||
public static String getOutput(final String command) { | ||
checkNotBlank(command, "command cannot be null"); | ||
|
||
try { | ||
final Process process = Runtime.getRuntime().exec(command); | ||
final String output = IOUtils.toString(process.getInputStream(), Charset.defaultCharset()); | ||
CommandLine.log.info("The output of command={} is {}", command, output); | ||
return output.trim(); | ||
} catch (final Exception e) { | ||
CommandLine.log.error("Failed to getOutput the command " + command, e); | ||
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
82 changes: 82 additions & 0 deletions
82
src/main/java/com/github/shiyouping/redis/embedded/util/PlatformUtil.java
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,82 @@ | ||
package com.github.shiyouping.redis.embedded.util; | ||
|
||
import com.github.shiyouping.redis.embedded.Platform; | ||
import com.github.shiyouping.redis.embedded.exception.EmbeddedRedisException; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import static com.github.shiyouping.redis.embedded.Platform.ARCH; | ||
import static com.github.shiyouping.redis.embedded.Platform.OS; | ||
|
||
/** | ||
* PlatformUtil. | ||
* | ||
* @author [email protected] | ||
* @since 08/11/2022 | ||
*/ | ||
@Slf4j | ||
public final class PlatformUtil { | ||
|
||
private PlatformUtil() { | ||
} | ||
|
||
|
||
public static Platform getPlatform() { | ||
final String os = System.getProperty("os.name").toLowerCase(); | ||
PlatformUtil.log.info("The operation system is {}", os); | ||
|
||
if (!os.contains("mac") && !os.contains("linux")) { | ||
throw new EmbeddedRedisException("Unsupported OS: " + os); | ||
} | ||
|
||
final Platform platform = new Platform(); | ||
platform.setArch(PlatformUtil.getArch()); | ||
|
||
if (os.contains("mac")) { | ||
platform.setOs(OS.MACOS); | ||
return platform; | ||
} | ||
|
||
platform.setOs(PlatformUtil.getLinuxOs()); | ||
return platform; | ||
} | ||
|
||
private static ARCH getArch() { | ||
String arch = CommandLine.getOutput("arch"); | ||
PlatformUtil.log.info("The architecture is {}", arch); | ||
|
||
if (arch == null) { | ||
throw new EmbeddedRedisException("Unable to get macos architecture"); | ||
} | ||
|
||
arch = arch.toLowerCase(); | ||
|
||
if (arch.contains("arm") || arch.contains("aarch")) { | ||
return ARCH.ARM64; | ||
} | ||
|
||
return ARCH.X86_64; | ||
} | ||
|
||
private static OS getLinuxOs() { | ||
String os = CommandLine.getOutput("grep '^NAME' /etc/os-release"); | ||
if (os == null) { | ||
throw new EmbeddedRedisException("Unable to get linux os"); | ||
} | ||
|
||
os = os.toLowerCase(); | ||
|
||
if (os.contains("ubuntu")) { | ||
return OS.UBUNTU; | ||
} | ||
|
||
if (os.contains("debian")) { | ||
return OS.DEBIAN; | ||
} | ||
|
||
if (os.contains("red hat")) { | ||
return OS.REDHAT; | ||
} | ||
|
||
throw new EmbeddedRedisException("Unsupported linux os " + os); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
redis.version=6.2.7 | ||
redis.version=6.2.4 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions
13
src/test/java/com/github/shiyouping/redis/embedded/util/CommandLineTest.java
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,13 @@ | ||
package com.github.shiyouping.redis.embedded.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CommandLineTest { | ||
|
||
@Test | ||
public void shouldGetOutput() { | ||
assertThat(CommandLine.getOutput("date")).isNotBlank(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/com/github/shiyouping/redis/embedded/util/PlatformUtilTest.java
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,17 @@ | ||
package com.github.shiyouping.redis.embedded.util; | ||
|
||
import com.github.shiyouping.redis.embedded.Platform; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class PlatformUtilTest { | ||
|
||
@Test | ||
public void shouldGetPlatform() { | ||
final Platform platform = PlatformUtil.getPlatform(); | ||
assertThat(platform).isNotNull(); | ||
assertThat(platform.getArch()).isNotNull(); | ||
assertThat(platform.getOs()).isNotNull(); | ||
} | ||
} |