Commit c6b2bbcb authored by Yuanle Song's avatar Yuanle Song
Browse files

v0.3.0 add support for auth related options

support the same options as redis-cli
parent 238a0a99
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
default: build upload
check:
	pycodestyle redisexport/
	pylint redisexport/
clean:
	rm -rf dist/*
build: clean
@@ -7,4 +10,4 @@ test-upload:
	python3 -m twine upload --repository testpypi dist/*
upload:
	python3 -m twine upload dist/*
.PHONY: default clean build upload
.PHONY: default check clean build test-upload upload
+7 −2
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ To install redis-export, simply:

.. code-block:: bash

   $ pip install redis-export
   $ python3 -m pip install redis-export

You should add ~/.local/bin/ to your PATH.

@@ -69,7 +69,7 @@ You may clone it via ssh or https protocol
License
----------

Copyright (C) 2022 Yuanle Song <sylecn@gmail.com>
Copyright (C) 2022, 2023 Yuanle Song <sylecn@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -87,6 +87,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
ChangeLog
---------

* v0.3.0 2023-03-13

  - support auth related options. Use the same options as redis-cli.
  - add source code URL in README.rst

* v0.2.0 2022-09-24

  - update README.rst, applied license, distributed on PyPI.
+1 −1
Original line number Diff line number Diff line
@@ -5,4 +5,4 @@

"""

__version__ = "0.2.0"
__version__ = "0.3.0"
+28 −4
Original line number Diff line number Diff line
#!/usr/bin/env python3
# coding=utf-8
#
# Copyright (C) 2022  Yuanle Song <sylecn@gmail.com>
# Copyright (C) 2022, 2023  Yuanle Song <sylecn@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -25,6 +25,8 @@ import argparse
import logging
import codecs
import json
import os
from getpass import getpass

import redis

@@ -34,14 +36,14 @@ from redisexport import __version__
logger = logging.getLogger(__name__)


def tob64(bs):
def tob64(bs):    # pylint: disable=invalid-name
    """encode bytes to base64 string.

    """
    return codecs.decode(codecs.encode(bs, 'base64'))


def fromb64(s):
def fromb64(s):    # pylint: disable=invalid-name
    """decode base64 string to bytes.

    """
@@ -49,7 +51,19 @@ def fromb64(s):


def get_redis(args):
    return redis.Redis(host=args.host, port=args.port, db=args.num)
    if args.uri:
        return redis.Redis.from_url(args.uri)

    if args.askpass:
        password = getpass()
    elif args.password:
        password = args.password
    elif os.getenv('REDISCLI_AUTH'):
        password = os.getenv('REDISCLI_AUTH')
    else:
        password = None
    return redis.Redis(host=args.host, port=args.port, db=args.num,
                       password=password)


def export_db(args):
@@ -94,6 +108,16 @@ def create_shared_parser():
    parser.add_argument('--host', default='localhost', help='redis host')
    parser.add_argument('-p', '--port', default=6379, help='redis port')
    parser.add_argument('-n', '--num', help='redis database number')
    parser.add_argument('-a', '--pass', dest='password',
                        help='''Password to use when connecting to the server.
                        You can also use the REDISCLI_AUTH environment
                        variable to pass this password more safely
                        (if both are used, this argument takes precedence).''')
    parser.add_argument('--askpass', action='store_true',
                        help='''Force user to input password with mask from '
                        STDIN. If this argument is used, '-a' and REDISCLI_AUTH
                        environment variable will be ignored.''')
    parser.add_argument('-u', metavar='URI', dest='uri', help='Server URI.')
    return parser