Skip to content

Commit e9e36ad

Browse files
authored
Merge pull request #80 from csomh/use-subparsers
Use argparse subparsers for subcommands
2 parents b5a957d + 390d349 commit e9e36ad

File tree

1 file changed

+102
-85
lines changed

1 file changed

+102
-85
lines changed

operatorcourier/cli.py

Lines changed: 102 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -28,110 +28,127 @@ def parse(self):
2828
"""
2929
parser = argparse.ArgumentParser(
3030
description='Build, verify and push operator bundles into '
31-
'external app registry',
32-
usage='''operator-courier <command> [<args>]
33-
34-
These are the commands you can use:
35-
verify Create a bundle and test it for correctness.
36-
push Create a bundle, test it, and push it to an app registry.
37-
nest Take a flat to-be-bundled directory and version nest it.
38-
flatten Create a flat directory from versioned operator bundle yaml files.
39-
''')
31+
'external app registry')
32+
4033
try:
4134
__version__ = pkg_resources.get_distribution('operator-courier').version
4235
except Exception:
4336
__version__ = 'unknown'
4437

45-
parser.add_argument('command', help='Subcommand to run')
46-
parser.add_argument('-v', '--version',
47-
help='Show the current version of operator-courier',
48-
action='version', version=__version__)
49-
50-
args = parser.parse_args(sys.argv[1:2])
51-
if not hasattr(self, args.command):
52-
parser.error('Unrecognized command')
38+
parser.add_argument(
39+
'-v', '--version',
40+
help='Show the current version of operator-courier',
41+
action='version', version=__version__)
5342

54-
getattr(self, args.command)()
43+
subparsers = parser.add_subparsers(title='subcommands')
5544

56-
# Parse the verify command
57-
def verify(self):
58-
parser = argparse.ArgumentParser(
45+
verify_parser = subparsers.add_parser(
46+
'verify',
47+
help='Create a bundle and test it for correctness.',
5948
description='Build and verify an operator bundle to test')
60-
parser.add_argument('source_dir', help='Path of your directory of yaml '
61-
'files to bundle. Either set this or '
62-
'use the files argument for bundle data.')
63-
parser.add_argument('--ui_validate_io',
64-
help='Validate bundle for operatorhub.io UI. '
65-
'To visually confirm that your operator '
66-
'will be displayed correctly, please visit '
67-
'https://operatorhub.io/preview and paste '
68-
'your operator CSV.',
69-
action='store_true')
70-
parser.add_argument('--validation-output', dest='validation_output',
71-
help='A file to write validation warnings and errors to'
72-
'in JSON format')
73-
args, leftovers = parser.parse_known_args(sys.argv[2:])
49+
verify_parser.add_argument(
50+
'source_dir',
51+
help='Path of your directory of yaml files to bundle. '
52+
'Either set this or use the files argument for bundle data.')
53+
verify_parser.add_argument(
54+
'--ui_validate_io',
55+
help='Validate bundle for operatorhub.io UI. '
56+
'To visually confirm that your operator will be displayed correctly, '
57+
'please visit https://operatorhub.io/preview and paste '
58+
'your operator CSV.',
59+
action='store_true')
60+
verify_parser.add_argument(
61+
'--validation-output',
62+
dest='validation_output',
63+
help='A file to write validation warnings and errors to in JSON format')
64+
verify_parser.set_defaults(func=self.verify)
65+
66+
push_parser = subparsers.add_parser(
67+
'push',
68+
help='Create a bundle, test it, and push it to an app registry.',
69+
description='Build, verify and push an operator bundle '
70+
'into external app registry.')
71+
push_parser.add_argument(
72+
'source_dir',
73+
help='Path of your directory of yaml files to bundle.')
74+
push_parser.add_argument(
75+
'namespace',
76+
help='Name of the Quay namespace to push operator to.')
77+
push_parser.add_argument(
78+
'repository',
79+
help='Application repository name the application is bundled for.')
80+
push_parser.add_argument(
81+
'release',
82+
help='The release version of the bundle.')
83+
push_parser.add_argument(
84+
'token',
85+
help='Authorization token for Quay api.')
86+
push_parser.add_argument(
87+
'--validation-output',
88+
dest='validation_output',
89+
help='A file to write validation warnings and errors to in JSON format')
90+
push_parser.set_defaults(func=self.push)
91+
92+
nest_parser = subparsers.add_parser(
93+
'nest',
94+
help='Take a flat to-be-bundled directory and version nest it.',
95+
description='Take a flat bundle directory and version nest it '
96+
'to eventually create an operator-registry image.')
97+
nest_parser.add_argument(
98+
'source_dir',
99+
help='Path of your directory of yaml files to bundle.')
100+
nest_parser.add_argument(
101+
'registry_dir',
102+
help='Path of your directory to be populated. '
103+
'If directory does not exist, it will be created.')
104+
nest_parser.set_defaults(func=self.nest)
105+
106+
flatten_parser = subparsers.add_parser(
107+
'flatten',
108+
help='Create a flat directory from versioned operator bundle yaml files.',
109+
description='Given a directory with different versions of '
110+
'operator bundles (CRD, CSV, package), this command extracts '
111+
'versioned CSVs and the latest version of each CRD along with '
112+
'the package file and creates a new flat directory '
113+
'of yaml files. See https://github.com/operator-framework/'
114+
'operator-registry#manifest-format to find out more about '
115+
'how nested bundles should be structured.')
116+
flatten_parser.add_argument(
117+
'source_dir',
118+
help='Path of the source directory that contains different '
119+
'versions of operator bundles (CRD, CSV, package)')
120+
flatten_parser.add_argument(
121+
'dest_dir',
122+
help='The new flat directory that contains '
123+
'extracted bundle files')
124+
flatten_parser.set_defaults(func=self.flatten)
125+
126+
args = parser.parse_args()
127+
args.func(args)
128+
129+
def verify(self, args):
130+
"""Run the verify command
131+
"""
74132
api.build_and_verify(source_dir=args.source_dir,
75133
ui_validate_io=args.ui_validate_io,
76134
validation_output=args.validation_output)
77135

78-
# Parse the push command
79-
def push(self):
80-
parser = argparse.ArgumentParser(
81-
description='Build, verify and push an operator bundle '
82-
'into external app registry.')
83-
parser.add_argument('source_dir',
84-
help='Path of your directory of yaml files to bundle.')
85-
parser.add_argument('namespace',
86-
help='Name of the Quay namespace to push operator to.')
87-
parser.add_argument('repository',
88-
help='Application repository name '
89-
'the application is bundled for.')
90-
parser.add_argument('release',
91-
help='The release version of the bundle.')
92-
parser.add_argument('token', help='Authorization token for Quay api.')
93-
parser.add_argument('--validation-output', dest='validation_output',
94-
help='A file to write validation warnings and errors to'
95-
'in JSON format')
96-
args, leftovers = parser.parse_known_args(sys.argv[2:])
136+
def push(self, args):
137+
"""Run the push command
138+
"""
97139
api.build_verify_and_push(args.namespace,
98140
args.repository,
99141
args.release,
100142
args.token,
101143
source_dir=args.source_dir,
102144
validation_output=args.validation_output)
103145

104-
# Parse the nest command
105-
def nest(self):
106-
parser = argparse.ArgumentParser(
107-
description='Take a flat bundle directory and version nest it '
108-
'to eventually create an operator-registry image.')
109-
parser.add_argument('source_dir',
110-
help='Path of your directory of yaml files to bundle.')
111-
parser.add_argument('registry_dir',
112-
help='Path of your directory to be populated. '
113-
'If directory does not exist, it will be created.')
114-
115-
args, leftovers = parser.parse_known_args(sys.argv[2:])
146+
def nest(self, args):
147+
"""Run the nest command
148+
"""
116149
api.nest(args.source_dir, args.registry_dir)
117150

118-
# Parse the flatten command
119-
def flatten(self):
120-
parser = argparse.ArgumentParser(
121-
usage='operator-courier flatten [-h] source_dir dest_dir',
122-
description='Given a directory with different versions of '
123-
'operator bundles (CRD, CSV, package), this command extracts '
124-
'versioned CSVs and the latest version of each CRD along with '
125-
'the package file and creates a new flat directory '
126-
'of yaml files. See https://github.com/operator-framework/'
127-
'operator-registry#manifest-format to find out more about '
128-
'how nested bundles should be structured.')
129-
parser.add_argument('source_dir',
130-
help='Path of the source directory that contains different '
131-
'versions of operator bundles (CRD, CSV, package)')
132-
parser.add_argument('dest_dir',
133-
help='The new flat directory that contains '
134-
'extracted bundle files')
135-
136-
args, leftovers = parser.parse_known_args(sys.argv[2:])
151+
def flatten(self, args):
152+
"""Parse the flatten command
153+
"""
137154
api.flatten(args.source_dir, args.dest_dir)

0 commit comments

Comments
 (0)