This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.py
executable file
·60 lines (45 loc) · 2.21 KB
/
lint.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
#!/usr/bin/env python3
import sys
import ruamel.yaml
import rules
def log_info(document_source, document_source_index, message):
print("[info] [{}@{}]: {}".format(document_source, document_source_index, message))
def log_error(document_source, document_source_index, message):
print("[error] [{}@{}]: {}".format(document_source, document_source_index, message))
def log_warning(document_source, document_source_index, message):
print("[warning] [{}@{}]: {}".format(document_source, document_source_index, message))
def lint_document(document_source, document_source_index, document):
if "apiVersion" not in document:
log_info(document_source, document_source_index, "document not recognized as an ArgoCD Application")
return
if document["apiVersion"] != "argoproj.io/v1alpha1":
log_info(document_source, document_source_index, "document not recognized as an ArgoCD Application")
return
if "kind" not in document:
log_info(document_source, document_source_index, "document not recognized as an ArgoCD Application")
return
if document["kind"] != "Application":
log_info(document_source, document_source_index, "document not recognized as an ArgoCD Application")
return
log_info(document_source, document_source_index, "document is an ArgoCD Application")
queue = []
queue.extend(rules.top)
while len(queue) > 0:
rule = queue.pop()
try:
log_info(document_source, document_source_index, "running rule {}".format(rule.__name__))
continuations = rule(document)
queue = continuations + queue
except Exception as e:
log_error(document_source, document_source_index, "failed rule {}: {}".format(rule.__name__, e))
def lint_file(yaml_source):
yaml_stream = sys.stdin if yaml_source == '-' else open(yaml_source)
with yaml_stream:
documents = ruamel.yaml.load_all(yaml_stream, Loader=ruamel.yaml.RoundTripLoader)
for document_index, document in enumerate(documents):
lint_document(yaml_source, document_index, document)
def main():
for yaml_source in sys.argv[1:]:
lint_file(yaml_source)
if __name__ == '__main__':
main()