Skip to content

Commit

Permalink
Update pki.server.cli.password to use argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
edewata committed Dec 13, 2024
1 parent 079455b commit aa8a770
Showing 1 changed file with 85 additions and 94 deletions.
179 changes: 85 additions & 94 deletions base/server/python/pki/server/cli/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
# All rights reserved.
#

from __future__ import absolute_import
from __future__ import print_function
import getopt
import argparse
import logging
import sys

import pki.cli

Expand All @@ -46,6 +43,24 @@ class PasswordFindCLI(pki.cli.CLI):
def __init__(self):
super().__init__('find', 'Find passwords')

self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'-i',
'--instance',
default='pki-tomcat')
self.parser.add_argument(
'-v',
'--verbose',
action='store_true')
self.parser.add_argument(
'--debug',
action='store_true')
self.parser.add_argument(
'--help',
action='store_true')

def print_help(self):
print('Usage: pki-server password-find [OPTIONS]')
print()
Expand All @@ -57,36 +72,19 @@ def print_help(self):

def execute(self, argv):

try:
opts, _ = getopt.gnu_getopt(argv, 'i:v', [
'instance=', 'force',
'verbose', 'debug', 'help'])
args = self.parser.parse_args(args=argv)

except getopt.GetoptError as e:
print('ERROR: %s' % e)
if args.help:
self.print_help()
sys.exit(1)

instance_name = 'pki-tomcat'

for o, a in opts:
if o in ('-i', '--instance'):
instance_name = a
return

elif o in ('-v', '--verbose'):
logging.getLogger().setLevel(logging.INFO)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

elif o == '--debug':
logging.getLogger().setLevel(logging.DEBUG)
elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

elif o == '--help':
self.print_help()
sys.exit()

else:
print('ERROR: unknown option: %s' % o)
self.print_help()
sys.exit(1)
instance_name = args.instance

instance = pki.server.PKIServerFactory.create(instance_name)

Expand All @@ -112,6 +110,26 @@ class PasswordAddCLI(pki.cli.CLI):
def __init__(self):
super().__init__('add', 'Add password')

self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'-i',
'--instance',
default='pki-tomcat')
self.parser.add_argument('--password')
self.parser.add_argument(
'-v',
'--verbose',
action='store_true')
self.parser.add_argument(
'--debug',
action='store_true')
self.parser.add_argument(
'--help',
action='store_true')
self.parser.add_argument('name')

def print_help(self):
print('Usage: pki-server password-add [OPTIONS] <password ID>')
print()
Expand All @@ -124,46 +142,21 @@ def print_help(self):

def execute(self, argv):

try:
opts, args = getopt.gnu_getopt(argv, 'i:v', [
'instance=',
'password=',
'verbose', 'debug', 'help'])
args = self.parser.parse_args(args=argv)

except getopt.GetoptError as e:
print('ERROR: %s' % e)
if args.help:
self.print_help()
sys.exit(1)

instance_name = 'pki-tomcat'
password = None

for o, a in opts:
if o in ('-i', '--instance'):
instance_name = a

elif o == '--password':
password = a

elif o in ('-v', '--verbose'):
logging.getLogger().setLevel(logging.INFO)
return

elif o == '--debug':
logging.getLogger().setLevel(logging.DEBUG)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

elif o == '--help':
self.print_help()
sys.exit()
elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

else:
print('ERROR: Unknown option: %s' % o)
self.print_help()
sys.exit(1)

if len(args) < 1:
raise Exception('Missing password ID')

name = args[0]
instance_name = args.instance
password = args.password
name = args.name

instance = pki.server.PKIServerFactory.create(instance_name)

Expand All @@ -184,6 +177,25 @@ class PasswordRemoveCLI(pki.cli.CLI):
def __init__(self):
super().__init__('del', 'Remove password')

self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'-i',
'--instance',
default='pki-tomcat')
self.parser.add_argument(
'-v',
'--verbose',
action='store_true')
self.parser.add_argument(
'--debug',
action='store_true')
self.parser.add_argument(
'--help',
action='store_true')
self.parser.add_argument('name')

def print_help(self):
print('Usage: pki-server password-del [OPTIONS] <password ID>')
print()
Expand All @@ -195,41 +207,20 @@ def print_help(self):

def execute(self, argv):

try:
opts, args = getopt.gnu_getopt(argv, 'i:v', [
'instance=',
'verbose', 'debug', 'help'])
args = self.parser.parse_args(args=argv)

except getopt.GetoptError as e:
print('ERROR: %s' % e)
if args.help:
self.print_help()
sys.exit(1)

instance_name = 'pki-tomcat'
return

for o, a in opts:
if o in ('-i', '--instance'):
instance_name = a

elif o in ('-v', '--verbose'):
logging.getLogger().setLevel(logging.INFO)

elif o == '--debug':
logging.getLogger().setLevel(logging.DEBUG)

elif o == '--help':
self.print_help()
sys.exit()

else:
print('ERROR: Unknown option: %s' % o)
self.print_help()
sys.exit(1)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

if len(args) < 1:
raise Exception('Missing password ID')
elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

name = args[0]
instance_name = args.instance
name = args.name

instance = pki.server.PKIServerFactory.create(instance_name)

Expand Down

0 comments on commit aa8a770

Please sign in to comment.