space-export/src/main/kotlin/main.kt

90 lines
2.9 KiB
Kotlin
Raw Normal View History

2021-12-26 15:28:53 +03:00
package ru.mipt.npm.space.documentextractor
import io.ktor.client.engine.cio.CIO
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
2023-06-16 20:16:22 +03:00
import kotlinx.cli.default
2021-12-26 15:28:53 +03:00
import kotlinx.cli.required
2022-04-26 21:20:46 +03:00
import kotlinx.coroutines.coroutineScope
import space.jetbrains.api.runtime.SpaceAppInstance
import space.jetbrains.api.runtime.SpaceAuth
import space.jetbrains.api.runtime.SpaceClient
import space.jetbrains.api.runtime.ktorClientForSpace
import space.jetbrains.api.runtime.resources.projects
import space.jetbrains.api.runtime.types.FolderIdentifier
import space.jetbrains.api.runtime.types.ProjectIdentifier
import java.nio.file.Files
2021-12-26 15:28:53 +03:00
import java.nio.file.Path
2023-06-16 11:23:04 +03:00
import kotlin.io.path.Path
2021-12-26 15:28:53 +03:00
suspend fun main(args: Array<String>) {
val parser = ArgParser("space-document-extractor")
val spaceUrl by parser.option(
ArgType.String,
description = "Url of the space instance like 'https://mipt-npm.jetbrains.space'"
).required()
2022-04-26 21:20:46 +03:00
val project by parser.option(
ArgType.String,
description = "The key of the exported project"
).required()
2023-06-16 20:16:22 +03:00
val path: String? by parser.option(
ArgType.String,
description = "Target directory. Default is './output/project-key'."
)
2022-04-26 21:20:46 +03:00
val folderId: String? by parser.option(
ArgType.String,
2022-04-27 09:26:24 +03:00
description = "FolderId for the folder to export. By default uses project root."
2022-04-26 21:20:46 +03:00
)
2023-06-16 20:16:22 +03:00
val generateHtml by parser.option(
ArgType.Boolean,
description = "If defined, generate HTML directory and zip"
).default(false)
val htmlOutputPath by parser.option(
ArgType.String,
description = "Path for html output directory sibling to 'output' directory"
).default("html")
2021-12-26 15:28:53 +03:00
val clientId by parser.option(
ArgType.String,
description = "Space application client ID (if not defined, use environment value 'space.clientId')"
)
val clientSecret by parser.option(
ArgType.String,
description = "Space application client secret (if not defined, use environment value 'space.clientSecret')"
)
parser.parse(args)
2023-06-16 20:16:22 +03:00
val target: Path = path?.let { Path(it) } ?: folderId?.let { Path("output") } ?: Path("output/$project")
2021-12-26 15:28:53 +03:00
2022-04-26 21:20:46 +03:00
Files.createDirectories(target)
2021-12-26 15:28:53 +03:00
2022-04-26 21:20:46 +03:00
val space: SpaceClient = SpaceClient(
ktorClientForSpace(CIO),
SpaceAppInstance(
clientId ?: System.getProperty("space.clientId"),
clientSecret ?: System.getProperty("space.clientSecret"),
spaceUrl
),
SpaceAuth.ClientCredentials()
2021-12-26 15:28:53 +03:00
)
2022-04-26 21:20:46 +03:00
coroutineScope {
println("Processing project \"${space.projects.getProject(ProjectIdentifier.Key(project)).name}\"")
space.downloadAndProcessDocumentsInProject(
target,
ProjectIdentifier.Key(project),
folderId?.let { FolderIdentifier.Id(it) } ?: FolderIdentifier.Root
)
2023-06-16 20:16:22 +03:00
if (generateHtml) {
generateHtml(target, target.resolveSibling(htmlOutputPath))
}
2021-12-26 15:28:53 +03:00
}
}