Skip to content

Commit 78373c3

Browse files
committed
Working cli base <safety pete>
1 parent 874e11b commit 78373c3

14 files changed

Lines changed: 200 additions & 22 deletions

File tree

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
'Click>=7.0',
1818
]
1919
keywords = []
20-
entry_points = {'console_scripts': ['veracode-cli=veracode.cli:main']}
20+
entry_points = {'console_scripts': [
21+
'veracode-cli=veracode.old:main',
22+
'veracode=veracode.cli.cli:main'
23+
]}
2124
python_requires='>= 3.5'
2225

2326
author = 'Chuck Orde'

veracode/application.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ def __new__(self, name=None, sandbox=None, build=None):
2424
return NewApplication()
2525

2626
@classmethod
27-
def list(self):
27+
def list(self, name_only=True):
2828
""" Returns a list of applications for the current account
2929
3030
>>> apps = Application.list()
3131
>>> isinstance(apps, list)
3232
True
3333
"""
3434
apps = SDK.upload.GetAppList()
35-
return [app.app_name for app in apps.app]
35+
if name_only:
36+
return [app.app_name for app in apps.app]
37+
return [ExistingApplication(app) for app in apps.app]
3638

3739

3840
class NewApplication(object):
@@ -336,12 +338,15 @@ def build(self, obj):
336338
else:
337339
if isinstance(obj, basestring):
338340
self._build = self._get_build_by_name(obj)
339-
if isinstance(obj, build.NewBuild):
340-
new_build = SDK.upload.CreateBuild(
341+
342+
elif isinstance(obj, build.NewBuild):
343+
try:
344+
self._build = self._get_build_by_name(obj.version)
345+
except:
346+
new_build = SDK.upload.CreateBuild(
341347
version=obj.version,
342348
app_id=self.id,
343349
sandbox_id=self.sandbox.id)
344-
345-
self._build = build.NewBuild(app=self, obj=new_build)
350+
self._build = build.NewBuild(app=self, obj=new_build)
346351
self._builds = []
347352

veracode/build.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,39 @@ def __init__(self, obj=None, app=None):
4747
self._renamed_properties = ['id', 'version']
4848
self._update_properties(obj)
4949

50-
self.info = Info()
51-
self.analysis = Analysis()
52-
self.policy = Policy()
53-
50+
self._info = None
5451
self._report = None
5552
self._modules = []
5653

57-
if not self.version:
58-
# match the Veracode default
59-
self.version = datetime.today().strftime("%d %b %Y Static")
54+
if obj:
55+
self.obj = obj
56+
if hasattr(obj, 'build'):
57+
self.version = obj.build.version
58+
59+
def _get_build_info(self):
60+
if self._info is None:
61+
try:
62+
self._info = SDK.upload.GetBuildInfo(
63+
app_id=self._app.id,
64+
build_id=self.id)
65+
except VeracodeInvalidArgumentError:
66+
raise VeracodeBuildError(
67+
'The application does not have any builds.')
68+
69+
@property
70+
def info(self):
71+
self._get_build_info()
72+
return Info(self._info.build)
73+
74+
@property
75+
def analysis(self):
76+
self._get_build_info()
77+
return Analysis(self._info.build)
6078

61-
if self._app.id:
62-
info = SDK.upload.GetBuildInfo(app_id=self._app.id,
63-
build_id=self.id)
64-
if hasattr(info, 'build'):
65-
self.info = Info(info.build)
66-
self.analysis = Analysis(info.build)
67-
self.policy = Policy(info.build)
79+
@property
80+
def policy(self):
81+
self._get_build_info()
82+
return Policy(self._info.build)
6883

6984
@property
7085
def name(self):
@@ -202,7 +217,7 @@ def exists(obj, prop):
202217
def __repr__(self):
203218
if not hasattr(self, 'sandbox_name'):
204219
self.sandbox_name = None
205-
return ("<Veracode Report: application='{}', sandbox='{}',"
220+
return ("<Veracode Report: application='{}', sandbox='{}', "
206221
"build='{}', flaws={}>".format(
207222
self.app_name, self.sandbox_name,
208223
self._build.version, self.total_flaws))

veracode/cli/__init__.py

Whitespace-only changes.

veracode/cli/app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

veracode/cli/app/commands.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import click
2+
import sys
3+
from veracode.application import Application
4+
from veracode.sandbox import Sandbox
5+
6+
@click.group()
7+
def app():
8+
pass
9+
10+
11+
@app.command()
12+
def list():
13+
for app in Application.list():
14+
click.echo(app)
15+
16+
17+
@app.command()
18+
@click.option('--name', '-n', required=True)
19+
@click.option('--criticality', '-c', required=True, type=click.Choice(
20+
[ 'Very High', 'High', 'Medium', 'Low', 'Very Low' ]))
21+
22+
@click.option('--sandbox', '-s')
23+
def create(name, criticality, sandbox=None):
24+
app = Application()
25+
app.name = name
26+
app.business_criticality = criticality
27+
app = app.save()
28+
if sandbox:
29+
sbx = Sandbox()
30+
sbx.name = sandbox
31+
app.sandbox = sbx
32+
33+
34+
@app.command()
35+
@click.option('--name', '-n', required=True,
36+
help='Name of application to update')
37+
@click.confirmation_option('--force', '-f',
38+
help='Suppress prompt before removal.',
39+
prompt='Are you sure you want to delete this application')
40+
def delete(name):
41+
app = Application(name)
42+
return app.delete()
43+
44+
45+
@app.command()
46+
@click.option('--name', '-n', required=True,
47+
help='Name of application to update.')
48+
@click.option('--rename', '-r',
49+
help='New name for the application.')
50+
@click.option('--criticality', '-c', type=click.Choice(
51+
[ 'Very High', 'High', 'Medium', 'Low', 'Very Low' ],
52+
case_sensitive=True), # update app to snake case so we can go -i
53+
help='New criticality for the application.')
54+
55+
def update(name, rename=None, criticality=None):
56+
# this isn't the right way to do this, google more
57+
if not (rename or criticality):
58+
with click.Context(update) as ctx:
59+
click.echo(update.get_help(ctx))
60+
sys.exit(('\nError: You must supply either "--rename / -r" '
61+
'or "--criticality / -c"'))
62+
app = Application(name)
63+
app.name = rename
64+
app.business_criticality = '{}'.format(criticality)
65+
app.save()
66+
67+
68+
@app.command()
69+
def scan():
70+
click.echo('scan')
71+

veracode/cli/cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import click
2+
from app import commands as app
3+
from sandbox import commands as sandbox
4+
from user import commands as user
5+
from report import commands as report
6+
7+
@click.group()
8+
def main():
9+
pass
10+
11+
main.add_command(app.app)
12+
main.add_command(sandbox.sandbox)
13+
main.add_command(user.user)
14+
main.add_command(report.report)
15+
16+
if __name__ == '__main__':
17+
main()

veracode/cli/report/__init__.py

Whitespace-only changes.

veracode/cli/report/commands.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import click
2+
3+
@click.group()
4+
def report():
5+
pass
6+
7+
@report.command()
8+
def list():
9+
click.echo('list')
10+
11+
@report.command()
12+
def create():
13+
click.echo('create')
14+
15+
@report.command()
16+
def delete():
17+
click.echo('delete')
18+
19+
@report.command()
20+
def update():
21+
click.echo('update')
22+

veracode/cli/sandbox/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)