app: cleanup PasswordRepository
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
8c6297d56f
commit
50b481f304
1 changed files with 5 additions and 26 deletions
|
@ -17,7 +17,6 @@ import dev.msfjarvis.aps.util.extensions.sharedPrefs
|
||||||
import dev.msfjarvis.aps.util.settings.PasswordSortOrder
|
import dev.msfjarvis.aps.util.settings.PasswordSortOrder
|
||||||
import dev.msfjarvis.aps.util.settings.PreferenceKeys
|
import dev.msfjarvis.aps.util.settings.PreferenceKeys
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileFilter
|
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
import java.nio.file.LinkOption
|
import java.nio.file.LinkOption
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
|
@ -66,7 +65,6 @@ object PasswordRepository {
|
||||||
* @param localDir needed only on the creation
|
* @param localDir needed only on the creation
|
||||||
* @return the git repository
|
* @return the git repository
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
|
||||||
fun getRepository(localDir: File?): Repository? {
|
fun getRepository(localDir: File?): Repository? {
|
||||||
if (repository == null && localDir != null) {
|
if (repository == null && localDir != null) {
|
||||||
val builder = FileRepositoryBuilder()
|
val builder = FileRepositoryBuilder()
|
||||||
|
@ -90,20 +88,13 @@ object PasswordRepository {
|
||||||
return repository
|
return repository
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
val isInitialized: Boolean
|
val isInitialized: Boolean
|
||||||
get() = repository != null
|
get() = repository != null
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun isGitRepo(): Boolean {
|
fun isGitRepo(): Boolean {
|
||||||
if (repository != null) {
|
return repository?.objectDatabase?.exists() ?: false
|
||||||
return repository!!.objectDatabase.exists()
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
@Throws(Exception::class)
|
|
||||||
fun createRepository(localDir: File) {
|
fun createRepository(localDir: File) {
|
||||||
localDir.delete()
|
localDir.delete()
|
||||||
|
|
||||||
|
@ -112,7 +103,6 @@ object PasswordRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add multiple remotes support for pull/push
|
// TODO add multiple remotes support for pull/push
|
||||||
@JvmStatic
|
|
||||||
fun addRemote(name: String, url: String, replace: Boolean = false) {
|
fun addRemote(name: String, url: String, replace: Boolean = false) {
|
||||||
val storedConfig = repository!!.config
|
val storedConfig = repository!!.config
|
||||||
val remotes = storedConfig.getSubsections("remote")
|
val remotes = storedConfig.getSubsections("remote")
|
||||||
|
@ -157,13 +147,11 @@ object PasswordRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun closeRepository() {
|
fun closeRepository() {
|
||||||
if (repository != null) repository!!.close()
|
repository?.close()
|
||||||
repository = null
|
repository = null
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun getRepositoryDirectory(): File {
|
fun getRepositoryDirectory(): File {
|
||||||
return if (settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false)) {
|
return if (settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false)) {
|
||||||
val externalRepo = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO)
|
val externalRepo = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO)
|
||||||
|
@ -173,7 +161,6 @@ object PasswordRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun initialize(): Repository? {
|
fun initialize(): Repository? {
|
||||||
val dir = getRepositoryDirectory()
|
val dir = getRepositoryDirectory()
|
||||||
// uninitialize the repo if the dir does not exist or is absolutely empty
|
// uninitialize the repo if the dir does not exist or is absolutely empty
|
||||||
|
@ -195,20 +182,13 @@ object PasswordRepository {
|
||||||
* @param path the directory path
|
* @param path the directory path
|
||||||
* @return the list of gpg files in that directory
|
* @return the list of gpg files in that directory
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
private fun getFilesList(path: File): ArrayList<File> {
|
||||||
fun getFilesList(path: File?): ArrayList<File> {
|
if (!path.exists()) return ArrayList()
|
||||||
if (path == null || !path.exists()) return ArrayList()
|
|
||||||
|
|
||||||
val directories =
|
|
||||||
(path.listFiles(FileFilter { pathname -> pathname.isDirectory }) ?: emptyArray()).toList()
|
|
||||||
val files =
|
val files =
|
||||||
(path.listFiles(FileFilter { pathname -> pathname.extension == "gpg" }) ?: emptyArray())
|
(path.listFiles { file -> file.isDirectory || file.extension == "gpg" } ?: emptyArray())
|
||||||
.toList()
|
.toList()
|
||||||
|
|
||||||
val items = ArrayList<File>()
|
val items = ArrayList<File>()
|
||||||
items.addAll(directories)
|
|
||||||
items.addAll(files)
|
items.addAll(files)
|
||||||
|
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,7 +198,6 @@ object PasswordRepository {
|
||||||
* @param path the directory path
|
* @param path the directory path
|
||||||
* @return a list of password items
|
* @return a list of password items
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
|
||||||
fun getPasswords(
|
fun getPasswords(
|
||||||
path: File,
|
path: File,
|
||||||
rootDir: File,
|
rootDir: File,
|
||||||
|
|
Loading…
Reference in a new issue