|
12 | 12 | from tenint.models.pyproject import PyProject
|
13 | 13 |
|
14 | 14 | console = Console()
|
15 |
| -tmpl = Path(os.path.dirname(__file__)).joinpath('templates') |
| 15 | +tmpl = Path(os.path.dirname(__file__)).joinpath("templates") |
16 | 16 | app = Typer()
|
17 | 17 |
|
18 | 18 |
|
19 |
| -@app.command('init') |
| 19 | +@app.command("init") |
20 | 20 | def init_connector(
|
21 | 21 | path: Annotated[
|
22 |
| - Path, Option(help='initialization path', dir_okay=True, file_okay=False) |
23 |
| - ] = Path('.'), |
| 22 | + Path, Option(help="initialization path", dir_okay=True, file_okay=False) |
| 23 | + ] = Path("."), |
24 | 24 | ):
|
25 | 25 | is_dirty = False
|
26 |
| - for fn in ('pyproject.toml', 'connector.py'): |
| 26 | + for fn in ("pyproject.toml", "connector.py"): |
27 | 27 | src = tmpl.joinpath(fn)
|
28 | 28 | dest = path.joinpath(fn)
|
29 | 29 |
|
30 | 30 | if dest.exists():
|
31 | 31 | console.print(
|
32 |
| - f'[yellow bold]WARNING[/yellow bold]: skipped ' |
33 |
| - f'existing [cyan]{dest}[/cyan] as it already exists' |
| 32 | + f"[yellow bold]WARNING[/yellow bold]: skipped " |
| 33 | + f"existing [cyan]{dest}[/cyan] as it already exists" |
34 | 34 | )
|
35 | 35 | is_dirty = True
|
36 | 36 | else:
|
37 |
| - with src.open('rb') as sobj, dest.open('wb') as dobj: |
| 37 | + with src.open("rb") as sobj, dest.open("wb") as dobj: |
38 | 38 | dobj.write(sobj.read())
|
39 | 39 |
|
40 |
| - tests = path.joinpath('tests') |
| 40 | + tests = path.joinpath("tests") |
41 | 41 | if not tests.exists():
|
42 | 42 | tests.mkdir()
|
43 |
| - src = tmpl.joinpath('test_connector.py') |
44 |
| - dest = tests.joinpath('test_connector.py') |
45 |
| - with dest.open('wb') as dobj, src.open('rb') as sobj: |
| 43 | + src = tmpl.joinpath("test_connector.py") |
| 44 | + dest = tests.joinpath("test_connector.py") |
| 45 | + with dest.open("wb") as dobj, src.open("rb") as sobj: |
46 | 46 | dobj.write(sobj.read())
|
47 | 47 | else:
|
48 | 48 | console.print(
|
49 |
| - '[yellow bold]NOTE[/yellow bold]: skipped adding tests. tests folder exists.' |
| 49 | + "[yellow bold]NOTE[/yellow bold]: skipped adding tests. tests folder exists." |
50 | 50 | )
|
51 | 51 |
|
52 | 52 | console.print(
|
53 | 53 | Panel(
|
54 |
| - 'Now that you have an initialized tenint connector, please review the ' |
55 |
| - 'files that have been created for your next steps.\n\n' |
56 |
| - '- [bold cyan]pyproject.toml[/bold cyan] contains the project ' |
57 |
| - ' requirements and is used to build the marketplace files\n' |
58 |
| - '- [bold cyan]connector.py[/bold cyan] is a sample connector ' |
59 |
| - 'to get you started. It contains some example settings, a ' |
60 |
| - 'credential example, and a sample function.', |
61 |
| - title='[bold cyan]Tenable Integration Framework Connector[/bold cyan]', |
| 54 | + "Now that you have an initialized tenint connector, please review the " |
| 55 | + "files that have been created for your next steps.\n\n" |
| 56 | + "- [bold cyan]pyproject.toml[/bold cyan] contains the project " |
| 57 | + " requirements and is used to build the marketplace files\n" |
| 58 | + "- [bold cyan]connector.py[/bold cyan] is a sample connector " |
| 59 | + "to get you started. It contains some example settings, a " |
| 60 | + "credential example, and a sample function.", |
| 61 | + title="[bold cyan]Tenable Integration Framework Connector[/bold cyan]", |
62 | 62 | )
|
63 | 63 | )
|
64 | 64 | if is_dirty:
|
65 | 65 | console.print(
|
66 | 66 | Panel(
|
67 |
| - 'Could not initialize all of the files as some appeared to already ' |
68 |
| - 'exist. Any files that previously existed were not modified. ' |
69 |
| - 'Recommend manually reviewing your project to ensure everything ' |
70 |
| - 'looks correct.', |
71 |
| - title='[bold red]Dirty Environment Warning[/bold red]', |
72 |
| - style='yellow', |
| 67 | + "Could not initialize all of the files as some appeared to already " |
| 68 | + "exist. Any files that previously existed were not modified. " |
| 69 | + "Recommend manually reviewing your project to ensure everything " |
| 70 | + "looks correct.", |
| 71 | + title="[bold red]Dirty Environment Warning[/bold red]", |
| 72 | + style="yellow", |
73 | 73 | )
|
74 | 74 | )
|
75 | 75 |
|
76 | 76 |
|
77 |
| -@app.command('build') |
| 77 | +@app.command("build") |
78 | 78 | def build_connector(
|
79 |
| - path: Annotated[Path, Option(help='connector code path')] = Path('.'), |
80 |
| - platform: Annotated[str | None, Option(help='platform to build to')] = None, |
| 79 | + path: Annotated[Path, Option(help="connector code path")] = Path("."), |
| 80 | + platform: Annotated[str | None, Option(help="platform to build to")] = None, |
81 | 81 | tag: Annotated[
|
82 | 82 | str | None,
|
83 |
| - Option( |
84 |
| - help='Tag for the fimal image. Defaults to the image settings if unspecified.' |
85 |
| - ), |
| 83 | + Option(help="Tag for the fimal image. defaults to the project name."), |
86 | 84 | ] = None,
|
87 | 85 | cleanup: Annotated[
|
88 |
| - bool | None, Option(help='auto-remove any generated build files?') |
| 86 | + bool | None, Option(help="auto-remove any generated build files?") |
89 | 87 | ] = None,
|
90 | 88 | ):
|
91 |
| - tmpldfile = tmpl.joinpath('Dockerfile') |
92 |
| - dockerfile = path.joinpath('Dockerfile') |
93 |
| - pyproject = path.joinpath('pyproject.toml') |
| 89 | + tmpldfile = tmpl.joinpath("Dockerfile") |
| 90 | + dockerfile = path.joinpath("Dockerfile") |
| 91 | + pyproject = path.joinpath("pyproject.toml") |
94 | 92 |
|
95 | 93 | if not tag:
|
96 |
| - with pyproject.open('rb') as pf: |
| 94 | + with pyproject.open("rb") as pf: |
97 | 95 | project = PyProject(**tomllib.load(pf))
|
98 |
| - tag = project.tool.tenint.connector.images.amd64 |
| 96 | + tag = project.project.name |
99 | 97 |
|
100 | 98 | if not dockerfile.exists():
|
101 |
| - with tmpldfile.open('rb') as src, dockerfile.open('wb') as dst: |
| 99 | + with tmpldfile.open("rb") as src, dockerfile.open("wb") as dst: |
102 | 100 | dst.write(src.read())
|
103 | 101 | cleanup = True if cleanup is None else cleanup
|
104 | 102 |
|
105 | 103 | builder = subprocess.Popen(
|
106 |
| - ['docker', 'build', '-t', tag, path], |
| 104 | + ["docker", "build", "-t", tag, path], |
107 | 105 | stdout=subprocess.PIPE,
|
108 | 106 | stderr=subprocess.STDOUT,
|
109 | 107 | )
|
110 | 108 | for line in builder.stdout:
|
111 |
| - console.out(line.decode('utf-8').strip('\n')) |
| 109 | + console.out(line.decode("utf-8").strip("\n")) |
112 | 110 |
|
113 | 111 | if cleanup:
|
114 | 112 | dockerfile.unlink()
|
115 | 113 |
|
116 | 114 |
|
117 |
| -@app.command('marketplace') |
| 115 | +@app.command("marketplace") |
118 | 116 | def gen_marketplace(
|
119 |
| - image: Annotated[ |
120 |
| - str, Option(..., help='Docker Image Name') |
121 |
| - ] = 'local/connector-example', |
| 117 | + image: Annotated[str, Option(..., help="Docker Image Name")] = None, |
122 | 118 | icon: Annotated[
|
123 |
| - str, Option(..., help='Icon Image URL') |
124 |
| - ] = 'https://nourl.example/logo.svg', |
125 |
| - path: Annotated[Path, Option(help='connector code path')] = Path('.'), |
126 |
| - output: Annotated[Path, Option(help='output marketplace json file')] = None, |
| 119 | + str, Option(..., help="Icon Image URL") |
| 120 | + ] = "https://nourl.example/logo.svg", |
| 121 | + path: Annotated[Path, Option(help="connector code path")] = Path("."), |
| 122 | + output: Annotated[Path, Option(help="output marketplace json file")] = None, |
127 | 123 | ):
|
128 |
| - mpfile = path.joinpath('marketplace-object.json') |
| 124 | + pyproject_file = path.joinpath("pyproject.toml") |
| 125 | + with pyproject_file.open("rb") as fobj: |
| 126 | + project = PyProject(**tomllib.load(fobj)) |
| 127 | + |
| 128 | + if not image: |
| 129 | + image = project.project.name |
| 130 | + |
| 131 | + mpfile = path.joinpath("marketplace-object.json") |
129 | 132 | mp = MarketplaceConnector.load_from_pyproject(
|
130 |
| - filename='pyproject.toml', image_url=image, icon_url=icon |
131 |
| - ).model_dump(mode='json') |
| 133 | + filename=project, image_url=image, icon_url=icon |
| 134 | + ).model_dump(mode="json") |
132 | 135 | console.print(mp)
|
133 | 136 | if output:
|
134 |
| - with mpfile.open('w', encoding='utf-8') as fobj: |
| 137 | + with mpfile.open("w", encoding="utf-8") as fobj: |
135 | 138 | fobj.write(mp.model_dump_json())
|
0 commit comments