src/ostree: Add --group option to ostree config
authorSinny Kumari <sinny@redhat.com>
Fri, 17 Aug 2018 15:55:56 +0000 (21:25 +0530)
committerAtomic Bot <atomic-devel@projectatomic.io>
Mon, 20 Aug 2018 14:31:15 +0000 (14:31 +0000)
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

Makefile-tests.am
bash/ostree
man/ostree-config.xml
src/ostree/ot-builtin-config.c
tests/test-config.sh [new file with mode: 0755]

index 3323c5f3a09b38dffac8cbae8280c751862f020e..9837e5cdf0dbcf43aa3d11b28d57e183d8329b46 100644 (file)
@@ -134,6 +134,7 @@ _installed_or_uninstalled_test_scripts = \
        tests/test-repo-finder-mount-integration.sh \
        tests/test-summary-collections.sh \
        tests/test-pull-collections.sh \
+       tests/test-config.sh \
        $(NULL)
 
 experimental_test_scripts = \
index d7b54373b900c0a040083623362973da985c748a..677aee7c48ab5a6bff31b4f4aae60a547ed34ac2 100644 (file)
@@ -398,6 +398,7 @@ _ostree_config() {
     "
 
     local options_with_args="
+        --group
         --repo
     "
 
index f12326029b368a67e10468035b91ccdc7b52534a..f052aebb9cf1d5e7743b7916b4aa046f00e1f29a 100644 (file)
@@ -51,10 +51,10 @@ Boston, MA 02111-1307, USA.
 
     <refsynopsisdiv>
             <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>
-                <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>
     </refsynopsisdiv>
 
@@ -68,7 +68,7 @@ Boston, MA 02111-1307, USA.
 
     <refsect1>
         <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>
     </refsect1>
 </refentry>
index 89bf3df9c193a014f41aa24618bb397b90015653..b9fa824d8e95b89e25ff05a4ef7704f0a23465a6 100644 (file)
 #include "ostree.h"
 #include "otutil.h"
 
+static char* opt_group;
+
 /* ATTENTION:
  * Please remember to update the bash-completion script (bash/ostree) and
  * man page (man/ostree-config.xml) when changing the option list.
  */
 
 static GOptionEntry options[] = {
+  { "group", 0, 0, G_OPTION_ARG_STRING, &opt_group , "Group name followed by key for a remote config", NULL },
   { NULL }
 };
 
@@ -44,7 +47,7 @@ split_key_string (const char   *k,
                   GError     **error)
 {
   const char *dot = strchr (k, '.');
-  
+
   if (!dot)
     {
       return glnx_throw (error,
@@ -85,18 +88,32 @@ ostree_builtin_config (int argc, char **argv, OstreeCommandInvocation *invocatio
 
   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,
-                       "KEY and VALUE must be specified");
-          goto out;
+          if (argc < 4)
+            {
+                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);
       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;
       g_autofree char *value = NULL;
-      if (argc < 3)
+      if (opt_group)
         {
-          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                       "KEY must be specified");
-          goto out;
+          if (argc < 3)
+            {
+              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);
       value = g_key_file_get_string (readonly_config, section, key, error);
@@ -133,7 +162,7 @@ ostree_builtin_config (int argc, char **argv, OstreeCommandInvocation *invocatio
                    "Unknown operation %s", op);
       goto out;
     }
-  
+
   ret = TRUE;
  out:
   if (config)
diff --git a/tests/test-config.sh b/tests/test-config.sh
new file mode 100755 (executable)
index 0000000..b1ea3e5
--- /dev/null
@@ -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"