cli: split parsing from nmc_utils_read_passwd_file()

Makes it easier testable.
This commit is contained in:
Thomas Haller 2020-05-04 16:33:59 +02:00
parent 360f0fae11
commit 38a79ca5cd
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 22 additions and 7 deletions

View file

@ -611,16 +611,11 @@ nmc_utils_read_passwd_file (const char *passwd_file,
GError **error)
{
nm_auto_clear_secret_ptr NMSecretPtr contents = { 0 };
gs_unref_hashtable GHashTable *pwds_hash = NULL;
const char *contents_str;
gsize contents_line;
pwds_hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, (GDestroyNotify) nm_free_secret);
NM_SET_OUT (out_error_line, -1);
if (!passwd_file)
return g_steal_pointer (&pwds_hash);
return g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, (GDestroyNotify) nm_free_secret);
if (!nm_utils_file_get_contents (-1,
passwd_file,
@ -632,7 +627,23 @@ nmc_utils_read_passwd_file (const char *passwd_file,
error))
return NULL;
contents_str = contents.str;
return nmc_utils_parse_passwd_file (contents.str, out_error_line, error);
}
GHashTable *
nmc_utils_parse_passwd_file (char *contents /* will be modified */,
gssize *out_error_line,
GError **error)
{
gs_unref_hashtable GHashTable *pwds_hash = NULL;
const char *contents_str;
gsize contents_line;
pwds_hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, (GDestroyNotify) nm_free_secret);
NM_SET_OUT (out_error_line, -1);
contents_str = contents;
contents_line = 0;
while (contents_str[0]) {
nm_auto_free_secret char *l_hash_key = NULL;

View file

@ -43,6 +43,10 @@ const char *nmc_password_subst_char (void);
void nmc_print_qrcode (const char *str);
GHashTable *nmc_utils_parse_passwd_file (char *contents,
gssize *out_error_line,
GError **error);
GHashTable *nmc_utils_read_passwd_file (const char *passwd_file,
gssize *out_error_line,
GError **error);