src/ostree: Add --group option to ostree config

Fetching value from a repo config using 'ostree config
get SECTIONNAME.KEYNAME' didn't work in some cases like
when having dots in Group Name entry.
As per Desktop entry file specification, Group Name
may contain all ASCII characters except for [ and ]
and control characters.
Link - https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.1.html

Having --group option will help user to clearly specify
Group Name and get desired result.

It also adds test for ostree config get|set and bash
completion for --group option

Fixes https://github.com/ostreedev/ostree/issues/1565

Closes: #1696
Approved by: cgwalters
This commit is contained in:
Sinny Kumari 2018-08-17 21:25:56 +05:30 committed by Atomic Bot
parent e7305bbc8a
commit dde3f1c0fb
5 changed files with 110 additions and 24 deletions

View File

@ -134,6 +134,7 @@ _installed_or_uninstalled_test_scripts = \
tests/test-repo-finder-mount-integration.sh \ tests/test-repo-finder-mount-integration.sh \
tests/test-summary-collections.sh \ tests/test-summary-collections.sh \
tests/test-pull-collections.sh \ tests/test-pull-collections.sh \
tests/test-config.sh \
$(NULL) $(NULL)
experimental_test_scripts = \ experimental_test_scripts = \

View File

@ -398,6 +398,7 @@ _ostree_config() {
" "
local options_with_args=" local options_with_args="
--group
--repo --repo
" "

View File

@ -51,10 +51,10 @@ Boston, MA 02111-1307, USA.
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>ostree config get</command> <arg choice="req">SECTIONNAME.KEYNAME</arg> <command>ostree config get</command> <arg choice="req"> --group=GROUPNAME</arg> <arg choice="req"> KEYNAME</arg>
</cmdsynopsis> </cmdsynopsis>
<cmdsynopsis> <cmdsynopsis>
<command>ostree config set</command> <arg choice="req">SECTIONNAME.KEYNAME</arg> <arg choice="req">VALUE</arg> <command>ostree config set</command> <arg choice="req"> --group=GROUPNAME</arg> <arg choice="req"> KEYNAME</arg> <arg choice="req">VALUE</arg>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
@ -68,7 +68,7 @@ Boston, MA 02111-1307, USA.
<refsect1> <refsect1>
<title>Example</title> <title>Example</title>
<para><command>$ ostree config get core.mode</command></para> <para><command>$ ostree config get --group=core mode</command></para>
<para>bare</para> <para>bare</para>
</refsect1> </refsect1>
</refentry> </refentry>

View File

@ -28,12 +28,15 @@
#include "ostree.h" #include "ostree.h"
#include "otutil.h" #include "otutil.h"
static char* opt_group;
/* ATTENTION: /* ATTENTION:
* Please remember to update the bash-completion script (bash/ostree) and * Please remember to update the bash-completion script (bash/ostree) and
* man page (man/ostree-config.xml) when changing the option list. * man page (man/ostree-config.xml) when changing the option list.
*/ */
static GOptionEntry options[] = { static GOptionEntry options[] = {
{ "group", 0, 0, G_OPTION_ARG_STRING, &opt_group , "Group name followed by key for a remote config", NULL },
{ NULL } { NULL }
}; };
@ -85,18 +88,32 @@ ostree_builtin_config (int argc, char **argv, OstreeCommandInvocation *invocatio
if (!strcmp (op, "set")) if (!strcmp (op, "set"))
{ {
if (argc < 4) printf("GROUP NUMBER = %s %d\n", opt_group, argc);
if (opt_group)
{ {
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, if (argc < 4)
"KEY and VALUE must be specified"); {
goto out; g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"GROUP name, KEY and VALUE must be specified");
goto out;
}
section = g_strdup(opt_group);
key = g_strdup(argv[2]);
value = argv[3];
}
else
{
if (argc < 4)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"KEY and VALUE must be specified");
goto out;
}
section_key = argv[2];
value = argv[3];
if(!split_key_string (section_key, &section, &key, error))
goto out;
} }
section_key = argv[2];
value = argv[3];
if (!split_key_string (section_key, &section, &key, error))
goto out;
config = ostree_repo_copy_config (repo); config = ostree_repo_copy_config (repo);
g_key_file_set_string (config, section, key, value); g_key_file_set_string (config, section, key, value);
@ -108,17 +125,29 @@ ostree_builtin_config (int argc, char **argv, OstreeCommandInvocation *invocatio
{ {
GKeyFile *readonly_config = NULL; GKeyFile *readonly_config = NULL;
g_autofree char *value = NULL; g_autofree char *value = NULL;
if (argc < 3) if (opt_group)
{ {
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, if (argc < 3)
"KEY must be specified"); {
goto out; g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Group name and key must be specified");
goto out;
}
section = g_strdup(opt_group);
key = g_strdup(argv[2]);
}
else
{
if(argc < 3)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"KEY must be specified");
goto out;
}
section_key = argv[2];
if (!split_key_string (section_key, &section, &key, error))
goto out;
} }
section_key = argv[2];
if (!split_key_string (section_key, &section, &key, error))
goto out;
readonly_config = ostree_repo_get_config (repo); readonly_config = ostree_repo_get_config (repo);
value = g_key_file_get_string (readonly_config, section, key, error); value = g_key_file_get_string (readonly_config, section, key, error);

55
tests/test-config.sh Executable file
View File

@ -0,0 +1,55 @@
#!/bin/bash
#
# Copyright (C) 2018 Sinny Kumari <skumari@redhat.com>
#
# SPDX-License-Identifier: LGPL-2.0+
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
set -euo pipefail
. $(dirname $0)/libtest.sh
echo '1..2'
ostree_repo_init repo
${CMD_PREFIX} ostree remote add --repo=repo --set=xa.title=Flathub --set=xa.title-is-set=true flathub https://dl.flathub.org/repo/
${CMD_PREFIX} ostree remote add --repo=repo org.mozilla.FirefoxRepo http://example.com/ostree/repo/
${CMD_PREFIX} ostree config --repo=repo get core.mode > list.txt
${CMD_PREFIX} ostree config --repo=repo get --group=core repo_version >> list.txt
${CMD_PREFIX} ostree config --repo=repo get --group='remote "flathub"' 'xa.title' >> list.txt
${CMD_PREFIX} ostree config --repo=repo get --group='remote "flathub"' 'xa.title-is-set' >> list.txt
${CMD_PREFIX} ostree config --repo=repo get --group='remote "org.mozilla.FirefoxRepo"' url >> list.txt
${CMD_PREFIX} cat list.txt
assert_file_has_content list.txt "bare"
assert_file_has_content list.txt "1"
assert_file_has_content list.txt "Flathub"
assert_file_has_content list.txt "true"
assert_file_has_content list.txt "http://example.com/ostree/repo/"
echo "ok config get"
${CMD_PREFIX} ostree config --repo=repo set core.mode bare-user-only
${CMD_PREFIX} ostree config --repo=repo set --group='remote "flathub"' 'xa.title' 'Nightly Flathub'
${CMD_PREFIX} ostree config --repo=repo set --group='remote "flathub"' 'xa.title-is-set' 'false'
${CMD_PREFIX} ostree config --repo=repo set --group='remote "org.mozilla.FirefoxRepo"' url http://example.com/ostree/
assert_file_has_content repo/config "bare-user-only"
assert_file_has_content repo/config "Nightly Flathub"
assert_file_has_content repo/config "false"
assert_file_has_content repo/config "http://example.com/ostree/"
echo "ok config set"