Skip to content

Commit 842f9e5

Browse files
committed
Add config option to be able to use non-default charon configuration
1 parent 1e9dba5 commit 842f9e5

File tree

9 files changed

+96
-15
lines changed

9 files changed

+96
-15
lines changed

charon/cmd/cmd_cache.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
format of CF defining too, and each path should be in a single line.
5959
"""
6060
)
61+
@option(
62+
"--config",
63+
"-c",
64+
help="""
65+
The charon configuration yaml file path. Default is
66+
$HOME/.charon/charon.yaml
67+
"""
68+
)
6169
@option(
6270
"--debug",
6371
"-D",
@@ -79,6 +87,7 @@ def invalidate(
7987
target: str,
8088
paths: List[str],
8189
path_file: str,
90+
config: str = None,
8291
quiet: bool = False,
8392
debug: bool = False
8493
):
@@ -110,7 +119,7 @@ def invalidate(
110119
break
111120

112121
try:
113-
(buckets, aws_profile) = _init_cmd(target)
122+
(buckets, aws_profile) = _init_cmd(target, config)
114123

115124
for b in buckets:
116125
cf_client = CFClient(aws_profile=aws_profile)
@@ -144,6 +153,14 @@ def invalidate(
144153
""",
145154
required=True
146155
)
156+
@option(
157+
"--config",
158+
"-c",
159+
help="""
160+
The charon configuration yaml file path. Default is
161+
$HOME/.charon/charon.yaml
162+
"""
163+
)
147164
@option(
148165
"--debug",
149166
"-D",
@@ -164,6 +181,7 @@ def invalidate(
164181
def check(
165182
invalidation_id: str,
166183
target: str,
184+
config: str = None,
167185
quiet: bool = False,
168186
debug: bool = False
169187
):
@@ -175,7 +193,7 @@ def check(
175193
is_quiet=quiet, is_debug=debug, use_log_file=False
176194
)
177195
try:
178-
(buckets, aws_profile) = _init_cmd(target)
196+
(buckets, aws_profile) = _init_cmd(target, config)
179197
if not buckets:
180198
sys.exit(1)
181199

@@ -203,8 +221,8 @@ def check(
203221
sys.exit(2)
204222

205223

206-
def _init_cmd(target: str) -> Tuple[List[Tuple[str, str, str, str, str]], str]:
207-
conf = get_config()
224+
def _init_cmd(target: str, config: str) -> Tuple[List[Tuple[str, str, str, str, str]], str]:
225+
conf = get_config(config)
208226
if not conf:
209227
sys.exit(1)
210228

charon/cmd/cmd_checksum.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,20 @@ def validate(
191191
""",
192192
required=True
193193
)
194+
@option(
195+
"--config",
196+
"-c",
197+
help="""
198+
The charon configuration yaml file path. Default is
199+
$HOME/.charon/charon.yaml
200+
"""
201+
)
194202
@command()
195203
def refresh(
196204
target: str,
197205
paths: List[str],
198206
path_file: str,
207+
config: str = None,
199208
quiet: bool = False,
200209
debug: bool = False
201210
):
@@ -219,7 +228,7 @@ def refresh(
219228
if paths:
220229
work_paths.extend(paths)
221230

222-
conf = get_config()
231+
conf = get_config(config)
223232
aws_profile = os.getenv("AWS_PROFILE") or conf.get_aws_profile()
224233
if not aws_profile:
225234
logger.error("No AWS profile specified!")

charon/cmd/cmd_delete.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@
9696
be extracted, when needed.
9797
""",
9898
)
99+
@option(
100+
"--config",
101+
"-c",
102+
help="""
103+
The charon configuration yaml file path. Default is
104+
$HOME/.charon/charon.yaml
105+
"""
106+
)
99107
@option(
100108
"--debug",
101109
"-D",
@@ -120,6 +128,7 @@ def delete(
120128
root_path="maven-repository",
121129
ignore_patterns: List[str] = None,
122130
work_dir: str = None,
131+
config: str = None,
123132
debug=False,
124133
quiet=False,
125134
dryrun=False
@@ -136,7 +145,7 @@ def delete(
136145
"no files will be deleted.")
137146
if not _validate_prod_key(product, version):
138147
return
139-
conf = get_config()
148+
conf = get_config(config)
140149
if not conf:
141150
sys.exit(1)
142151

charon/cmd/cmd_index.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
""",
4343
required=True
4444
)
45+
@option(
46+
"--config",
47+
"-c",
48+
help="""
49+
The charon configuration yaml file path. Default is
50+
$HOME/.charon/charon.yaml
51+
"""
52+
)
4553
@option(
4654
"--debug",
4755
"-D",
@@ -61,6 +69,7 @@
6169
def index(
6270
path: str,
6371
target: str,
72+
config: str = None,
6473
debug: bool = False,
6574
quiet: bool = False,
6675
dryrun: bool = False
@@ -73,7 +82,7 @@ def index(
7382
is_quiet=quiet, is_debug=debug, use_log_file=False
7483
)
7584
try:
76-
conf = get_config()
85+
conf = get_config(config)
7786
if not conf:
7887
sys.exit(1)
7988

charon/cmd/cmd_upload.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@
9797
be extracted, when needed.
9898
""",
9999
)
100+
@option(
101+
"--config",
102+
"-c",
103+
help="""
104+
The charon configuration yaml file path. Default is
105+
$HOME/.charon/charon.yaml
106+
"""
107+
)
100108
@option(
101109
"--contain_signature",
102110
"-s",
@@ -137,6 +145,7 @@ def upload(
137145
root_path="maven-repository",
138146
ignore_patterns: List[str] = None,
139147
work_dir: str = None,
148+
config: str = None,
140149
contain_signature: bool = False,
141150
sign_key: str = "redhatdevel",
142151
debug=False,
@@ -155,7 +164,7 @@ def upload(
155164
"no files will be uploaded.")
156165
if not _validate_prod_key(product, version):
157166
return
158-
conf = get_config()
167+
conf = get_config(config)
159168
if not conf:
160169
sys.exit(1)
161170

@@ -211,7 +220,8 @@ def upload(
211220
cf_enable=conf.is_aws_cf_enable(),
212221
key=sign_key,
213222
dry_run=dryrun,
214-
manifest_bucket_name=manifest_bucket_name
223+
manifest_bucket_name=manifest_bucket_name,
224+
config=config
215225
)
216226
if not succeeded:
217227
sys.exit(1)

charon/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ def is_aws_cf_enable(self) -> bool:
6868
return self.__aws_cf_enable
6969

7070

71-
def get_config() -> Optional[CharonConfig]:
72-
config_file_path = os.path.join(os.getenv("HOME"), ".charon", CONFIG_FILE)
71+
def get_config(cfgPath=None) -> Optional[CharonConfig]:
72+
config_file_path = cfgPath
73+
if not config_file_path or not os.path.isfile(config_file_path):
74+
config_file_path = os.path.join(os.getenv("HOME"), ".charon", CONFIG_FILE)
7375
data = read_yaml_from_file_path(config_file_path, 'schemas/charon.json')
7476
return CharonConfig(data)
7577

charon/pkgs/maven.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def handle_maven_uploading(
272272
cf_enable=False,
273273
key=None,
274274
dry_run=False,
275-
manifest_bucket_name=None
275+
manifest_bucket_name=None,
276+
config=None
276277
) -> Tuple[str, bool]:
277278
""" Handle the maven product release tarball uploading process.
278279
* repo is the location of the tarball in filesystem
@@ -406,7 +407,7 @@ def handle_maven_uploading(
406407

407408
# 10. Generate signature file if contain_signature is set to True
408409
if gen_sign:
409-
conf = get_config()
410+
conf = get_config(config)
410411
if not conf:
411412
sys.exit(1)
412413
suffix_list = __get_suffix(PACKAGE_TYPE_MAVEN, conf)

charon/pkgs/npm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def handle_npm_uploading(
8787
cf_enable=False,
8888
key=None,
8989
dry_run=False,
90-
manifest_bucket_name=None
90+
manifest_bucket_name=None,
91+
config=None
9192
) -> Tuple[str, bool]:
9293
""" Handle the npm product release tarball uploading process.
9394
For NPM uploading, tgz file and version metadata will be relocated based
@@ -189,7 +190,7 @@ def handle_npm_uploading(
189190
logger.info("package.json uploading done")
190191

191192
if gen_sign:
192-
conf = get_config()
193+
conf = get_config(config)
193194
if not conf:
194195
sys.exit(1)
195196
suffix_list = __get_suffix(PACKAGE_TYPE_NPM, conf)

tests/test_config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
import charon.config as config
2424
import re
25+
import tempfile
2526

2627
from charon.constants import DEFAULT_REGISTRY
28+
from charon.utils.files import overwrite_file
2729
from tests.base import BaseTest
2830

2931

@@ -49,6 +51,26 @@ def test_no_config(self):
4951
with pytest.raises(FileNotFoundError):
5052
config.get_config()
5153

54+
def test_non_default_config(self):
55+
self.__base.setUp()
56+
config_content = """
57+
ignore_patterns:
58+
- ".*^(redhat).*"
59+
60+
targets:
61+
changed:
62+
- bucket: changed-bucket
63+
prefix: changed-prefix
64+
"""
65+
_, tmp_config_file = tempfile.mkstemp(prefix="charon-test-config", suffix=".yaml")
66+
overwrite_file(tmp_config_file, config_content)
67+
conf = config.get_config(tmp_config_file)
68+
self.assertIsNotNone(conf)
69+
self.assertEqual("changed-bucket", conf.get_target("changed")[0].get('bucket', ''))
70+
self.assertEqual("changed-prefix", conf.get_target("changed")[0].get('prefix', ''))
71+
self.assertEqual([], conf.get_target("ga"))
72+
os.remove(tmp_config_file)
73+
5274
def test_config_missing_targets(self):
5375
content_missing_targets = """
5476
ignore_patterns:

0 commit comments

Comments
 (0)