Skip to content

Commit

Permalink
Add kubectl-ng config current-context (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored May 16, 2024
1 parent 41fac23 commit 450c89b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
20 changes: 20 additions & 0 deletions examples/kubectl-ng/kubectl_ng/_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-FileCopyrightText: Copyright (c) 2024, Kr8s Developers (See LICENSE for list)
# SPDX-License-Identifier: BSD 3-Clause License
import typer

import kr8s

config = typer.Typer(
no_args_is_help=True,
name="config",
help="Modify kubeconfig files.",
)


@config.command(name="current-context", help="Display the current-context")
def config_current_context():
"""Display the current context."""
try:
typer.echo(kr8s.api().auth.kubeconfig.current_context)
except KeyError:
typer.echo("error: current-context is not set")
12 changes: 9 additions & 3 deletions examples/kubectl-ng/kubectl_ng/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ._api_resources import api_resources
from ._api_versions import api_versions
from ._config import config
from ._create import create
from ._delete import delete
from ._exec import kexec
Expand All @@ -26,10 +27,14 @@ def wrapper(*args, **kwargs):
def register(app, func, alias=None):
if asyncio.iscoroutinefunction(func):
func = _typer_async(func)
if alias is not None:
app.command(alias)(func)
if isinstance(func, typer.Typer):
assert alias, "Typer subcommand must have an alias."
app.add_typer(func, name=alias)
else:
app.command()(func)
if alias is not None:
app.command(alias)(func)
else:
app.command()(func)


app = typer.Typer(no_args_is_help=True)
Expand All @@ -41,6 +46,7 @@ def register(app, func, alias=None):
register(app, version)
register(app, wait)
register(app, kexec, "exec")
register(app, config, "config")


def go():
Expand Down
15 changes: 15 additions & 0 deletions examples/kubectl-ng/kubectl_ng/tests/test_kng_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-FileCopyrightText: Copyright (c) 2024, Kr8s Developers (See LICENSE for list)
# SPDX-License-Identifier: BSD 3-Clause License
from kubectl_ng.cli import app
from typer.testing import CliRunner

import kr8s

runner = CliRunner()


def test_create_and_delete():
current_context = kr8s.api().auth.kubeconfig.current_context
result = runner.invoke(app, ["config", "current-context"])
assert result.exit_code == 0
assert current_context in result.stdout

0 comments on commit 450c89b

Please sign in to comment.