Skip to content

Commit 0d4f59c

Browse files
authored
Added enum for exit codes (#622)
1 parent 816e218 commit 0d4f59c

25 files changed

+154
-108
lines changed

linodecli/__init__.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from rich.table import Column, Table
1414

1515
from linodecli import plugins
16+
from linodecli.exit_codes import ExitCodes
1617

1718
from .arg_helpers import (
1819
bake_command,
@@ -85,7 +86,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
8586
# print version info and exit - but only if no command was given
8687
print(f"linode-cli {VERSION}")
8788
print(f"Built from spec version {cli.spec_version}")
88-
sys.exit(0)
89+
sys.exit(ExitCodes.SUCCESS)
8990
else:
9091
# something else might want to parse version
9192
# find where it was originally, as it was removed from args
@@ -96,50 +97,50 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
9697
if parsed.command == "bake":
9798
if parsed.action is None:
9899
print("No spec provided, cannot bake")
99-
sys.exit(9)
100+
sys.exit(ExitCodes.ARGUMENT_ERROR)
100101
bake_command(cli, parsed.action)
101-
sys.exit(0)
102+
sys.exit(ExitCodes.SUCCESS)
102103
elif cli.ops is None:
103104
# if not spec was found and we weren't baking, we're doomed
104-
sys.exit(3)
105+
sys.exit(ExitCodes.ARGUMENT_ERROR)
105106

106107
if parsed.command == "register-plugin":
107108
if parsed.action is None:
108109
print("register-plugin requires a module name!")
109-
sys.exit(9)
110+
sys.exit(ExitCodes.ARGUMENT_ERROR)
110111
msg, code = register_plugin(parsed.action, cli.config, cli.ops)
111112
print(msg)
112113
sys.exit(code)
113114

114115
if parsed.command == "remove-plugin":
115116
if parsed.action is None:
116117
print("remove-plugin requires a plugin name to remove!")
117-
sys.exit(9)
118+
sys.exit(ExitCodes.ARGUMENT_ERROR)
118119
msg, code = remove_plugin(parsed.action, cli.config)
119120
print(msg)
120121
sys.exit(code)
121122

122123
if parsed.command == "completion":
123124
print(get_completions(cli.ops, parsed.help, parsed.action))
124-
sys.exit(0)
125+
sys.exit(ExitCodes.SUCCESS)
125126

126127
# handle a help for the CLI
127128
if parsed.command is None or (parsed.command is None and parsed.help):
128129
parser.print_help()
129130
print_help_default()
130-
sys.exit(0)
131+
sys.exit(ExitCodes.SUCCESS)
131132

132133
if parsed.command == "env-vars":
133134
print_help_env_vars()
134-
sys.exit(0)
135+
sys.exit(ExitCodes.SUCCESS)
135136

136137
if parsed.command == "commands":
137138
print_help_commands(cli.ops)
138-
sys.exit(0)
139+
sys.exit(ExitCodes.SUCCESS)
139140

140141
if parsed.command == "plugins":
141142
print_help_plugins(cli.config)
142-
sys.exit(0)
143+
sys.exit(ExitCodes.SUCCESS)
143144

144145
# configure
145146
if parsed.command == "configure":
@@ -151,7 +152,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
151152
)
152153
else:
153154
cli.configure()
154-
sys.exit(0)
155+
sys.exit(ExitCodes.SUCCESS)
155156

156157
# block of commands for user-focused operations
157158
if parsed.command == "set-user":
@@ -163,7 +164,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
163164
)
164165
else:
165166
cli.config.set_default_user(parsed.action)
166-
sys.exit(0)
167+
sys.exit(ExitCodes.SUCCESS)
167168

168169
if parsed.command == "show-users":
169170
if parsed.help:
@@ -177,7 +178,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
177178
)
178179
else:
179180
cli.config.print_users()
180-
sys.exit(0)
181+
sys.exit(ExitCodes.SUCCESS)
181182

182183
if parsed.command == "remove-user":
183184
if parsed.help or not parsed.action:
@@ -190,7 +191,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
190191
)
191192
else:
192193
cli.config.remove_user(parsed.action)
193-
sys.exit(0)
194+
sys.exit(ExitCodes.SUCCESS)
194195

