Skip to content

Commit

Permalink
Add --debug option to metadata plugin (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
zliang-akamai authored Apr 8, 2024
1 parent a3cf252 commit 5716fcd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
13 changes: 7 additions & 6 deletions linodecli/arg_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import yaml

from linodecli import plugins

from .completion import bake_completions
from .helpers import pagination_args_shared, register_args_shared
from linodecli.completion import bake_completions
from linodecli.helpers import (
pagination_args_shared,
register_args_shared,
register_debug_arg,
)


def register_args(parser):
Expand Down Expand Up @@ -137,12 +140,10 @@ def register_args(parser):
action="store_true",
help="Prints version information and exits.",
)
parser.add_argument(
"--debug", action="store_true", help="Enable verbose HTTP debug output."
)

pagination_args_shared(parser)
register_args_shared(parser)
register_debug_arg(parser)

return parser

Expand Down
10 changes: 10 additions & 0 deletions linodecli/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ def register_args_shared(parser: ArgumentParser):
return parser


def register_debug_arg(parser: ArgumentParser):
"""
Add the debug argument to the given
ArgumentParser that may be shared across the CLI and plugins.
"""
parser.add_argument(
"--debug", action="store_true", help="Enable verbose HTTP debug output."
)


def expand_globs(pattern: str):
"""
Expand glob pattern (for example, '/some/path/*.txt')
Expand Down
14 changes: 10 additions & 4 deletions linodecli/plugins/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
linode-cli metadata [ENDPOINT]
"""

import argparse
import sys
from argparse import ArgumentParser

from linode_metadata import MetadataClient
from linode_metadata.objects.error import ApiError
Expand All @@ -16,6 +16,8 @@
from rich import print as rprint
from rich.table import Table

from linodecli.helpers import register_debug_arg

PLUGIN_BASE = "linode-cli metadata"


Expand Down Expand Up @@ -156,7 +158,7 @@ def get_ssh_keys(client: MetadataClient):
}


def print_help(parser: argparse.ArgumentParser):
def print_help(parser: ArgumentParser):
"""
Print out the help info to the standard output
"""
Expand All @@ -181,7 +183,9 @@ def get_metadata_parser():
"""
Builds argparser for Metadata plug-in
"""
parser = argparse.ArgumentParser(PLUGIN_BASE, add_help=False)
parser = ArgumentParser(PLUGIN_BASE, add_help=False)

register_debug_arg(parser)

parser.add_argument(
"endpoint",
Expand Down Expand Up @@ -209,7 +213,9 @@ def call(args, context):
# make a client, but only if we weren't printing help and endpoint is valid
if "--help" not in args:
try:
client = MetadataClient(user_agent=context.client.user_agent)
client = MetadataClient(
user_agent=context.client.user_agent, debug=parsed.debug
)
except ConnectTimeout as exc:
raise ConnectionError(
"Can't access Metadata service. Please verify that you are inside a Linode."
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ PyYAML
packaging
rich
urllib3<3
linode-metadata
linode-metadata>=0.3.0
10 changes: 10 additions & 0 deletions tests/unit/test_plugin_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pytest import CaptureFixture

from linodecli.plugins.metadata import (
get_metadata_parser,
print_instance_table,
print_networking_tables,
print_ssh_keys_table,
Expand Down Expand Up @@ -129,3 +130,12 @@ def test_empty_ssh_key_table(capsys: CaptureFixture):

assert "user" in captured_text.out
assert "ssh key" in captured_text.out


def test_arg_parser():
parser = get_metadata_parser()
parsed, args = parser.parse_known_args(["--debug"])
assert parsed.debug

parsed, args = parser.parse_known_args(["--something-else"])
assert not parsed.debug

0 comments on commit 5716fcd

Please sign in to comment.