Add configuration subsystem 73/10273/7
authorThanh Ha <thanh.ha@linuxfoundation.org>
Mon, 30 Apr 2018 18:16:03 +0000 (14:16 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 13 Jul 2018 18:15:06 +0000 (14:15 -0400)
Create a basic ini based configuration subsystem
to be used in the future by lftools subsystems
as a single point of pulling configuration from.

Usage:

  lftools config get section [OPTION]
  lftools config set section option value

Example:

  lftools config set global username zxiiro
  lftools config get global

Issue: RELENG-915
Change-Id: I346662ae2c623538e8a6881574dad357a8ae4dee
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
docs/commands/config.rst [new file with mode: 0644]
docs/commands/index.rst
lftools/cli/__init__.py
lftools/cli/config.py [new file with mode: 0644]
lftools/config.py [new file with mode: 0644]
requirements.txt

diff --git a/docs/commands/config.rst b/docs/commands/config.rst
new file mode 100644 (file)
index 0000000..341d80d
--- /dev/null
@@ -0,0 +1,21 @@
+******
+Config
+******
+
+.. program-output:: lftools config --help
+
+Commands
+========
+
+.. contents:: Config Commands
+    :local:
+
+get
+---
+
+.. program-output:: lftools config get --help
+
+set
+----
+
+.. program-output:: lftools config set --help
index c4bd35b..86e449b 100644 (file)
@@ -8,6 +8,7 @@ bash. It supports the following commands.
 .. toctree::
     :maxdepth: 2
 
+    config
     deploy
     license
     nexus
index ff844ff..635941a 100644 (file)
@@ -13,6 +13,7 @@ __author__ = 'Thanh Ha'
 
 import click
 
+from lftools.cli.config import config_sys
 from lftools.cli.deploy import deploy
 from lftools.cli.jenkins import jenkins_cli
 from lftools.cli.license import license
@@ -29,6 +30,7 @@ def cli(ctx):
     pass
 
 
+cli.add_command(config_sys)
 cli.add_command(deploy)
 cli.add_command(jenkins_cli, name='jenkins')
 cli.add_command(license)
diff --git a/lftools/cli/config.py b/lftools/cli/config.py
new file mode 100644 (file)
index 0000000..c671c8c
--- /dev/null
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: EPL-1.0
+##############################################################################
+# Copyright (c) 2018 The Linux Foundation and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+##############################################################################
+"""CLI interface for config subsystem."""
+
+__author__ = 'Thanh Ha'
+
+
+import click
+
+from lftools import config
+
+
+@click.group(name='config')
+@click.pass_context
+def config_sys(ctx):
+    """Configuration subsystem."""
+    pass
+
+
+@click.command(name='get')
+@click.argument('section', type=str)
+@click.argument('option', type=str, required=False)
+@click.pass_context
+def get_setting(ctx, section, option):
+    """Print section or setting from config file."""
+    result = config.get_setting(section, option)
+    if isinstance(result, list):
+        for i in result:
+            print('{}: {}'.format(i, config.get_setting(section, i)))
+    else:
+        print(result)
+
+
+@click.command(name='set')
+@click.argument('section')
+@click.argument('option')
+@click.argument('value')
+@click.pass_context
+def set_setting(ctx, section, option, value):
+    """Set a setting in the config file."""
+    config.set_setting(section, option, value)
+
+
+config_sys.add_command(get_setting)
+config_sys.add_command(set_setting)
diff --git a/lftools/config.py b/lftools/config.py
new file mode 100644 (file)
index 0000000..c6abc9a
--- /dev/null
@@ -0,0 +1,58 @@
+# -*- coding: utf-8 -*-
+# SPDX-License-Identifier: EPL-1.0
+##############################################################################
+# Copyright (c) 2018 The Linux Foundation and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+##############################################################################
+"""Configuration subsystem for lftools."""
+
+__author__ = 'Thanh Ha'
+
+import sys
+
+from six.moves import configparser
+from xdg import XDG_CONFIG_HOME
+
+LFTOOLS_CONFIG_FILE = '/'.join([XDG_CONFIG_HOME, 'lftools', 'lftools.ini'])
+
+
+def get_config():
+    """Get the config object."""
+    config = configparser.ConfigParser()
+    config.read(LFTOOLS_CONFIG_FILE)
+    return config
+
+
+def get_setting(section, option=None):
+    """Get a configuration from a section."""
+    config = get_config()
+
+    if option:
+        try:
+            return config.get(section, option)
+        except configparser.NoOptionError:
+            print('ERROR: Config option does not exist.')
+            sys.exit(1)
+        except configparser.NoSectionError:
+            print('ERROR: Config section does not exist.')
+            sys.exit(1)
+
+    else:
+        try:
+            return config.options(section)
+        except configparser.NoSectionError:
+            print('ERROR: Config section does not exist.')
+            sys.exit(1)
+
+
+def set_setting(section, option, value):
+    """Save a configuration setting to config file."""
+    config = get_config()
+    config.set(section, option, value)
+
+    with open(LFTOOLS_CONFIG_FILE, 'w') as configfile:
+        config.write(configfile)
index 2015e89..3df89f7 100644 (file)
@@ -2,7 +2,10 @@ click
 pyyaml
 requests~=2.18.0
 setuptools>=36.5.0
+six~=1.11.0
 python-jenkins~=1.1.0
+xdg~=1.0.7;python_version<'3'
+xdg~=3.0.0;python_version>='3'
 
 # workarounds to prevent upstream from breaking us
 netifaces==0.10.5