zypper.conf enabled using augeas
authorJán Kupec <jkupec@suse.cz>
Thu, 19 Mar 2009 16:32:14 +0000 (17:32 +0100)
committerJán Kupec <jkupec@suse.cz>
Thu, 19 Mar 2009 16:32:14 +0000 (17:32 +0100)
- plus some cleanup in color option names
- zypper.aug and zypper.conf added to spec file

CMakeLists.txt
src/CMakeLists.txt
src/Config.cc
src/Config.h
src/utils/colors.cc
zypper.conf
zypper.spec.cmake

index d0d87c0..91fce6a 100644 (file)
@@ -62,3 +62,9 @@ INSTALL(
   DESTINATION ${INSTALL_PREFIX}/share/doc/packages/${PACKAGE}
 )
 
+# system-wide zypper.conf
+INSTALL(
+  FILES zypper.conf
+  DESTINATION ${SYSCONFDIR}/zypp
+)
+
index c968e9a..18229f8 100644 (file)
@@ -140,5 +140,5 @@ INSTALL(
 # augeas lens
 INSTALL(
   FILES utils/zypper.aug
-  DESTINATION ${INSTALL_PREFIX}/share/zypper
+  DESTINATION ${INSTALL_PREFIX}/share/${PACKAGE}
 )
index 9dec290..b834bc2 100644 (file)
 \*---------------------------------------------------------------------------*/
 
 #include <iostream>
+#include <map>
+extern "C"
+{
+  #include <libintl.h>
+}
 
 #include "zypp/base/Logger.h"
 #include "zypp/base/Measure.h"
+#include "zypp/base/String.h"
+#include "zypp/base/Exception.h"
 
+#include "utils/Augeas.h"
 #include "Config.h"
 
+// redefine _ gettext macro defined by ZYpp
+#ifdef _
+#undef _
+#endif
+#define _(MSG) ::gettext(MSG)
+
 using namespace std;
 using namespace zypp;
 
+static map<string, ConfigOption::Option> _table;
+static map<ConfigOption::Option, string> _table_str;
+
+const ConfigOption ConfigOption::COLOR_USE_COLORS(ConfigOption::COLOR_USE_COLORS_e);
+const ConfigOption ConfigOption::COLOR_BACKGROUND(ConfigOption::COLOR_BACKGROUND_e);
+const ConfigOption ConfigOption::COLOR_RESULT(ConfigOption::COLOR_RESULT_e);
+const ConfigOption ConfigOption::COLOR_MSG_STATUS(ConfigOption::COLOR_MSG_STATUS_e);
+const ConfigOption ConfigOption::COLOR_MSG_ERROR(ConfigOption::COLOR_MSG_ERROR_e);
+const ConfigOption ConfigOption::COLOR_MSG_WARNING(ConfigOption::COLOR_MSG_WARNING_e);
+const ConfigOption ConfigOption::COLOR_POSITIVE(ConfigOption::COLOR_POSITIVE_e);
+const ConfigOption ConfigOption::COLOR_NEGATIVE(ConfigOption::COLOR_NEGATIVE_e);
+const ConfigOption ConfigOption::COLOR_PROMPT_OPTION(ConfigOption::COLOR_PROMPT_OPTION_e);
+const ConfigOption ConfigOption::COLOR_PROMPT_SHORTHAND(ConfigOption::COLOR_PROMPT_SHORTHAND_e);
+
+ConfigOption::ConfigOption(const std::string & strval_r)
+  : _value(parse(strval_r))
+{}
+
+ConfigOption::Option ConfigOption::parse(const std::string & strval_r)
+{
+  if (_table.empty())
+  {
+    // initialize it
+    _table["color/useColors"] = ConfigOption::COLOR_USE_COLORS_e;
+    _table["color/background"] = ConfigOption::COLOR_BACKGROUND_e;
+    _table["color/result"] = ConfigOption::COLOR_RESULT_e;
+    _table["color/msgStatus"] = ConfigOption::COLOR_MSG_STATUS_e;
+    _table["color/msgError"] = ConfigOption::COLOR_MSG_ERROR_e;
+    _table["color/msgWarning"] = ConfigOption::COLOR_MSG_WARNING_e;
+    _table["color/positive"] = ConfigOption::COLOR_POSITIVE_e;
+    _table["color/negative"] = ConfigOption::COLOR_NEGATIVE_e;
+    _table["color/promptOption"] = ConfigOption::COLOR_PROMPT_OPTION_e;
+  }
+  map<string, ConfigOption::Option>::const_iterator it = _table.find(strval_r);
+  if (it == _table.end())
+  {
+    string message =
+      zypp::str::form(_("Unknown configuration option '%s'"), strval_r.c_str());
+    ZYPP_THROW(zypp::Exception(message));
+  }
+  return it->second;
+}
+
+const string ConfigOption::asString() const
+{
+  if (_table.empty())
+  {
+    // initialize it
+    _table_str[COLOR_USE_COLORS_e] = string("color/useColors");
+    _table_str[COLOR_BACKGROUND_e] = "color/background";
+    _table_str[COLOR_RESULT_e] = "color/result";
+    _table_str[COLOR_MSG_STATUS_e] = "color/msgStatus";
+    _table_str[COLOR_MSG_ERROR_e] = "color/msgError";
+    _table_str[COLOR_MSG_WARNING_e] = "color/msgWarning";
+    _table_str[COLOR_POSITIVE_e] = "color/positive";
+    _table_str[COLOR_NEGATIVE_e] = "color/negative";
+    _table_str[COLOR_PROMPT_OPTION_e] = "color/promptOption";
+  }
+  map<ConfigOption::Option, string>::const_iterator it = _table_str.find(_value);
+  if (it != _table_str.end())
+    return it->second;
+  return string();
+}
+
+
 Config::Config()
-  : do_colors(false)
-  , color_useColors("never")
-  , color_background(false)          // dark background
-  , color_colorResult     ("white")  // default colors for dark background
-  , color_colorMsgStatus  ("grey")   // if background is actually light, these
-  , color_colorMsgError   ("red")    // colors will be overwritten in read()
-  , color_colorMsgWarning ("yellow")
-  , color_colorPositive   ("green")
-  , color_colorNegative   ("red")
-  , color_colorPromptOption("grey")
-  , color_colorPromptShorthand("yellow")
+  : do_colors        (false)
+  , color_useColors  ("never")
+  , color_background (false)    // dark background
+  , color_result     ("white")  // default colors for dark background
+  , color_msgStatus  ("grey")   // if background is actually light, these
+  , color_msgError   ("red")    // colors will be overwritten in read()
+  , color_msgWarning ("yellow")
+  , color_positive   ("green")
+  , color_negative   ("red")
+  , color_promptOption("grey")
 {}
 
 void Config::read()
 {
   debug::Measure m("ReadConfig");
 
-  // get augeas
+  Augeas augeas;
 
   m.elapsed();
 
@@ -43,99 +121,87 @@ void Config::read()
 
   // ---------------[ colors ]------------------------------------------------
 
-  // color_useColors = augeas.getOption("colors/useColors");
+  color_useColors = augeas.getOption(ConfigOption::COLOR_USE_COLORS.asString());
   do_colors =
     (color_useColors == "autodetect" && has_colors())
     || color_useColors == "always";
 
-  ////// colors/background //////
+  ////// color/background //////
 
   string s;
-  // s = augeas.getOption("colors/background");
+  s = augeas.getOption(ConfigOption::COLOR_BACKGROUND.asString());
   if (s == "light")
     color_background = true;
   else if (!s.empty() && s != "dark")
-    ERR << "invalid colors/background value: " << s << endl;
+    ERR << "invalid color/background value: " << s << endl;
 
-  Color c("none");
+  Color c("");
 
-  ////// colors/colorResult //////
+  ////// color/colorResult //////
 
-  // c =  augeas.getOption("colors/colorResult");
+  c = Color(augeas.getOption(ConfigOption::COLOR_RESULT.asString()));
   if (c.value().empty())
   {
     // set a default for light background
     if (color_background)
-      color_colorResult = Color("black");
+      color_result = Color("black");
   }
   else
-    color_colorResult = c;
+    color_result = c;
 
-  ////// colors/colorMsgStatus //////
+  ////// color/colorMsgStatus //////
 
-  // c =  augeas.getOption("colors/colorMsgStatus");
+  c = Color(augeas.getOption(ConfigOption::COLOR_MSG_STATUS.asString()));
   if (c.value().empty())
   {
     // set a default for light background
     if (color_background)
-      color_colorMsgStatus = Color("default");
+      color_msgStatus = Color("default");
   }
   else
-    color_colorMsgStatus = c;
+    color_msgStatus = c;
 
-  ////// colors/colorMsgError //////
+  ////// color/colorMsgError //////
 
-  // c =  augeas.getOption("colors/colorMsgError");
+  c = Color(augeas.getOption(ConfigOption::COLOR_MSG_ERROR.asString()));
   if (!c.value().empty())
-    color_colorMsgError = c;
+    color_msgError = c;
 
-  ////// colors/colorMsgWarning //////
+  ////// color/colorMsgWarning //////
 
-  // c =  augeas.getOption("colors/colorMsgWarning");
+  c = Color(augeas.getOption(ConfigOption::COLOR_MSG_WARNING.asString()));
   if (c.value().empty())
   {
     // set a default for light background
     if (color_background)
-      color_colorMsgWarning = Color("brown");
+      color_msgWarning = Color("brown");
   }
   else
-    color_colorMsgWarning = c;
+    color_msgWarning = c;
 
-  ////// colors/colorPositive //////
+  ////// color/colorPositive //////
 
-  // c =  augeas.getOption("colors/colorPositive");
+  c = Color(augeas.getOption(ConfigOption::COLOR_POSITIVE.asString()));
   if (!c.value().empty())
-    color_colorPositive = c;
+    color_positive = c;
 
-  ////// colors/colorNegative //////
+  ////// color/colorNegative //////
 
-  // c =  augeas.getOption("colors/colorNegative");
+  c = Color(augeas.getOption(ConfigOption::COLOR_NEGATIVE.asString()));
   if (!c.value().empty())
-    color_colorNegative = c;
-
-  ////// colors/colorPromptOption //////
-
-  // c =  augeas.getOption("colors/colorPromptOption");
-  if (c.value().empty())
-  {
-    // set a default for light background
-    if (color_background)
-      color_colorPromptOption = Color("darkgrey");
-  }
-  else
-    color_colorPromptOption = c;
+    color_negative = c;
 
-  ////// colors/colorPromptShorthand //////
+  ////// color/colorPromptOption //////
 
-  // c =  augeas.getOption("colors/colorPromptShorthand");
+  c = Color(augeas.getOption(ConfigOption::COLOR_PROMPT_OPTION.asString()));
   if (c.value().empty())
   {
     // set a default for light background
     if (color_background)
-      color_colorPromptShorthand = Color("cyan");
+      color_promptOption = Color("darkgrey");
   }
   else
-    color_colorPromptShorthand = c;
+    color_promptOption = c;
 
   m.stop();
 }
index f22b5be..3330b8d 100644 (file)
 
 #include "utils/colors.h"
 
+class ConfigOption
+{
+public:
+  static const ConfigOption COLOR_USE_COLORS;
+  static const ConfigOption COLOR_BACKGROUND;
+  static const ConfigOption COLOR_RESULT;
+  static const ConfigOption COLOR_MSG_STATUS;
+  static const ConfigOption COLOR_MSG_ERROR;
+  static const ConfigOption COLOR_MSG_WARNING;
+  static const ConfigOption COLOR_POSITIVE;
+  static const ConfigOption COLOR_NEGATIVE;
+  static const ConfigOption COLOR_PROMPT_OPTION;
+  static const ConfigOption COLOR_PROMPT_SHORTHAND;
+
+  enum Option
+  {
+    COLOR_USE_COLORS_e,
+    COLOR_BACKGROUND_e,
+    COLOR_RESULT_e,
+    COLOR_MSG_STATUS_e,
+    COLOR_MSG_ERROR_e,
+    COLOR_MSG_WARNING_e,
+    COLOR_POSITIVE_e,
+    COLOR_NEGATIVE_e,
+    COLOR_PROMPT_OPTION_e,
+    COLOR_PROMPT_SHORTHAND_e
+  };
+
+  ConfigOption(Option option) : _value(option) {}
+
+  explicit ConfigOption(const std::string & strval_r);
+
+  Option toEnum() const { return _value; }
+
+  ConfigOption::Option parse(const std::string & strval_r);
+
+  const std::string asString() const;
+
+private:
+  Option _value;
+};
+
+
+/**
+ *
+ */
 struct Config
 {
   /** Initializes the config options to defaults. */
@@ -37,14 +83,13 @@ struct Config
    */
   bool color_background;
 
-  Color color_colorResult;
-  Color color_colorMsgStatus;
-  Color color_colorMsgError;
-  Color color_colorMsgWarning;
-  Color color_colorPositive;
-  Color color_colorNegative;
-  Color color_colorPromptOption;
-  Color color_colorPromptShorthand;
+  Color color_result;
+  Color color_msgStatus;
+  Color color_msgError;
+  Color color_msgWarning;
+  Color color_positive;
+  Color color_negative;
+  Color color_promptOption;
 };
 
 #endif /* ZYPPER_CONFIG_H_ */
index 2b90c60..1bc5d0f 100644 (file)
@@ -26,6 +26,9 @@ Color::Color(const string & color_str)
 
 string Color::parse(const string & value)
 {
+  if (value.empty())
+    return value;
+
   if (str2esc.empty())
   {
     str2esc["green"]          = COLOR_GREEN;
@@ -73,21 +76,19 @@ static const string get_color(const Config & conf, const ColorContext context)
   switch (context)
   {
   case COLOR_CONTEXT_RESULT:
-    return conf.color_colorResult.value();
+    return conf.color_result.value();
   case COLOR_CONTEXT_MSG_STATUS:
-    return conf.color_colorMsgStatus.value();
+    return conf.color_msgStatus.value();
   case COLOR_CONTEXT_MSG_WARNING:
-    return conf.color_colorMsgWarning.value();
+    return conf.color_msgWarning.value();
   case COLOR_CONTEXT_MSG_ERROR:
-    return conf.color_colorMsgError.value();
+    return conf.color_msgError.value();
   case COLOR_CONTEXT_POSTIVE:
-    return conf.color_colorPositive.value();
+    return conf.color_positive.value();
   case COLOR_CONTEXT_NEGATIVE:
-    return conf.color_colorNegative.value();
+    return conf.color_negative.value();
   case COLOR_CONTEXT_PROMPT_OPTION:
-    return conf.color_colorPromptOption.value();
-  case COLOR_CONTEXT_PROMPT_SHORTHAND:
-    return conf.color_colorPromptShorthand.value();
+    return conf.color_promptOption.value();
   default:
     return COLOR_RESET;
   }
index 495d5d2..98ecc21 100644 (file)
@@ -1,10 +1,20 @@
 ## Configuration file for Zypper.
 ##
+## location: /etc/zypp/zypper.conf (system-wide)
+##       or: $HOME/.zypper.conf    (user-specific)
+##
 ## You can edit this file by hand (try to keep the structure) or by using
-## 'zypper conf' command (TODO).
+## 'zypper conf' command (TODO). If you need user-specific configuration
+## and you do not have ~/.zypper.conf yet, use /etc/zypp/zypper.conf
+## as template (copy it to ~/zypper.conf) and adjust to your liking.
+##
 ## The options known also to libzypp will be overriden by zypper.conf, if set.
 ##
-## Boolean values are 0 1 yes no on off true false
+## Boolean values are:
+##   0 1 yes no on off true false
+## Color values are:
+##   white grey darkgrey black red green blue yellow brown cyan purple
+##   lightred lightgreen lighblue lighcyan lighpurple
 
 [main]
 
 
 ## Do you use dark or light terminal background?
 ##
+## Valid values: dark light
 ## Default value: dark
 ##
 # background = dark
 
-## TODO: options to configure specific colorization
+## Color for displaying results of operations.
+## This includes installation summary, tables, and result messages.
+##
+## Valid values: color
+## Default value: white
+##
+# result = white
+
+## Color for displaying status and progress messages.
+##
+## Valid values: color
+## Default value: grey
+##
+# msgStatus = grey
+
+## Color for displaying error messages.
+##
+## Valid values: color
+## Default value: red
+##
+# msgError = red
+
+## Color for displaying warnings.
+##
+## Valid values: color
+## Default value: yellow
+##
+# msgWarning = yellow
+
+## Color for highlighting positive information.
+## For example, 'done' result of progress indicator.
+## (not used yet)
+##
+## Default value: green
+##
+# positive = green
+
+## Color for highlighting negative information.
+## For example, 'error' result of progress indicator.
+## (not used yet)
+##
+## Valid values: color
+## Default value: red
+##
+# negative = red
+
+## Color for user dialog options.
+##
+## Valid values: color
+## Default value: grey
+##
+# promptOption = grey
+
index 10312b9..d7e9f2b 100644 (file)
@@ -11,7 +11,8 @@
 # norootforbuild
 
 Name:           @PACKAGE@
-BuildRequires:  libzypp-devel >= 6.1.0 boost-devel >= 1.33.1 gettext-devel >= 0.15 readline-devel >= 5.1
+BuildRequires:  libzypp-devel >= 6.1.0 boost-devel >= 1.33.1 gettext-devel >= 0.15
+BuildRequires:  readline-devel >= 5.1 augeas-devel >= 0.4.2
 BuildRequires:  gcc-c++ >= 4.1 cmake >= 2.4.6
 Requires:      procps
 Recommends:     logrotate cron 
@@ -83,14 +84,16 @@ touch %buildroot%_var/log/zypper.log
 
 %files -f zypper.lang
 %defattr(-,root,root)
-%{_sysconfdir}/logrotate.d/zypper.lr
-%{_sysconfdir}/logrotate.d/zypp-refresh.lr
+%config(noreplace) %{_sysconfdir}/zypp/zypper.conf
+%config(noreplace) %{_sysconfdir}/logrotate.d/zypper.lr
+%config(noreplace) %{_sysconfdir}/logrotate.d/zypp-refresh.lr
 %{_sysconfdir}/bash_completion.d/zypper.sh
 %{_bindir}/zypper
 %{_bindir}/installation_sources
 %{_sbindir}/zypp-refresh
 %verify(not mode) %attr (755,root,root) %{_sbindir}/zypp-refresh-wrapper
 %dir %{prefix}/share/zypper
+%{prefix}/share/zypper/zypper.aug
 %dir %{prefix}/share/zypper/xml
 %{prefix}/share/zypper/xml/xmlout.rnc
 %dir %{prefix}/include/zypper