Fabric icon indicating copy to clipboard operation
Fabric copied to clipboard

Use ConfigParser to parse user config file

Open RyanBalfanz opened this issue 1 year ago • 2 comments

This fixes a bug caused by extra characters (e.g. newline) leading to an invalid ~/.config/fabric/.env file (according to the current parsing logic, that is). A newline should be allowed yet causes an exception to be raised with a traceback pointing elsewhere in the code. Such a traceback could be difficult to fix for some users and turn them away from fabric rather quickly. Plus, sometimes text editors will automatically add the newline.

Traceback (most recent call last):
  File "/Users/ryan/fabric/client/fabric", line 68, in <module>
    standalone.sendMessage(text)
  File "/Users/ryan/fabric/client/utils.py", line 100, in sendMessage
    print(response)
          ^^^^^^^^
UnboundLocalError: cannot access local variable 'response' where it is not associated with a value

Separately, I would also argue that the code around the traceback needs some changes too, but this change does fix the issue I first encountered.

RyanBalfanz avatar Feb 03 '24 00:02 RyanBalfanz

A simpler change that would also fix the extra newline in the .env file would be to simply strip apikey. If that is preferred I can update this PR.

https://github.com/danielmiessler/fabric/blob/fba34371af239490ca81e43746542e1917ee65d8/client/utils.py#L16

apikey = f.read().split("=")[1].strip()

I didn't want to introduce any new dependencies (e.g. python-dotenv, pydantic-settings), preferred to use the standard library, hence configparser.

RyanBalfanz avatar Feb 03 '24 00:02 RyanBalfanz

A simpler change that would also fix the extra newline in the .env file would be to simply strip apikey. If that is preferred I can update this PR.

Alternative PR at https://github.com/danielmiessler/fabric/pull/29

RyanBalfanz avatar Feb 03 '24 00:02 RyanBalfanz

I think we fixed this by pulling the key in a different way. Thank you so much for this, though!

danielmiessler avatar Feb 03 '24 22:02 danielmiessler