CLI: use GNU style for arguments

add short argument options
This commit is contained in:
Nicola Murino 2019-08-08 12:03:24 +02:00
parent 4d3c8fb6c4
commit a7158634a0
2 changed files with 91 additions and 79 deletions

View file

@ -8,16 +8,22 @@ You can see the usage with the following command:
python sftpgo_api_cli.py --help
```
Basically there is a subcommand for each REST API and a two global arguments:
and
- `debug`, default disabled, print useful debug info.
- `base_url`, default `http://127.0.0.1:8080`. Base URL for SFTPGo REST API
- `auth_type` supported auth type are `basic` and `digest`. Default none
- `auth_user`
- `auth_password`
- `verify`, disable to ignore verifying the SSL certificate. Default enabled
```
python sftpgo_api_cli.py [sub-command] --help
```
For each subcommand `--help` shows the required arguments, try for example:
Basically there is a subcommand for each REST API and a some global arguments:
- `--debug`, default disabled, print useful debug info.
- `--base-url`, default `http://127.0.0.1:8080`. Base URL for SFTPGo REST API
- `--auth-type`, HTTP auth type. Supported HTTP auth type are `basic` and `digest`. Default none
- `--auth-user`, user for HTTP authentication
- `--auth-password`, password for HTTP authentication
- `--secure`, disable to ignore verifying the SSL certificate. Default enabled
For each subcommand `--help` shows the available arguments, try for example:
```python sftpgo_api_cli.py add_user --help```
@ -28,7 +34,7 @@ Let's see a sample usage for each REST API.
Command:
```
python sftpgo_api_cli.py add_user test_username --password "test_pwd" --home_dir="/tmp/test_home_dir" --uid 33 --gid 1000 --max_sessions 2 --quota_size 0 --quota_files 3 --permissions "list" "download" "upload" "delete" "rename" "create_dirs" --upload_bandwidth 100 --download_bandwidth 60
python sftpgo_api_cli.py add-user test_username --password "test_pwd" --home-dir="/tmp/test_home_dir" --uid 33 --gid 1000 --max-sessions 2 --quota-size 0 --quota-files 3 --permissions "list" "download" "upload" "delete" "rename" "create_dirs" --upload-bandwidth 100 --download-bandwidth 60
```
Output:
@ -64,7 +70,7 @@ Output:
Command:
```
python sftpgo_api_cli.py update_user 5140 test_username --password "test_pwd" --home_dir="/tmp/test_home_dir" --uid 0 --gid 33 --max_sessions 2 --quota_size 0 --quota_files 4 --permissions "*" --upload_bandwidth 90 --download_bandwidth 80
python sftpgo_api_cli.py update-user 5140 test_username --password "test_pwd" --home-dir="/tmp/test_home_dir" --uid 0 --gid 33 --max-sessions 3 --quota-size 0 --quota-files 4 --permissions "*" --upload-bandwidth 90 --download-bandwidth 80
```
Output:
@ -82,7 +88,7 @@ Output:
Command:
```
python sftpgo_api_cli.py get_user_by_id 5140
python sftpgo_api_cli.py get-user-by-id 5140
```
Output:
@ -146,7 +152,7 @@ Output:
Command:
```
python sftpgo_api_cli.py get_sftp_connections
python sftpgo_api_cli.py get-sftp-connections
```
Output:
@ -177,7 +183,7 @@ Output:
Command:
```
python sftpgo_api_cli.py close_sftp_connection 76a11b22260ee4249328df28bef34dc64c70f7c097db52159fc24049eeb0e32c
python sftpgo_api_cli.py close-sftp-connection 76a11b22260ee4249328df28bef34dc64c70f7c097db52159fc24049eeb0e32c
```
Output:
@ -195,7 +201,7 @@ Output:
Command:
```
python sftpgo_api_cli.py get_quota_scans
python sftpgo_api_cli.py get-quota-scans
```
### Start quota scan
@ -203,7 +209,7 @@ python sftpgo_api_cli.py get_quota_scans
Command:
```
python sftpgo_api_cli.py start_quota_scan test_username
python sftpgo_api_cli.py start-quota-scan test_username
```
Output:
@ -221,7 +227,7 @@ Output:
Command:
```
python sftpgo_api_cli.py delete_user 5140
python sftpgo_api_cli.py delete-user 5140
```
Output:
@ -239,7 +245,7 @@ Output:
Command:
```
python sftpgo_api_cli.py get_version
python sftpgo_api_cli.py get-version
```
Output:

