commit initial version of PieCannon library and desktop app.
Documentation is sparse right now, I'm working on getting the desktop and android apps working first.
This commit is contained in:
3
desktop/piecannon
Executable file
3
desktop/piecannon
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
RESULT=$(mvn -B exec:java -Dexec.mainClass=net.monarchpass.piecannon.App -Dexec.arguments=$1 -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN | tail -n 1)
|
||||
xdg-open $RESULT
|
67
desktop/pom.xml
Normal file
67
desktop/pom.xml
Normal file
@@ -0,0 +1,67 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>net.monarchpass</groupId>
|
||||
<artifactId>piecannon-desktop-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.monarchpass</groupId>
|
||||
<artifactId>libpiecannon</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>30.0-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.kothar</groupId>
|
||||
<artifactId>xdg-java</artifactId>
|
||||
<version>0.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.16</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.7.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.truth</groupId>
|
||||
<artifactId>truth</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
84
desktop/src/main/java/net/monarchpass/piecannon/App.java
Normal file
84
desktop/src/main/java/net/monarchpass/piecannon/App.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package net.monarchpass.piecannon;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.freedesktop.BaseDirectory;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@Log
|
||||
public class App {
|
||||
public static void main (final String... args) throws Exception {
|
||||
final File serversJson = getServersJson();
|
||||
final List<Server> servers = loadServersFrom(serversJson);
|
||||
log.log(Level.INFO, "{0} servers loaded from {1}", new Object[] {
|
||||
servers.size(), serversJson
|
||||
});
|
||||
|
||||
if (servers.isEmpty()) {
|
||||
log.log(Level.SEVERE, "No servers defined, please define at least one in {0}", serversJson);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
final Server server = servers.get(new Random().nextInt(servers.size()));
|
||||
log.log(Level.INFO, "Randomly selected server: {0}", server.getLabel());
|
||||
|
||||
if (args.length == 0) {
|
||||
log.log(Level.SEVERE, "No filename provided");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
final File source = new File(args[0]);
|
||||
if (!source.exists()) {
|
||||
log.log(Level.SEVERE, "No such file: {0}", source);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
final URI result = server.upload(source);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
public static List<Server> loadServersFrom (final File serversJson) throws IOException {
|
||||
if (!serversJson.exists()) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
final ServerFactory factory = new ServerFactory();
|
||||
try (final InputStream in = new FileInputStream(serversJson)) {
|
||||
final JsonParser parser = new JsonParser();
|
||||
return Streams.stream(parser.parse(new InputStreamReader(in)).getAsJsonArray())
|
||||
.filter(JsonElement::isJsonObject)
|
||||
.map(JsonObject.class::cast)
|
||||
.map(factory)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
public static File getServersJson () {
|
||||
return new File(getDataDirectory(), "servers.json");
|
||||
}
|
||||
|
||||
public static File getDataDirectory () {
|
||||
return new File(BaseDirectory.get(BaseDirectory.XDG_DATA_HOME), "piecannon");
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package net.monarchpass.piecannon;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import net.monarchpass.piecannon.impl.SftpServer;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
|
||||
public class ServerFactory implements Function<JsonObject, Server> {
|
||||
public Server apply (final JsonObject object) {
|
||||
return makeSftpServer(object);
|
||||
}
|
||||
|
||||
private Server makeSftpServer (final JsonObject object) {
|
||||
final String host = object.getAsJsonPrimitive("host").getAsString();
|
||||
final String label = object.has("label") ? object.getAsJsonPrimitive("label").getAsString() : host;
|
||||
final int port = object.has("port") ? object.getAsJsonPrimitive("port").getAsInt() : 22;
|
||||
final String username = object.getAsJsonPrimitive("username").getAsString();
|
||||
final String password = object.getAsJsonPrimitive("password").getAsString();
|
||||
final String path = object.getAsJsonPrimitive("path").getAsString();
|
||||
final String url = object.getAsJsonPrimitive("url").getAsString();
|
||||
|
||||
return new SftpServer(
|
||||
label, host, port, username, password,
|
||||
path, URI.create(url)
|
||||
);
|
||||
}
|
||||
}
|
10
desktop/src/test/java/net/monarchpass/piecannon/AppTest.java
Normal file
10
desktop/src/test/java/net/monarchpass/piecannon/AppTest.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package net.monarchpass.piecannon;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test () throws Exception {}
|
||||
}
|
Reference in New Issue
Block a user