Add specific error messages to GitServerConfigActivity

This commit is contained in:
Fabian Henneke 2020-05-14 11:07:01 +02:00 committed by Fabian Henneke
parent f806438f2c
commit de4ce44531
3 changed files with 44 additions and 27 deletions

View file

@ -17,6 +17,7 @@ import androidx.preference.PreferenceManager
import com.github.ajalt.timberkt.Timber.tag import com.github.ajalt.timberkt.Timber.tag
import com.github.ajalt.timberkt.e import com.github.ajalt.timberkt.e
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.git.config.ConnectionMode
import com.zeapo.pwdstore.git.config.Protocol import com.zeapo.pwdstore.git.config.Protocol
import com.zeapo.pwdstore.git.config.SshApiSessionFactory import com.zeapo.pwdstore.git.config.SshApiSessionFactory
@ -83,14 +84,25 @@ abstract class BaseGitActivity : AppCompatActivity() {
super.onDestroy() super.onDestroy()
} }
enum class GitUpdateUrlResult(val textRes: Int) {
Ok(0),
CustomPortRequiresAbsoluteUrlError(R.string.git_config_error_custom_port_absolute),
EmptyHostnameError(R.string.git_config_error_hostname_empty),
GenericError(R.string.git_config_error_generic),
NonNumericPortError(R.string.git_config_error_nonnumeric_port)
}
/** /**
* Update the [url] field with the values that build it up. This function returns a boolean * Update the [url] field with the values that build it up. This function returns a
* indicating whether or not the values are likely valid or not, and only adds the `origin` * [GitUpdateUrlResult] indicating whether the values could be used to build a URL and only adds
* remote when it is. This check is not perfect, it is mostly meant to catch typos. * the `origin` remote when they were. This check is not perfect, it is mostly meant to catch
* syntax-related typos.
*/ */
fun updateUrl(): Boolean { fun updateUrl(): GitUpdateUrlResult {
if (serverHostname.isEmpty() || !serverPort.isDigitsOnly()) if (serverHostname.isEmpty())
return false return GitUpdateUrlResult.EmptyHostnameError
if (!serverPort.isDigitsOnly())
return GitUpdateUrlResult.NonNumericPortError
val previousUrl = url ?: "" val previousUrl = url ?: ""
// Whether we need the leading ssh:// depends on the use of a custom port. // Whether we need the leading ssh:// depends on the use of a custom port.
@ -105,9 +117,9 @@ abstract class BaseGitActivity : AppCompatActivity() {
val pathPart = serverPath.trimStart('/', ':') val pathPart = serverPath.trimStart('/', ':')
"$userPart$hostnamePart:$pathPart" "$userPart$hostnamePart:$pathPart"
} else { } else {
// We only support absolute paths with custom ports. // Only absolute paths are supported with custom ports.
if (!serverPath.startsWith('/')) if (!serverPath.startsWith('/'))
return false return GitUpdateUrlResult.CustomPortRequiresAbsoluteUrlError
val pathPart = serverPath val pathPart = serverPath
// We have to specify the ssh scheme as this is the only way to pass a custom // We have to specify the ssh scheme as this is the only way to pass a custom
// port. // port.
@ -128,9 +140,9 @@ abstract class BaseGitActivity : AppCompatActivity() {
if (URI(url).rawAuthority != null) if (URI(url).rawAuthority != null)
url url
else else
return false return GitUpdateUrlResult.GenericError
} catch (_: Exception) { } catch (_: Exception) {
return false return GitUpdateUrlResult.GenericError
} }
} }
} }
@ -141,7 +153,7 @@ abstract class BaseGitActivity : AppCompatActivity() {
if (previousUrl.isNotEmpty() && newUrl != previousUrl && protocol == Protocol.Https) if (previousUrl.isNotEmpty() && newUrl != previousUrl && protocol == Protocol.Https)
encryptedSettings.edit { remove("https_password") } encryptedSettings.edit { remove("https_password") }
url = newUrl url = newUrl
return true return GitUpdateUrlResult.Ok
} }
/** /**

View file

@ -97,22 +97,23 @@ class GitServerConfigActivity : BaseGitActivity() {
binding.saveButton.setOnClickListener { binding.saveButton.setOnClickListener {
if (isClone && PasswordRepository.getRepository(null) == null) if (isClone && PasswordRepository.getRepository(null) == null)
PasswordRepository.initialize(this) PasswordRepository.initialize(this)
if (updateUrl()) { when (val result = updateUrl()) {
settings.edit { GitUpdateUrlResult.Ok -> {
putString("git_remote_protocol", protocol.pref) settings.edit {
putString("git_remote_auth", connectionMode.pref) putString("git_remote_protocol", protocol.pref)
putString("git_remote_server", serverHostname) putString("git_remote_auth", connectionMode.pref)
putString("git_remote_port", serverPort) putString("git_remote_server", serverHostname)
putString("git_remote_username", serverUser) putString("git_remote_port", serverPort)
putString("git_remote_location", serverPath) putString("git_remote_username", serverUser)
putString("git_remote_location", serverPath)
}
if (!isClone) {
Snackbar.make(binding.root, getString(R.string.git_server_config_save_success), Snackbar.LENGTH_SHORT).show()
Handler().postDelayed(500) { finish() }
} else
cloneRepository()
} }
if (!isClone) { else -> Snackbar.make(binding.root, getString(R.string.git_server_config_save_error_prefix, getString(result.textRes)), Snackbar.LENGTH_LONG).show()
Snackbar.make(binding.root, getString(R.string.git_server_config_save_success), Snackbar.LENGTH_SHORT).show()
Handler().postDelayed(500) { finish() }
} else
cloneRepository()
} else {
Snackbar.make(binding.root, getString(R.string.git_server_config_save_failure), Snackbar.LENGTH_LONG).show()
} }
} }
} }

View file

@ -363,7 +363,11 @@
<string name="connection_mode_openkeychain" translatable="false">OpenKeychain</string> <string name="connection_mode_openkeychain" translatable="false">OpenKeychain</string>
<string name="connection_mode_none">None</string> <string name="connection_mode_none">None</string>
<string name="git_server_config_save_success">Successfully saved configuration</string> <string name="git_server_config_save_success">Successfully saved configuration</string>
<string name="git_server_config_save_failure">Configuration error: please verify your settings and try again</string> <string name="git_server_config_save_error_prefix">Configuration error: %s</string>
<string name="git_config_error_hostname_empty">empty hostname</string>
<string name="git_config_error_generic">please verify your settings and try again</string>
<string name="git_config_error_nonnumeric_port">port must be numeric</string>
<string name="git_config_error_custom_port_absolute">path must be absolute (start with \'/\') when using a custom port</string>
<string name="git_operation_unable_to_open_ssh_key_title">Unable to open the ssh-key</string> <string name="git_operation_unable_to_open_ssh_key_title">Unable to open the ssh-key</string>
<string name="git_operation_unable_to_open_ssh_key_message">Please check that it was imported.</string> <string name="git_operation_unable_to_open_ssh_key_message">Please check that it was imported.</string>
<string name="git_operation_wrong_passphrase">Wrong passphrase</string> <string name="git_operation_wrong_passphrase">Wrong passphrase</string>