Skip to content

CLI Examples

Embedding the plauth Command in Another click Program

It is possible to embed the plauth command into other programs to present a unified experience that leverages the Planet Auth Library package for client authentication plumbing. This is done by using a special version of the command that is configured for embedding.

When using the embedded version of the command, the outer application must take on the responsibility of instantiating the auth context and handling command line options so that this context may be available to click commands that are outside the plauth root command.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import click
import logging
import requests

import planet_auth_utils

logging.basicConfig(level=logging.DEBUG)

# TODO: This example does not show how the application may use built-in profile
#     dependency injection to provide a smooth user experience when using an
#     OAuth sevice account that is defined by a client ID and client secret.


@click.group(help="my cli main help message")
@planet_auth_utils.opt_profile
@planet_auth_utils.opt_client_id()
@planet_auth_utils.opt_client_secret()
@click.pass_context
def my_cli_main(ctx, auth_profile, auth_client_id, auth_client_secret):
    ctx.ensure_object(dict)
    ctx.obj["AUTH"] = planet_auth_utils.PlanetAuthFactory.initialize_auth_client_context(
        auth_profile_opt=auth_profile,
        auth_client_id_opt=auth_client_id,
        auth_client_secret_opt=auth_client_secret,
    )


@my_cli_main.command("cmd1", help="cmd1 help message")
@click.pass_context
def do_cmd1(ctx):
    auth_ctx = ctx.obj["AUTH"]
    print("Doing cmd1 with the auth profile '{}'".format(auth_ctx.profile_name()))
    result = requests.get(
        url="http://localhost:5001/",  # flask example service.
        auth=auth_ctx.request_authenticator(),
        timeout=30,
    )
    print("Status: {}".format(result.status_code))
    print("Payload:\n{}".format(result.text))


@my_cli_main.command("cmd2", help="cmd2 help message")
@click.pass_context
def do_cmd2(ctx):
    auth_ctx = ctx.obj["AUTH"]
    print("Doing cmd2 with the auth profile '{}'".format(auth_ctx.profile_name()))
    result = requests.get(
        url="http://localhost:5001/secret",  # flask example service.
        auth=auth_ctx.request_authenticator(),
        timeout=30,
    )
    print("Status: {}".format(result.status_code))
    print("Payload:\n{}".format(result.text))


my_cli_main.add_command(planet_auth_utils.cmd_plauth_embedded)

if __name__ == "__main__":
    my_cli_main()  # pylint: disable=E1120

Advanced Embedding

Beyond simple embedding, it is possible for an application to customize some the appearance and behavior of the command.

For example, an application may rename commands or hide options and sub-commands it does not wish to expose to the user. For an extensive example of this in a downstream application, you can look at the Planet SDK's CLI program, which both embeds the whole plauth command as a hidden root level sub-command planet plauth, and cherry-picks specific sub-commands to power its own auth sub-command. This allows the downstream application to leverage the Planet Auth Library, while also using configuration injection to provide a smoother end-user experience