Skip to content

Commit 5716fcd

Browse files
Add --debug option to metadata plugin (#592)
1 parent a3cf252 commit 5716fcd

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

linodecli/arg_helpers.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
import yaml
1212

1313
from linodecli import plugins
14-
15-
from .completion import bake_completions
16-
from .helpers import pagination_args_shared, register_args_shared
14+
from linodecli.completion import bake_completions
15+
from linodecli.helpers import (
16+
pagination_args_shared,
17+
register_args_shared,
18+
register_debug_arg,
19+
)
1720

1821

1922
def register_args(parser):
@@ -137,12 +140,10 @@ def register_args(parser):
137140
action="store_true",
138141
help="Prints version information and exits.",
139142
)
140-
parser.add_argument(
141-
"--debug", action="store_true", help="Enable verbose HTTP debug output."
142-
)
143143

144144
pagination_args_shared(parser)
145145
register_args_shared(parser)
146+
register_debug_arg(parser)
146147

147148
return parser
148149

linodecli/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def register_args_shared(parser: ArgumentParser):
9393
return parser
9494

9595

96+
def register_debug_arg(parser: ArgumentParser):
97+
"""
98+
Add the debug argument to the given
99+
ArgumentParser that may be shared across the CLI and plugins.
100+
"""
101+
parser.add_argument(
102+
"--debug", action="store_true", help="Enable verbose HTTP debug output."
103+
)
104+
105+
96106
def expand_globs(pattern: str):
97107
"""
98108
Expand glob pattern (for example, '/some/path/*.txt')

linodecli/plugins/metadata.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
linode-cli metadata [ENDPOINT]
77
"""
88

9-
import argparse
109
import sys
10+
from argparse import ArgumentParser
1111

1212
from linode_metadata import MetadataClient
1313
from linode_metadata.objects.error import ApiError
@@ -16,6 +16,8 @@
1616
from rich import print as rprint
1717
from rich.table import Table
1818

19+
from linodecli.helpers import register_debug_arg
20+
1921
PLUGIN_BASE = "linode-cli metadata"
2022

2123

@@ -156,7 +158,7 @@ def get_ssh_keys(client: MetadataClient):
156158
}
157159

158160

159-
def print_help(parser: argparse.ArgumentParser):
161+
def print_help(parser: ArgumentParser):
160162
"""
161163
Print out the help info to the standard output
162164
"""
@@ -181,7 +183,9 @@ def get_metadata_parser():
181183
"""
182184
Builds argparser for Metadata plug-in
183185
"""
184-
parser = argparse.ArgumentParser(PLUGIN_BASE, add_help=False)
186+
parser = ArgumentParser(PLUGIN_BASE, add_help=False)
187+
188+
register_debug_arg(parser)
185189

186190
parser.add_argument(
187191
"endpoint",
@@ -209,7 +213,9 @@ def call(args, context):
209213
# make a client, but only if we weren't printing help and endpoint is valid
210214
if "--help" not in args:
211215
try:
212-
client = MetadataClient(user_agent=context.client.user_agent)
216+
client = MetadataClient(
217+
user_agent=context.client.user_agent, debug=parsed.debug
218+
)
213219
except ConnectTimeout as exc:
214220
raise ConnectionError(
215221
"Can't access Metadata service. Please verify that you are inside a Linode."

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ PyYAML
44
packaging
55
rich
66
urllib3<3
7-
linode-metadata
7+
linode-metadata>=0.3.0

tests/unit/test_plugin_metadata.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pytest import CaptureFixture
88

99
from linodecli.plugins.metadata import (
10+
get_metadata_parser,
1011
print_instance_table,
1112
print_networking_tables,
1213
print_ssh_keys_table,
@@ -129,3 +130,12 @@ def test_empty_ssh_key_table(capsys: CaptureFixture):
129130

130131
assert "user" in captured_text.out
131132
assert "ssh key" in captured_text.out
133+
134+
135+
def test_arg_parser():
136+
parser = get_metadata_parser()
137+
parsed, args = parser.parse_known_args(["--debug"])
138+
assert parsed.debug
139+
140+
parsed, args = parser.parse_known_args(["--something-else"])
141+
assert not parsed.debug

0 commit comments

Comments
 (0)