Skip to content

Commit 73f1acc

Browse files
committed
adding fancier help text
1 parent 75350b2 commit 73f1acc

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

awth/help_text.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"""
2+
help text functions for the onboardme cli
3+
"""
4+
# file for rich printing
5+
import click
6+
from rich.console import Console
7+
from rich.highlighter import RegexHighlighter
8+
from rich.panel import Panel
9+
from rich.table import Table
10+
from rich.text import Text
11+
from rich.theme import Theme
12+
13+
# custom local module
14+
from .constants import (VERSION)
15+
16+
17+
# this is for creating new help text svgs for the READMEs
18+
RECORD = False
19+
20+
21+
def pretty_choices(default_list: list) -> str:
22+
"""
23+
Takes a list of default choices and surrounds them with a meta markup tag
24+
and join them with a comma for a pretty return "Choices" string.
25+
Example: pretty_choices(['beep', 'boop']) returns:
26+
'Choices: [meta]beep[/meta], [meta]boop[/meta]'
27+
"""
28+
defaults = '[/meta], [meta]'.join(default_list)
29+
return 'Choices: [meta]' + defaults + '[/meta]'
30+
31+
32+
def options_help() -> dict:
33+
"""
34+
Help text for all the options/switches for main()
35+
"""
36+
log_levels = pretty_choices(['CRITICAL', 'ERROR', 'WARNING', 'INFO',
37+
'DEBUG', 'NOTSET'])
38+
39+
return {
40+
"device": "The MFA Device ARN. This value can also be provided via "
41+
"the environment variable 'MFA_DEVICE' or the "
42+
"~/.aws/credentials variable 'aws_mfa_device'.",
43+
44+
'duration': "The duration, in seconds, that the temporary "
45+
"credentials should remain valid. Minimum value: "
46+
"900 (15 minutes). Maximum: 129600 (36 hours). "
47+
"Defaults to 43200 (12 hours), or 3600 (one "
48+
"hour) when using '--assume-role'. This value "
49+
"can also be provided via the environment "
50+
"variable 'MFA_STS_DURATION'. ",
51+
52+
'profile': "If using profiles, specify the name here. The "
53+
"default profile name is 'default'. The value can "
54+
"also be provided via the environment variable "
55+
"'AWS_PROFILE'.",
56+
57+
'long-term-suffix': "The suffix appended to the profile name to"
58+
"identify the long term credential section",
59+
'short-term-suffix': "The suffix appended to the profile name to"
60+
"identify the short term credential section",
61+
'assume-role': "The ARN of the AWS IAM Role you would like to "
62+
"assume, if specified. This value can also be provided"
63+
" via the environment variable 'MFA_ASSUME_ROLE'. "
64+
"Example: 'arn:aws:iam::123456788990:role/RoleName'",
65+
'role-session-name': "Friendly session name required when using --assume-role",
66+
'force': "Refresh credentials even if currently valid.",
67+
'log-level': f"Set log level. {log_levels}",
68+
'setup': "Setup a new log term credentials section",
69+
'token': "Provide MFA token as an argument",
70+
'region': "AWS STS Region",
71+
'keychain': "Use system keychain to store or retrieve long term credentials"
72+
}
73+
74+
75+
class RichCommand(click.Command):
76+
"""
77+
Override Clicks help with a Richer version.
78+
79+
This is from the Textualize/rich-cli project on github.com:
80+
https://github.com/Textualize/rich-cli
81+
"""
82+
83+
def format_help(self, ctx, formatter):
84+
85+
class OptionHighlighter(RegexHighlighter):
86+
highlights = [r"(?P<switch>\-\w)",
87+
r"(?P<option>\-\-[\w\-]+)",
88+
r"(?P<unstable>[b][e][t][a])"]
89+
90+
highlighter = OptionHighlighter()
91+
92+
console = Console(theme=Theme({"option": "cornflower_blue",
93+
"switch": "deep_sky_blue1",
94+
"meta": "light_steel_blue",
95+
"unstable": "italic cyan"}),
96+
highlighter=highlighter, record=RECORD)
97+
98+
title = "☁️ [cornflower_blue]awth[/] 🗝️\n"
99+
desc = (
100+
"[steel_blue]Authenticate to AWS using MFA.")
101+
102+
console.print(title + desc, justify="center")
103+
104+
console.print("\n[b]Usage[/]: [royal_blue1]awth[/] " +
105+
"[cornflower_blue][OPTIONS]\n")
106+
107+
options_table = Table(highlight=True, box=None, show_header=False,
108+
row_styles=["", "dim"],
109+
padding=(1, 1, 0, 0))
110+
111+
# this used to be self.get_params(ctx)[1:] to have only one hidden option
112+
for param in self.get_params(ctx):
113+
114+
if len(param.opts) == 2:
115+
opt1 = highlighter(param.opts[1])
116+
opt2 = highlighter(param.opts[0])
117+
else:
118+
opt2 = highlighter(param.opts[0])
119+
opt1 = Text("")
120+
121+
if param.metavar:
122+
opt2 += Text(f" {param.metavar}",
123+
style="meta")
124+
125+
options = Text(" ".join(reversed(param.opts)))
126+
help_record = param.get_help_record(ctx)
127+
if help_record is None:
128+
help = ""
129+
else:
130+
help = Text.from_markup(param.get_help_record(ctx)[-1],
131+
emoji=False)
132+
133+
if param.metavar:
134+
options += f" {param.metavar}"
135+
136+
options_table.add_row(opt1, opt2, highlighter(help))
137+
138+
url = (" ♥ docs: [link=https://github.com/small-hack/awth]"
139+
"small-hack/awth[/link]")
140+
141+
console.print(Panel(options_table,
142+
border_style="dim light_steel_blue",
143+
title="⌥ Options",
144+
title_align="left",
145+
subtitle=url,
146+
subtitle_align="right"))
147+
148+
# I use this to print a pretty svg at the end sometimes
149+
if RECORD:
150+
console.save_svg("screenshots/awth.svg", title="term")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "awth"
3-
version = "0.1.0a1"
3+
version = "0.1.0a2"
44
description = "awth your way into aws, again, with mfa"
55
authors = [
66
"jessebot <[email protected]>",

0 commit comments

Comments
 (0)