creds: make --pretty behave in a slightly more expected manner

This commit is contained in:
Frantisek Sumsal 2023-04-25 12:58:15 +02:00
parent ee46e4d982
commit c74e13a5c3
2 changed files with 10 additions and 8 deletions

View file

@ -228,7 +228,8 @@
<listitem><para>When specified with <command>encrypt</command> controls whether to show the encrypted <listitem><para>When specified with <command>encrypt</command> controls whether to show the encrypted
credential as <varname>SetCredentialEncrypted=</varname> setting that may be pasted directly into a credential as <varname>SetCredentialEncrypted=</varname> setting that may be pasted directly into a
unit file.</para></listitem> unit file. Has effect only when used together with <option>--name=</option> and <literal>-</literal>
as the output file.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View file

@ -506,20 +506,21 @@ static int verb_encrypt(int argc, char **argv, void *userdata) {
if (base64_size < 0) if (base64_size < 0)
return base64_size; return base64_size;
if (arg_pretty) { /* Pretty print makes sense only if we're printing stuff to stdout
* and if a cred name is provided via --name= (since we can't use
* the output file name as the cred name here) */
if (arg_pretty && !output_path && name) {
_cleanup_free_ char *escaped = NULL, *indented = NULL, *j = NULL; _cleanup_free_ char *escaped = NULL, *indented = NULL, *j = NULL;
if (name) { escaped = cescape(name);
escaped = cescape(name); if (!escaped)
if (!escaped) return log_oom();
return log_oom();
}
indented = strreplace(base64_buf, "\n", " \\\n "); indented = strreplace(base64_buf, "\n", " \\\n ");
if (!indented) if (!indented)
return log_oom(); return log_oom();
j = strjoin("SetCredentialEncrypted=", name, ": \\\n ", indented, "\n"); j = strjoin("SetCredentialEncrypted=", escaped, ": \\\n ", indented, "\n");
if (!j) if (!j)
return log_oom(); return log_oom();