这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@tsroten
Copy link
Member

@tsroten tsroten commented Nov 14, 2015

This fixes #161 by introducing system-wide config files. Right now, only '/etc/myclirc' is checked, but adding other locations to the code is a simple as adding an item to a list. If the last successful file read is not a system-wide config file, then mycli checks to see if it should write the default user config file (by seeing if one exists). But, if the last successful file read was a system-wide config file, then no default user config file is created.

This cleans up and refactors some of the ConfigObj() code to use a generic function that handles two types of errors: config parsing errors and file permission errors.

Because logging isn't initialized before we read the config files, these two outputs are possible:

% mycli
You don't have permission to read config file '/etc/myclirc'.
Version: 1.5.2
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Foo
mysql dev@localhost:(none)>

And:

% mycli
Error parsing config file '/etc/myclirc'.
Recovering partially parsed config values.
Version: 1.5.2
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Foo
mysql dev@localhost:(none)>

I'm not sure if there is a better way to deal with those messages. They aren't printed by mycli.py so they don't know about click.secho(). They are basic logging calls to the default logging handler (stderr). Can anyone think of a better way to handle this? Or are we happy with it as it is?

@tsroten tsroten mentioned this pull request Nov 14, 2015
mycli/config.py Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick to avoid stomping on built-in file keyword.

@amjith
Copy link
Member

amjith commented Nov 14, 2015

Looks like you're targeting Python 3. There is no such thing called PermissionError in Python 2.7.

@amjith
Copy link
Member

amjith commented Nov 14, 2015

I was surprised when you said it'll print the error messages at startup because I've never seen that behavior when loggers are uninitialized.

Here's what I get when there is a parsing error in /etc/myclirc.

No handlers could be found for logger "mycli.config"
Version: 1.5.2
Chat: https://gitter.im/dbcli/mycli
Mail: https://groups.google.com/forum/#!forum/mycli-users
Home: http://mycli.net
Thanks to the contributor - Chris Anderton
mysql root@localhost:django_poll>

@tsroten
Copy link
Member Author

tsroten commented Nov 14, 2015

It looks like, as of Python 3.2, logging writes to sys.stderr using a StreamHandler in the event that logging isn't configured. In Python 2, we'd have to add a NullHandler in order to silence that annoying No handlers could be found... message.

I think that most users expect command-line apps to write user-facing error messages to stderr. I'd hate for our Python 2 users to not see a potentially helpful error message. So, I suggest that we use the print() function to write to sys.stderr for all rc-related file reading/parsing (instead of using the logging library). @amjith What are your thoughts on that?

By the way, thanks for pointing out the PermissionsError. I'll fix that.

@amjith
Copy link
Member

amjith commented Nov 15, 2015

It looks like, as of Python 3.2, logging writes to sys.stderr using a StreamHandler in the event that logging isn't configured.

Python 3 really makes it easy to do the right thing.

I think that most users expect command-line apps to write user-facing error messages to stderr. I'd hate for our Python 2 users to not see a potentially helpful error message. So, I suggest that we use the print() function to write to sys.stderr for all rc-related file reading/parsing (instead of using the logging library). @amjith What are your thoughts on that?

Yes!! I completely agree. We should make choices that are most intuitive for the user. So, let's use print statements there.

mycli/config.py Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch adding the line numbers. We might get some complaints regarding this error message since ConfigObj will fail on some of the constructs such as !includedir /usr/local/etc/my.cnf.d. But I think that is inevitable.

@amjith
Copy link
Member

amjith commented Nov 15, 2015

An interesting side effect of this change is now I'm aware of an inefficiency. I noticed that when I launched mycli I got the Error parsing... message three times. Further investigation revealed we're reading it once for pager, once for prompt and once for connection details.

Optimizing that should help a little bit with startup time. :)

@amjith
Copy link
Member

amjith commented Nov 15, 2015

Changes look good and it works without a hitch. I'm a bit hesitant about the parsing error being printed at startup. ConfigObj can't parse some of the INI file extension that might be valid for mysql but not valid for ConfigObj. So people can't change those lines to be ConfigObj compliant. This means we'll be printing that error message every time they start mycli. Which seems excessive.

Since this is only an issue when we're reading my.cnf files, can we log those messages to the log file instead of stdout?

This means, errors in RC files will be printed to stdout, but errors in my.cnf files will be logged to the log file.

I can't think of a better solution. Suggestions welcome.

@tsroten
Copy link
Member Author

tsroten commented Nov 15, 2015

@amjith I agree. I'll have config.py check to see if there is a parent logger (which means logging is initialized) to know whether to print to stderr or write to the logging file. That means:

  • All *.cnf-related errors will be written to the log file, not printed to stderr.
  • The first pass (which I'll work on optimizing later) on the myclircs will print errors to stderr. Subsequent passes will write to the logging file.

@tsroten
Copy link
Member Author

tsroten commented Nov 16, 2015

@amjith Ok, I've added those changes. There is some optimizing to reading config files that could be done, but this doesn't seem like the appropriate pull request for that. Let me know what you think of the changes.

@amjith
Copy link
Member

amjith commented Nov 16, 2015

Looks good. 👍

You're right the optimization are a digression to this PR. We can do it on a different PR.

amjith added a commit that referenced this pull request Nov 16, 2015
Adds support for system-wide config file.
@amjith amjith merged commit 4f1f040 into master Nov 16, 2015
@amjith amjith deleted the tsroten/systemwide-config branch November 16, 2015 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System wide myclirc

3 participants