Skip to content

Commit

Permalink
Update pki.server.cli.est to use argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
edewata committed Dec 19, 2024
1 parent 14f5650 commit 8681d94
Showing 1 changed file with 71 additions and 72 deletions.
143 changes: 71 additions & 72 deletions base/server/python/pki/server/cli/est.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
# SPDX-License-Identifier: GPL-2.0-or-later


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

import pki.cli
import pki.server
Expand Down Expand Up @@ -36,6 +33,31 @@ class ESTCreateCLI(pki.cli.CLI):
def __init__(self):
super().__init__('create', 'Create EST subsystem')

self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'-i',
'--instance',
default=DEFAULT_INSTANCE_NAME)
self.parser.add_argument(
'--force',
action='store_true')
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',
nargs='?',
default=DEFAULT_SUBSYSTEM_NAME)

def print_help(self):
print('Usage: pki-server est-create [OPTIONS] [name]')
print()
Expand All @@ -48,45 +70,21 @@ def print_help(self):

def execute(self, argv):

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

except getopt.GetoptError as e:
logger.error(e)
if args.help:
self.print_help()
sys.exit(1)

name = DEFAULT_SUBSYSTEM_NAME
instance_name = DEFAULT_INSTANCE_NAME
force = False

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

elif o == '--force':
force = True

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

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

elif o == '--help':
self.print_help()
sys.exit()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

else:
logger.error('Unknown option: %s', o)
self.print_help()
sys.exit(1)
elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

if len(args) > 0:
name = args[0]
instance_name = args.instance
name = args.name
force = args.force

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

Expand All @@ -104,6 +102,31 @@ class ESTRemoveCLI(pki.cli.CLI):
def __init__(self):
super().__init__('remove', 'Remove EST subsystem')

self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'-i',
'--instance',
default=DEFAULT_INSTANCE_NAME)
self.parser.add_argument(
'--force',
action='store_true')
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',
nargs='?',
default=DEFAULT_SUBSYSTEM_NAME)

def print_help(self):
print('Usage: pki-server est-remove [OPTIONS] [name]')
print()
Expand All @@ -116,45 +139,21 @@ def print_help(self):

def execute(self, argv):

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

except getopt.GetoptError as e:
logger.error(e)
if args.help:
self.print_help()
sys.exit(1)

name = DEFAULT_SUBSYSTEM_NAME
instance_name = DEFAULT_INSTANCE_NAME
force = False

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

elif o == '--force':
force = True

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

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

elif o == '--help':
self.print_help()
sys.exit()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

else:
logger.error('Unknown option: %s', o)
self.print_help()
sys.exit(1)
elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

if len(args) > 0:
name = args[0]
instance_name = args.instance
name = args.name
force = args.force

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

Expand Down

0 comments on commit 8681d94

Please sign in to comment.