Skip to content

Commit

Permalink
Fixes #154. Add netrc support to ia configure.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjake committed Mar 14, 2018
1 parent 22550e3 commit b290b31
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions internetarchive/cli/ia_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
"""Configure 'ia' with your Archive.org credentials.
usage:
ia configure
ia configure --username=<username> --password=<password>
ia configure --netrc
ia configure [--help]
ia configure [--username=<username> --password=<password>]
options:
-h, --help
-u, --username=<username> Provide username as an option rather than
providing it interactively.
-p, --password=<password> Provide password as an option rather than
providing it interactively.
-n, --netrc Use netrc file for login.
"""
from __future__ import absolute_import, print_function, unicode_literals
import sys
import netrc

from docopt import docopt

Expand All @@ -42,20 +46,32 @@
def main(argv, session):
args = docopt(__doc__, argv=argv)
try:
# CLI params.
if args['--username'] and args['--password']:
config_file_path = configure(args['--username'],
args['--password'],
session.config_file)
print('Config saved to: {0}'.format(config_file_path))

# Netrc
elif args['--netrc']:
print("Configuring 'ia' with netrc file...")
try:
n = netrc.netrc()
except netrc.NetrcParseError as exc:
print('error: netrc.netrc() cannot parse your .netrc file.')
sys.exit(1)
username, _, password = n.hosts['archive.org']
config_file_path = configure(username, password,
config_file=session.config_file)
print('Config saved to: {0}'.format(config_file_path))

# Interactive input.
else:
print("Enter your Archive.org credentials below to configure 'ia'.\n")
config_file_path = configure(config_file=session.config_file)
print('\nConfig saved to: {0}'.format(config_file_path))

except AuthenticationError as exc:
# TODO: refactor output so we don't have to have special cases
# for adding newlines!
if args['--username']:
print('error: {0}'.format(str(exc)))
else:
print('\nerror: {0}'.format(str(exc)))
print('\nerror: {0}'.format(str(exc)))
sys.exit(1)

0 comments on commit b290b31

Please sign in to comment.