Skip to content

Commit ee0c101

Browse files
authored
add: bash export (#7)
* add: bash export * change: up stage
1 parent ead5ccf commit ee0c101

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ pip install aws-paramstore-py
1212

1313
## Usage
1414

15-
in console
15+
in shell
1616
```bash
1717
# AWS credentials from env vars
1818
aws-pspy /path/to/params
1919
# returns {"key1": "value1", "key2": "value2"}
20+
21+
eval "$(aws-pspy /path/to/params --bash-export)"
22+
# set env vars:
23+
# - key1="value1"
24+
# - key2="value2"
2025
```
2126

2227
in Python
2328
```python
2429
import aws_paramstore_py as paramstore
2530

26-
# AWS credentials from env vars
31+
# use default boto3 ssm client
2732
params = paramstore.get('/path/to/params')
2833
# dict(key1: "value1", key2: "value2")
34+
35+
# use your own boto3 ssm client
36+
import boto3
37+
ssm = boto3.client('ssm')
38+
params = paramstore.get('/path/to/params', ssm_client=ssm)
2939
```

aws_paramstore_py/__init__.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import argparse
22
import json
3+
import shlex
34

45
from .main import get
56

6-
__version__ = '0.0.4'
7+
__version__ = '0.0.5'
78

89

910
def cli():
11+
def print_bash_export(params):
12+
for key, value in params.items():
13+
var_name = key.replace('/', '_').replace('-', '_').replace('.', '_')
14+
var_val = shlex.quote(value)
15+
print('export {0}={1}'.format(var_name, var_val))
16+
1017
parser = argparse.ArgumentParser(description='Query params from AWS System Manager Parameter Store')
11-
parser.add_argument('paths', metavar='path', nargs='*', help='The hierarchy for the parameter')
18+
parser.add_argument('paths', metavar='path', nargs='*', help='The hierarchy for the parameters')
1219
parser.add_argument('--decryption', action='store_true', help='Decrypt secure values or not')
20+
parser.add_argument('--bash-export', action='store_true', help='Print bash export script')
1321
parser.add_argument('--version', action='version', version=__version__)
1422
args = parser.parse_args()
15-
paths = args.paths
16-
decryption = args.decryption
17-
params = get(*paths, decryption=decryption)
18-
print(json.dumps(params))
23+
24+
params = get(*args.paths, decryption=args.decryption)
25+
if args.bash_export:
26+
print_bash_export(params)
27+
else:
28+
print(json.dumps(params))

aws_paramstore_py/main.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import boto3
22

33

4-
def get(*paths, decryption=False):
5-
ssm = boto3.client('ssm')
4+
def get(*paths, decryption=False, ssm_client=None):
5+
if ssm_client is None:
6+
ssm_client = boto3.client('ssm')
67
path = _join_slashes(paths)
7-
response = ssm.get_parameters_by_path(Path=path, Recursive=True, WithDecryption=decryption)
8-
params = map(lambda p: _remove_prefix(p, path), response['Parameters'])
8+
response = ssm_client.get_parameters_by_path(Path=path, Recursive=True, WithDecryption=decryption)
9+
params = (_remove_prefix(p, path) for p in response['Parameters'])
910
return _convert_to_dict(params)
1011

1112

1213
def _join_slashes(paths):
13-
path = '/'.join(filter(lambda e: len(e), map(lambda e: _remove_slashes_on_edge(e), paths)))
14+
path = '/'.join(_remove_slashes_on_edge(e) for e in paths)
1415
if not path:
1516
return '/'
1617
else:
@@ -34,7 +35,10 @@ def _is_followed_by_slash(string):
3435

3536

3637
def _remove_prefix(param, prefix):
37-
param['Name'] = param['Name'][len(prefix):]
38+
name = param['Name']
39+
if _is_led_by_slash(name):
40+
name = param['Name'][len(prefix):]
41+
param['Name'] = name
3842
return param
3943

4044

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# For a discussion on single-sourcing the version across setup.py and the
3333
# project code, see
3434
# https://packaging.python.org/en/latest/single_source_version.html
35-
version='0.0.4', # Required
35+
version='0.0.5', # Required
3636

3737
# This is a one-line description or tagline of what your project does. This
3838
# corresponds to the "Summary" metadata field:
@@ -72,7 +72,7 @@
7272
# 3 - Alpha
7373
# 4 - Beta
7474
# 5 - Production/Stable
75-
'Development Status :: 3 - Alpha',
75+
'Development Status :: 4 - Beta',
7676

7777
# Indicate who your project is intended for
7878
'Intended Audience :: Developers',

spec/test_main.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ def test_get_root(self, mock):
2727

2828
def test_get_slash(self, mock):
2929
method = mock.client('ssm').get_parameters_by_path
30+
method.return_value = {'Parameters': [
31+
{'Name': '/path/to/params/key1', 'Value': "value1"},
32+
{'Name': '/path/to/params/key2', 'Value': "value2"},
33+
{'Name': 'root-key3', 'Value': "value3"}
34+
]}
3035

31-
paramstore.get('/')
36+
params = paramstore.get('/')
3237

3338
method.assert_called_with(Path='/', Recursive=True, WithDecryption=False)
39+
self.assertDictEqual({
40+
"path/to/params/key1": "value1",
41+
"path/to/params/key2": "value2",
42+
"root-key3": "value3"
43+
}, params)
3444

3545
def test_get_leading_slash(self, mock):
3646
method = mock.client('ssm').get_parameters_by_path

0 commit comments

Comments
 (0)