195196
# check for plugin invocation
196197
if parsed.command not in cli.ops and parsed.command in plugins.available(
@@ -202,7 +203,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
202203
plugin_args = argv[1:] # don't include the program name
203204
plugin_args.remove(parsed.command) # don't include the plugin name
204205
plugins.invoke(parsed.command, plugin_args, context)
205-
sys.exit(0)
206+
sys.exit(ExitCodes.SUCCESS)
206207

207208
# unknown commands
208209
if (
@@ -211,7 +212,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
211212
and parsed.command not in HELP_TOPICS
212213
):
213214
print(f"Unrecognized command {parsed.command}")
214-
sys.exit(1)
215+
sys.exit(ExitCodes.UNRECOGNIZED_COMMAND)
215216

216217
# handle a help for a command - either --help or no action triggers this
217218
if (
@@ -236,10 +237,10 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
236237
table.add_row(*row)
237238

238239
rprint(table)
239-
sys.exit(0)
240+
sys.exit(ExitCodes.SUCCESS)
240241

241242
if parsed.command is not None and parsed.action is not None:
242243
if parsed.help:
243244
print_help_action(cli, parsed.command, parsed.action)
244-
sys.exit(0)
245+
sys.exit(ExitCodes.SUCCESS)
245246
cli.handle_command(parsed.command, parsed.action, args)

linodecli/api_request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from packaging import version
1313
from requests import Response
1414

15+
from linodecli.exit_codes import ExitCodes
1516
from linodecli.helpers import API_CA_PATH
1617

1718
from .baked.operation import (
@@ -394,7 +395,7 @@ def _handle_error(ctx, response):
394395
title="errors",
395396
to=sys.stderr,
396397
)
397-
sys.exit(1)
398+
sys.exit(ExitCodes.REQUEST_FAILED)
398399

399400

400401
def _check_retry(response):

linodecli/arg_helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import yaml
1212

1313
from linodecli import plugins
14+
from linodecli.exit_codes import ExitCodes
1415
from linodecli.helpers import (
1516
register_args_shared,
1617
register_debug_arg,
@@ -183,6 +184,6 @@ def bake_command(cli, spec_loc):
183184
raise RuntimeError(f"Request failed to {spec_loc}")
184185
except Exception as e:
185186
print(f"Could not load spec: {e}")
186-
sys.exit(2)
187+
sys.exit(ExitCodes.REQUEST_FAILED)
187188

188189
cli.bake(spec)

linodecli/baked/operation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from linodecli.baked.request import OpenAPIFilteringRequest, OpenAPIRequest
2020
from linodecli.baked.response import OpenAPIResponse
21+
from linodecli.exit_codes import ExitCodes
2122
from linodecli.output.output_handler import OutputHandler
2223
from linodecli.overrides import OUTPUT_OVERRIDES
2324

@@ -586,7 +587,7 @@ def _validate_parent_child_conflicts(self, parsed: argparse.Namespace):
586587
file=sys.stderr,
587588
)
588589

589-
sys.exit(2)
590+
sys.exit(ExitCodes.ARGUMENT_ERROR)
590591

591592
@staticmethod
592593
def _handle_list_items(

linodecli/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from linodecli.api_request import do_request, get_all_pages
1313
from linodecli.baked import OpenAPIOperation
1414
from linodecli.configuration import CLIConfig
15+
from linodecli.exit_codes import ExitCodes
1516
from linodecli.output.output_handler import OutputHandler, OutputMode
1617

1718
METHODS = ("get", "post", "put", "delete")
@@ -123,7 +124,7 @@ def handle_command(self, command, action, args):
123124
operation = self.find_operation(command, action)
124125
except ValueError as e:
125126
print(e, file=sys.stderr)
126-
sys.exit(1)
127+
sys.exit(ExitCodes.REQUEST_FAILED)
127128

128129
if not self.pagination:
129130
result = get_all_pages(self, operation, args)

linodecli/configuration/auth.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import requests
1414

15+
from linodecli.exit_codes import ExitCodes
1516
from linodecli.helpers import API_CA_PATH
1617

1718
TOKEN_GENERATION_URL = "https://cloud.linode.com/profile/tokens"
@@ -59,7 +60,7 @@ def _handle_response_status(
5960

6061
print(f"Could not contact {response.url} - Error: {response.status_code}")
6162
if exit_on_error:
62-
sys.exit(4)
63+
sys.exit(ExitCodes.REQUEST_FAILED)
6364

6465

6566
# TODO: merge config do_request and cli do_request
@@ -243,7 +244,7 @@ def _get_token_web(base_url: str) -> Tuple[str, str]:
243244

244245
if username is None:
245246
print("OAuth failed. Please try again of use a token for auth.")
246-
sys.exit(1)
247+
sys.exit(ExitCodes.OAUTH_ERROR)
247248

248249
# the token returned via public oauth will expire in 2 hours, which
249250
# isn't great. Instead, we're gonna make a token that expires never
@@ -345,6 +346,6 @@ def log_message(self, form, *args): # pylint: disable=arguments-differ
345346
"try token using a token by invoking with `linode-cli configure --token`, "
346347
"and open an issue at https://github.com/linode/linode-cli"
347348
)
348-
sys.exit(1)
349+
sys.exit(ExitCodes.OAUTH_ERROR)
349350

350351
return serv.token

linodecli/configuration/config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import sys
88
from typing import Any, Dict, List, Optional
99

10+
from linodecli.exit_codes import ExitCodes
11+
1012
from .auth import (
1113
_check_full_access,
1214
_do_get_request,
@@ -95,7 +97,7 @@ def set_user(self, username: str):
9597
"""
9698
if not self.config.has_section(username):
9799
print(f"User {username} is not configured!")
98-
sys.exit(1)
100+
sys.exit(ExitCodes.USERNAME_ERROR)
99101

100102
self.username = username
101103

@@ -112,7 +114,7 @@ def remove_user(self, username: str):
112114
f"Cannot remove {username} as they are the default user! You can "
113115
"change the default user with: `linode-cli set-user USERNAME`"
114116
)
115-
sys.exit(1)
117+
sys.exit(ExitCodes.USERNAME_ERROR)
116118

117119
if self.config.has_section(username):
118120
self.config.remove_section(username)
@@ -129,15 +131,15 @@ def print_users(self):
129131
if sec != "DEFAULT":
130132
print(f'{"*" if sec == default_user else " "} {sec}')
131133

132-
sys.exit(0)
134+
sys.exit(ExitCodes.SUCCESS)
133135

134136
def set_default_user(self, username: str):
135137
"""
136138
Sets the default user. If that user isn't in the config, exits with error
137139
"""
138140
if not self.config.has_section(username):
139141
print(f"User {username} is not configured!")
140-
sys.exit(1)
142+
sys.exit(ExitCodes.USERNAME_ERROR)
141143

142144
self.config.set("DEFAULT", "default-user", username)
143145
self.write_config()
@@ -263,7 +265,7 @@ def update(
263265
ENV_TOKEN_NAME, None
264266
):
265267
print(f"User {username} is not configured.")
266-
sys.exit(1)
268+
sys.exit(ExitCodes.USERNAME_ERROR)
267269
if not self.config.has_section(username) or allowed_defaults is None:
268270
return namespace
269271

linodecli/exit_codes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
This is an enumeration of the various exit codes in Linode CLI
3+
4+
"""
5+
6+
from enum import IntEnum
7+
8+
9+
class ExitCodes(IntEnum):
10+
"""
11+
An enumeration of the various exit codes in Linode CLI
12+
"""
13+
14+
SUCCESS = 0
15+
UNRECOGNIZED_COMMAND = 1
16+
REQUEST_FAILED = 2
17+
OAUTH_ERROR = 3
18+
USERNAME_ERROR = 4
19+
FIREWALL_ERROR = 5
20+
KUBECONFIG_ERROR = 6
21+
ARGUMENT_ERROR = 7
22+
FILE_ERROR = 8

linodecli/plugins/firewall-editor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from rich import print as rprint
1515
from rich.table import Table
1616

17+
from linodecli.exit_codes import ExitCodes
1718
from linodecli.plugins import inherit_plugin_args
1819

1920
BOLD = "\033[1m"
@@ -204,15 +205,15 @@ def _get_firewall(firewall_id, client):
204205

205206
if code != 200:
206207
print(f"Error retrieving firewall: {code}")
207-
sys.exit(1)
208+
sys.exit(ExitCodes.FIREWALL_ERROR)
208209

209210
code, rules = client.call_operation(
210211
"firewalls", "rules-list", args=[firewall_id]
211212
)
212213

213214
if code != 200:
214215
print(f"Error retrieving firewall rules: {code}")
215-
sys.exit(2)
216+
sys.exit(ExitCodes.FIREWALL_ERROR)
216217

217218
return firewall, rules
218219

0 commit comments

Comments
 (0)