forked from fugue/credstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
78 lines (63 loc) · 2.13 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# Copyright 2015 Luminal, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import boto3
import botocore.exceptions
def clean_fail(func):
'''
A decorator to cleanly exit on a failed call to AWS.
catch a `botocore.exceptions.ClientError` raised from an action.
This sort of error is raised if you are targeting a region that
isn't set up (see, `credstash setup`.
'''
def func_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except botocore.exceptions.ClientError as e:
print(str(e), file=sys.stderr)
sys.exit(1)
return func_wrapper
def printStdErr(s):
sys.stderr.write(str(s))
sys.stderr.write("\n")
def fatal(s):
printStdErr(s)
sys.exit(1)
def expand_wildcard(string, secrets):
prog = re.compile('^' + string.replace(WILDCARD_CHAR, '.*') + '$')
output = []
for secret in secrets:
if prog.search(secret) is not None:
output.append(secret)
return output
def csv_dump(dictionary):
csvfile = StringIO()
csvwriter = csv.writer(csvfile)
for key in dictionary:
csvwriter.writerow([key, dictionary[key]])
return csvfile.getvalue()
def dotenv_dump(dictionary):
dotenv_buffer = StringIO()
for key in dictionary:
dotenv_buffer.write("%s=%s\n" % (key.upper(), dictionary[key]))
dotenv_buffer.seek(0)
return dotenv_buffer.read()
def paddedInt(i):
'''
return a string that contains `i`, left-padded with 0's up to PAD_LEN digits
'''
i_str = str(i)
pad = PAD_LEN - len(i_str)
return (pad * "0") + i_str