aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
blob: 6acd4f2b5acec10e16363e78cb62c4730f67fb33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.pacien.tincapp.commands

import java8.util.concurrent.CompletableFuture
import org.pacien.tincapp.context.AppPaths

/**
 * @author pacien
 */
object Tinc {

    private fun newCommand(netName: String): Command =
            Command(AppPaths.tinc().absolutePath)
                    .withOption("config", AppPaths.confDir(netName).absolutePath)
                    .withOption("pidfile", AppPaths.pidFile(netName).absolutePath)

    fun stop(netName: String): CompletableFuture<Unit> =
            Executor.call(newCommand(netName).withArguments("stop"))
                    .thenApply { }

    fun dumpNodes(netName: String, reachable: Boolean = false): CompletableFuture<List<String>> =
            Executor.call(
                    if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes")
                    else newCommand(netName).withArguments("dump", "nodes"))

    fun info(netName: String, node: String): CompletableFuture<String> =
            Executor.call(newCommand(netName).withArguments("info", node))
                    .thenApply<String> { it.joinToString("\n") }

    fun init(netName: String, nodeName: String): CompletableFuture<String> =
            if (netName.isBlank())
                CompletableFuture.failedFuture(IllegalArgumentException("Network name cannot be blank."))
            else
                Executor.call(Command(AppPaths.tinc().absolutePath)
                        .withOption("config", AppPaths.confDir(netName).absolutePath)
                        .withArguments("init", nodeName))
                        .thenApply<String> { it.joinToString("\n") }

    fun join(netName: String, invitationUrl: String): CompletableFuture<String> =
            if (netName.isBlank())
                CompletableFuture.failedFuture(IllegalArgumentException("Network name cannot be blank."))
            else
                Executor.call(Command(AppPaths.tinc().absolutePath)
                        .withOption("config", AppPaths.confDir(netName).absolutePath)
                        .withArguments("join", invitationUrl))
                        .thenApply<String> { it.joinToString("\n") }

}