Skip to content

Commit 66a3cde

Browse files
committed
#1481 refactor block and file cli to shows the whole notes in a format json output
1 parent c7d2abc commit 66a3cde

File tree

4 files changed

+46
-47
lines changed

4 files changed

+46
-47
lines changed

SoftLayer/CLI/block/list.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import SoftLayer
66
from SoftLayer.CLI import columns as column_helper
77
from SoftLayer.CLI import environment
8-
from SoftLayer.CLI import formatting
9-
8+
from SoftLayer.CLI import storage_utils
109

1110
COLUMNS = [
1211
column_helper.Column('id', ('id',), mask="id"),
@@ -18,7 +17,7 @@
1817
'storage_type',
1918
lambda b: b['storageType']['keyName'].split('_').pop(0)
2019
if 'storageType' in b and 'keyName' in b['storageType']
21-
and isinstance(b['storageType']['keyName'], str)
20+
and isinstance(b['storageType']['keyName'], str)
2221
else '-',
2322
mask="storageType.keyName"),
2423
column_helper.Column('capacity_gb', ('capacityGb',), mask="capacityGb"),
@@ -52,8 +51,6 @@
5251
'notes'
5352
]
5453

55-
DEFAULT_NOTES_SIZE = 20
56-
5754

5855
@click.command()
5956
@click.option('--username', '-u', help='Volume username')
@@ -78,24 +75,5 @@ def cli(env, sortby, columns, datacenter, username, storage_type, order):
7875
order=order,
7976
mask=columns.mask())
8077

81-
table = formatting.Table(columns.columns)
82-
table.sortby = sortby
83-
84-
_reduce_notes(block_volumes)
85-
86-
for block_volume in block_volumes:
87-
table.add_row([value or formatting.blank()
88-
for value in columns.row(block_volume)])
89-
78+
table = storage_utils.build_output_table(env, block_volumes, columns, sortby)
9079
env.fout(table)
91-
92-
93-
def _reduce_notes(block_volumes):
94-
"""Reduces the size of the notes in a volume list.
95-
96-
:param block_volumes: An list of block volumes
97-
"""
98-
for block_volume in block_volumes:
99-
if len(block_volume.get('notes', '')) > DEFAULT_NOTES_SIZE:
100-
shortened_notes = block_volume['notes'][:DEFAULT_NOTES_SIZE]
101-
block_volume['notes'] = shortened_notes

SoftLayer/CLI/environment.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def fmt(self, output, fmt=None):
5252
fmt = self.format
5353
return formatting.format_output(output, fmt)
5454

55+
def format_output_is_json(self):
56+
"""Return True if format output is json or jsonraw"""
57+
return 'json' in self.format
58+
5559
def fout(self, output, newline=True):
5660
"""Format the input and output to the console (stdout)."""
5761
if output is not None:

SoftLayer/CLI/file/list.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import SoftLayer
66
from SoftLayer.CLI import columns as column_helper
77
from SoftLayer.CLI import environment
8-
from SoftLayer.CLI import formatting
8+
from SoftLayer.CLI import storage_utils
99

1010
COLUMNS = [
1111
column_helper.Column('id', ('id',), mask="id"),
@@ -76,24 +76,5 @@ def cli(env, sortby, columns, datacenter, username, storage_type, order):
7676
order=order,
7777
mask=columns.mask())
7878

79-
table = formatting.Table(columns.columns)
80-
table.sortby = sortby
81-
82-
_reduce_notes(file_volumes)
83-
84-
for file_volume in file_volumes:
85-
table.add_row([value or formatting.blank()
86-
for value in columns.row(file_volume)])
87-
79+
table = storage_utils.build_output_table(env, file_volumes, columns, sortby)
8880
env.fout(table)
89-
90-
91-
def _reduce_notes(file_volumes):
92-
"""Reduces the size of the notes in a volume list.
93-
94-
:param file_volumes: An list of file volumes
95-
"""
96-
for file_volume in file_volumes:
97-
if len(file_volume.get('notes', '')) > DEFAULT_NOTES_SIZE:
98-
shortened_notes = file_volume['notes'][:DEFAULT_NOTES_SIZE]
99-
file_volume['notes'] = shortened_notes

SoftLayer/CLI/storage_utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@
22
# :license: MIT, see LICENSE for more details.
33

44
from SoftLayer.CLI import columns as column_helper
5+
from SoftLayer.CLI import formatting
6+
7+
DEFAULT_NOTES_SIZE = 20
8+
9+
10+
def reduce_notes(volumes, env):
11+
"""Reduces all long notes found in the volumes list just if the format output is different from a JSON format.
12+
13+
:param list volumes: An list of storage volumes
14+
:param env :A environment console.
15+
"""
16+
if env.format_output_is_json():
17+
return
18+
19+
for volume in volumes:
20+
if len(volume.get('notes', '')) > DEFAULT_NOTES_SIZE:
21+
shortened_notes = volume['notes'][:DEFAULT_NOTES_SIZE]
22+
volume['notes'] = shortened_notes
23+
24+
25+
def build_output_table(env, volumes, columns, sortby):
26+
"""Builds a formatting table for a list of volumes.
27+
28+
:param env :A Environment console.
29+
:param list volumes: An list of storage volumes
30+
:param columns :A ColumnFormatter for column names
31+
:param str sortby :A string to sort by.
32+
"""
33+
table = formatting.Table(columns.columns)
34+
if sortby in table.columns:
35+
table.sortby = sortby
36+
37+
reduce_notes(volumes, env)
38+
for volume in volumes:
39+
table.add_row([value or formatting.blank()
40+
for value in columns.row(volume)])
41+
return table
542

643

744
def _format_name(obj):
@@ -96,7 +133,6 @@ def _format_name(obj):
96133
"""),
97134
]
98135

99-
100136
DEFAULT_COLUMNS = [
101137
'id',
102138
'name',

0 commit comments

Comments
 (0)