From 6eae8b4607287deda5ca3bdd5f40f9a70be50e84 Mon Sep 17 00:00:00 2001 From: Arash Ghoreyshi Date: Tue, 18 Jun 2013 15:10:54 -0500 Subject: [PATCH] In keep, make environment variables the defaults of several args --- keep | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/keep b/keep index 6d66c532..577e8394 100755 --- a/keep +++ b/keep @@ -1,6 +1,7 @@ #!/usr/bin/env python import argparse +import os from barbicanclient import client @@ -23,15 +24,23 @@ class Keep: choices=["order", "secret"], help="type to operate on") parser.add_argument('--auth_endpoint', '-A', - help='the URL to authenticate against') - parser.add_argument('--user', '-U', help='the user to authenticate as') - parser.add_argument('--password', '-P', - help='the API key or password to ' - 'authenticate with') - parser.add_argument('--tenant', '-T', help='the tenant ID') - parser.add_argument('--endpoint', '-E', - help='the URL of the barbican server') - parser.add_argument('--token', '-K', help='the authentication token') + default=env('OS_AUTH_URL'), + help='the URL to authenticate against (default: ' + '%(default)s)') + parser.add_argument('--user', '-U', default=env('OS_USERNAME'), + help='the user to authenticate as (default: %(de' + 'fault)s)') + parser.add_argument('--password', '-P', default=env('OS_PASSWORD'), + help='the API key or password to authenticate with' + ' (default: %(default)s)') + parser.add_argument('--tenant', '-T', default=env('OS_TENANT_NAME'), + help='the tenant ID (default: %(default)s)') + parser.add_argument('--endpoint', '-E', default=env('SERVICE_ENDPOINT') + , help='the URL of the barbican server (default: %' + '(default)s)') + parser.add_argument('--token', '-K', default=env('SERVICE_TOKEN'), + help='the authentication token (default: %(default' + ')s)') return parser def add_create_args(self): @@ -152,6 +161,20 @@ class Keep: args.func(args) +def env(*vars, **kwargs): + """Search for the first defined of possibly many env vars + + Returns the first environment variable defined in vars, or + returns the default defined in kwargs. + + """ + for v in vars: + value = os.environ.get(v, None) + if value: + return value + return kwargs.get('default', '') + + def main(): k = Keep() k.execute()