-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathghs.py
More file actions
185 lines (162 loc) · 5.85 KB
/
ghs.py
File metadata and controls
185 lines (162 loc) · 5.85 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import click
from .config import pass_config, pass_validate, create_artifacts, \
list_by_comma, print_pretty_json, is_docker
from .profile import Profile
from .secret import Secret
class AliasedGroup(click.Group):
def get_command(self, ctx, cmd_name):
aliases = {
"p": "profile",
"s": "secret",
"a": "apply",
"g": "get",
"d": "delete",
"l": "list",
}
if len(cmd_name) == 2:
words = [aliases[char] for char in cmd_name if char in aliases]
cmd_name = "-".join(words)
rv = click.Group.get_command(self, ctx, cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx)
if x.startswith(cmd_name)]
if not matches:
return None
elif len(matches) == 1:
return click.Group.get_command(self, ctx, matches[0])
ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")
# @click.group()
@ click.command(cls=AliasedGroup)
@ pass_config
@ click.option('--ci', '-ci', is_flag=True, help="Use this flag to skip confirmation prompts") # noqa: E501
def cli(config, ci):
"""All commands can run without providing options, and then you'll be prompted to insert values.\n
Secrets' values and Personal-Access-Tokens are hidden when prompted""" # noqa: E501
if is_docker():
ci = True
config.ci = ci # noqa: F821
@ cli.command()
@ pass_config
def init(config):
"""Create a credentials file to store your profiles"""
create_artifacts(config)
@ cli.command()
@ pass_validate
@ pass_config
@ click.option('--profile-name', '-p', prompt=True)
@ click.option('--github-owner', '-o', prompt=True)
@ click.option(
'--personal-access-token', '-t', prompt=True,
hide_input=True, confirmation_prompt=True
)
def profile_apply(
config, validate,
profile_name, github_owner, personal_access_token
):
"""[pa] Create or modify multiple profiles providing a string delimited by commas ","\n
Example: ghs profile-apply -p 'willy, oompa'""" # noqa: 501
profile_names = list_by_comma(profile_name)
for prof_name in profile_names:
profile = Profile(config, prof_name)
profile.apply(github_owner, personal_access_token)
@ cli.command()
@ pass_validate
@ pass_config
@ click.option('--profile-name', '-p', prompt=True)
def profile_delete(
config, validate,
profile_name
):
"""[pd] Delete multiple profiles providing a string delimited by commas ","\n
Example: ghs profile-delete -p 'willy, oompa'"""
profile_names = list_by_comma(profile_name)
for prof_name in profile_names:
profile = Profile(config, prof_name)
profile.delete()
@ cli.command()
@ pass_validate
@ pass_config
def profile_list(config, validate):
"""[pl] List all profile - truncates personal access tokens"""
Profile.lista()
@ cli.command()
@ pass_validate
@ pass_config
@ click.option('--repository', '-r', prompt=True)
@ click.option('--profile-name', '-p', prompt=True)
@ click.option('--secret-name', '-s', prompt=True)
@ click.option(
'--secret-value', '-v', prompt=True,
hide_input=True, confirmation_prompt=True
)
def secret_apply(
config, validate,
repository, profile_name, secret_name, secret_value
):
"""[sa] Apply to multiple repositories providing a string delimited by commas ","\n
Example: ghs secret-apply -p willy -r 'githubsecrets, serverless-template'""" # noqa: 501
profile = Profile(config, profile_name)
repositories = list_by_comma(repository)
responses = []
for repo in repositories:
secret = Secret(config, profile, repo, secret_name, secret_value)
secret.apply()
responses.append(secret.apply())
print_pretty_json(responses)
@ cli.command()
@ pass_validate
@ pass_config
@ click.option('--repository', '-r', prompt=True)
@ click.option('--profile-name', '-p', prompt=True)
@ click.option('--secret-name', '-s', prompt=True)
def secret_delete(
config, validate,
repository, profile_name, secret_name
):
"""[sd] Delete secrets from multiple repositories providing a string delimited by commas ","\n
Example: ghs secret-delete -p willy -r 'githubsecrets, serverless-template'""" # noqa: 501
profile = Profile(config, profile_name)
repositories = list_by_comma(repository)
responses = []
for repo in repositories:
secret = Secret(config, profile, repo, secret_name)
responses.append(secret.delete())
print_pretty_json(responses)
@ cli.command()
@ pass_validate
@ pass_config
@ click.option('--repository', '-r', prompt=True)
@ click.option('--profile-name', '-p', prompt=True)
@ click.option('--secret-name', '-s', prompt=True)
def secret_get(
config, validate,
repository, profile_name, secret_name
):
"""[sg] Get secrets from multiple repositories providing a string delimited by commas ","\n
Example: ghs secret-get -p willy -r 'githubsecrets, serverless-template'""" # noqa: 501
profile = Profile(config, profile_name)
repositories = list_by_comma(repository)
responses = []
for repo in repositories:
secret = Secret(config, profile, repo, secret_name)
responses.append(secret.get())
print_pretty_json(responses)
@ cli.command()
@ pass_validate
@ pass_config
@ click.option('--repository', '-r', prompt=True)
@ click.option('--profile-name', '-p', prompt=True)
def secret_list(
config, validate,
repository, profile_name
):
"""[sl] List secrets of multiple repositories providing a string delimited by commas ","\n
Example: ghs secret-delete -p willy -r 'githubsecrets, serverless-template'""" # noqa: 501
profile = Profile(config, profile_name)
repositories = list_by_comma(repository)
responses = []
for repo in repositories:
secret = Secret(config, profile, repo)
responses.append(secret.lista())
print_pretty_json(responses)