View file

@ -12,19 +12,19 @@ except ImportError:
class SFTPGoApiRequests:
def __init__(self, debug, baseUrl, authType, authUser, authPassword, verify):
self.userPath = urlparse.urljoin(baseUrl, "/api/v1/user")
self.quotaScanPath = urlparse.urljoin(baseUrl, "/api/v1/quota_scan")
self.activeConnectionsPath = urlparse.urljoin(baseUrl, "/api/v1/sftp_connection")
self.versionPath = urlparse.urljoin(baseUrl, "/api/v1/version")
def __init__(self, debug, baseUrl, authType, authUser, authPassword, secure):
self.userPath = urlparse.urljoin(baseUrl, '/api/v1/user')
self.quotaScanPath = urlparse.urljoin(baseUrl, '/api/v1/quota_scan')
self.activeConnectionsPath = urlparse.urljoin(baseUrl, '/api/v1/sftp_connection')
self.versionPath = urlparse.urljoin(baseUrl, '/api/v1/version')
self.debug = debug
if authType == "basic":
if authType == 'basic':
self.auth = requests.auth.HTTPBasicAuth(authUser, authPassword)
elif authType == "digest":
elif authType == 'digest':
self.auth = requests.auth.HTTPDigestAuth(authUser, authPassword)
else:
self.auth = None
self.verify = verify
self.verify = secure
def formatAsJSON(self, text):
if not text:
@ -106,98 +106,104 @@ class SFTPGoApiRequests:
def addCommonUserArguments(parser):
parser.add_argument('username', type=str)
parser.add_argument('--password', type=str, default="", help="default: %(default)s")
parser.add_argument('--public_keys', type=str, nargs='+', default=[], help="default: %(default)s")
parser.add_argument('--home_dir', type=str, default="", help="default: %(default)s")
parser.add_argument('--uid', type=int, default=0, help="default: %(default)s")
parser.add_argument('--gid', type=int, default=0, help="default: %(default)s")
parser.add_argument('--max_sessions', type=int, default=0, help="default: %(default)s")
parser.add_argument('--quota_size', type=int, default=0, help="default: %(default)s")
parser.add_argument('--quota_files', type=int, default=0, help="default: %(default)s")
parser.add_argument('--permissions', type=str, nargs='+', default=[],
parser.add_argument('-P', '--password', type=str, default="", help='Default: %(default)s')
parser.add_argument('-K', '--public-keys', type=str, nargs='+', default=[], help='Default: %(default)s')
parser.add_argument('-H', '--home-dir', type=str, default="", help='Default: %(default)s')
parser.add_argument('--uid', type=int, default=0, help='Default: %(default)s')
parser.add_argument('--gid', type=int, default=0, help='Default: %(default)s')
parser.add_argument('-C', '--max-sessions', type=int, default=0,
help='Maximum concurrent sessions. 0 means unlimited. Default: %(default)s')
parser.add_argument('-S', '--quota-size', type=int, default=0,
help='Maximum size allowed as bytes. 0 means unlimited. Default: %(default)s')
parser.add_argument('-F', '--quota-files', type=int, default=0, help="default: %(default)s")
parser.add_argument('-G', '--permissions', type=str, nargs='+', default=[],
choices=['*', 'list', 'download', 'upload', 'delete', 'rename', 'create_dirs',
'create_symlinks'], help="default: %(default)s")
parser.add_argument('--upload_bandwidth', type=int, default=0, help="default: %(default)s")
parser.add_argument('--download_bandwidth', type=int, default=0, help="default: %(default)s")
'create_symlinks'], help='Default: %(default)s')
parser.add_argument('-U', '--upload-bandwidth', type=int, default=0,
help='Maximum upload bandwidth as KB/s, 0 means unlimited. Default: %(default)s')
parser.add_argument('-D', '--download-bandwidth', type=int, default=0,
help='Maximum download bandwidth as KB/s, 0 means unlimited. Default: %(default)s')
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--base_url", type=str, default="http://127.0.0.1:8080",
help="Base URL for SFTPGo REST API. Default: %(default)s")
parser.add_argument("--auth_type", type=str, default=None, choices=["basic", "digest"],
help="Authentication type to use. Default: %(default)s")
parser.add_argument("--auth_user", type=str, default="",
help="User to use for authentication. Default: %(default)s")
parser.add_argument("--auth_password", type=str, default="",
help="Password to use for authentication. Default: %(default)s")
parser.add_argument("--debug", dest='debug', action='store_true')
parser.add_argument('-b', '--base-url', type=str, default='http://127.0.0.1:8080',
help='Base URL for SFTPGo REST API. Default: %(default)s')
parser.add_argument('-a', '--auth-type', type=str, default=None, choices=['basic', 'digest'],
help='HTTP authentication type. Default: %(default)s')
parser.add_argument("-u", "--auth-user", type=str, default="",
help='User to use for HTTP authentication. Default: %(default)s')
parser.add_argument('-p', '--auth-password', type=str, default='',
help='Password to use for HTTP authentication. Default: %(default)s')
parser.add_argument('-d', '--debug', dest='debug', action='store_true')
parser.set_defaults(debug=False)
parser.add_argument("--verify", dest='verify', action='store_true',
help="Set to false to ignore verifying the SSL certificate")
parser.set_defaults(verify=True)
parser.add_argument('-s', '--secure', dest='secure', action='store_true',
help='Set to false to ignore verifying the SSL certificate')
parser.set_defaults(secure=True)
subparsers = parser.add_subparsers(dest="command", help='sub-command --help')
subparsers = parser.add_subparsers(dest='command', help='sub-command --help')
subparsers.required = True
parserAddUser = subparsers.add_parser("add_user", help="Add a new SFTP user")
parserAddUser = subparsers.add_parser('add-user', help='Add a new SFTP user')
addCommonUserArguments(parserAddUser)
parserUpdateUser = subparsers.add_parser("update_user", help="Update an existing user")
parserUpdateUser.add_argument('id', type=int)
parserUpdateUser = subparsers.add_parser('update-user', help='Update an existing user')
parserUpdateUser.add_argument('id', type=int, help='User\'s ID to update')
addCommonUserArguments(parserUpdateUser)
parserDeleteUser = subparsers.add_parser("delete_user", help="Delete an existing user")
parserDeleteUser.add_argument('id', type=int)
parserDeleteUser = subparsers.add_parser('delete-user', help='Delete an existing user')
parserDeleteUser.add_argument('id', type=int, help='User\'s ID to delete')
parserGetUsers = subparsers.add_parser("get_users", help="Returns an array with one or more SFTP users")
parserGetUsers.add_argument('--limit', type=int, default=100, help="default: %(default)s")
parserGetUsers.add_argument('--offset', type=int, default=0, help="default: %(default)s")
parserGetUsers.add_argument('--username', type=str, default="", help="default: %(default)s")
parserGetUsers.add_argument('--order', type=str, choices=['ASC', 'DESC'], default='ASC',
help="default: %(default)s")
parserGetUsers = subparsers.add_parser('get-users', help='Returns an array with one or more SFTP users')
parserGetUsers.add_argument('-L', '--limit', type=int, default=100, choices=range(1, 501),
help='Maximum allowed value is 500. Default: %(default)s', metavar='[1...500]')
parserGetUsers.add_argument('-O', '--offset', type=int, default=0, help='Default: %(default)s')
parserGetUsers.add_argument('-U', '--username', type=str, default='', help='Default: %(default)s')
parserGetUsers.add_argument('-S', '--order', type=str, choices=['ASC', 'DESC'], default='ASC',
help='default: %(default)s')
parserGetUserByID = subparsers.add_parser("get_user_by_id", help="Find user by ID")
parserGetUserByID = subparsers.add_parser('get-user-by-id', help='Find user by ID')
parserGetUserByID.add_argument('id', type=int)
parserGetSFTPConnections = subparsers.add_parser("get_sftp_connections", help="Get the active sftp users and info about their uploads/downloads")
parserGetSFTPConnections = subparsers.add_parser('get-sftp-connections',
help='Get the active sftp users and info about their uploads/downloads')
parserCloseSFTPConnection = subparsers.add_parser("close_sftp_connection", help="Terminate an active SFTP connection")
parserCloseSFTPConnection.add_argument("connectionID", type=str)
parserCloseSFTPConnection = subparsers.add_parser('close-sftp-connection', help='Terminate an active SFTP connection')
parserCloseSFTPConnection.add_argument('connectionID', type=str)
parserGetQuotaScans = subparsers.add_parser("get_quota_scans", help="Get the active quota scans")
parserGetQuotaScans = subparsers.add_parser('get-quota-scans', help='Get the active quota scans')
parserStartQuotaScans = subparsers.add_parser("start_quota_scan", help="Start a new quota scan")
parserStartQuotaScans = subparsers.add_parser('start-quota-scan', help='Start a new quota scan')
addCommonUserArguments(parserStartQuotaScans)
parserGetVersion = subparsers.add_parser("get_version", help="Get version details")
parserGetVersion = subparsers.add_parser('get-version', help='Get version details')
args = parser.parse_args()
api = SFTPGoApiRequests(args.debug, args.base_url, args.auth_type, args.auth_user, args.auth_password, args.verify)
api = SFTPGoApiRequests(args.debug, args.base_url, args.auth_type, args.auth_user, args.auth_password, args.secure)
if args.command == "add_user":
if args.command == 'add-user':
api.addUser(args.username, args.password, args.public_keys, args.home_dir,
args.uid, args.gid, args.max_sessions, args.quota_size, args.quota_files,
args.permissions, args.upload_bandwidth, args.download_bandwidth)
elif args.command == "update_user":
elif args.command == 'update-user':
api.updateUser(args.id, args.username, args.password, args.public_keys, args.home_dir,
args.uid, args.gid, args.max_sessions, args.quota_size, args.quota_files,
args.permissions, args.upload_bandwidth, args.download_bandwidth)
elif args.command == "delete_user":
elif args.command == 'delete-user':
api.deleteUser(args.id)
elif args.command == "get_users":
elif args.command == 'get-users':
api.getUsers(args.limit, args.offset, args.order, args.username)
elif args.command == "get_user_by_id":
elif args.command == 'get-user-by-id':
api.getUserByID(args.id)
elif args.command == "get_sftp_connections":
elif args.command == 'get-sftp-connections':
api.getSFTPConnections()
elif args.command == "close_sftp_connection":
elif args.command == 'close-sftp-connection':
api.closeSFTPConnection(args.connectionID)
elif args.command == "get_quota_scans":
elif args.command == 'get-quota-scans':
api.getQuotaScans()
elif args.command == "start_quota_scan":
elif args.command == 'start-quota-scan':
api.startQuotaScan(args.username)
elif args.command == "get_version":
elif args.command == 'get-version':
api.getVersion()