2020-08-04 18:24:19 +00:00
|
|
|
import os
|
2020-08-06 01:45:12 +00:00
|
|
|
import re
|
2020-08-04 18:24:19 +00:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
2020-08-04 14:52:30 +00:00
|
|
|
class Config:
|
2020-08-05 00:08:55 +00:00
|
|
|
KIWI_ROOT = os.getenv('KIWI_ROOT', '.')
|
2020-08-06 01:45:12 +00:00
|
|
|
__yml_content = None
|
2020-08-04 14:52:30 +00:00
|
|
|
|
2020-08-05 00:08:55 +00:00
|
|
|
@classmethod
|
|
|
|
def __from_file(cls, filename):
|
2020-08-06 01:45:12 +00:00
|
|
|
result = cls()
|
|
|
|
|
2020-08-04 14:52:30 +00:00
|
|
|
with open(filename, 'r') as stream:
|
|
|
|
try:
|
2020-08-06 01:45:12 +00:00
|
|
|
result.__yml_content = yaml.safe_load(stream)
|
2020-08-04 14:52:30 +00:00
|
|
|
except yaml.YAMLError as exc:
|
|
|
|
print(exc)
|
|
|
|
|
2020-08-06 01:45:12 +00:00
|
|
|
return result
|
|
|
|
|
2020-08-04 14:52:30 +00:00
|
|
|
@classmethod
|
|
|
|
def default(cls):
|
2020-08-05 00:08:55 +00:00
|
|
|
result = cls.__from_file(cls.KIWI_ROOT + "/default.kiwi.yml")
|
|
|
|
|
|
|
|
with open(cls.KIWI_ROOT + "/version-tag", 'r') as stream:
|
2020-08-06 01:45:12 +00:00
|
|
|
result.__yml_content["version"] = stream.read().strip()
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-05 00:08:55 +00:00
|
|
|
return result
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-06 01:45:12 +00:00
|
|
|
def __yml_resolve(self, key):
|
2020-08-05 00:08:55 +00:00
|
|
|
# "a:b:c" => path = ['a', 'b'], key = 'c'
|
|
|
|
path = key.split(':')
|
2020-08-06 01:45:12 +00:00
|
|
|
path, key = path[:-1], path[-1]
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-05 00:08:55 +00:00
|
|
|
# resolve path
|
2020-08-06 01:45:12 +00:00
|
|
|
content = self.__yml_content
|
2020-08-04 18:24:19 +00:00
|
|
|
for step in path:
|
|
|
|
content = content[step]
|
|
|
|
|
2020-08-06 01:45:12 +00:00
|
|
|
return content, key
|
|
|
|
|
|
|
|
def __yml_get(self, key):
|
|
|
|
content, key = self.__yml_resolve(key)
|
|
|
|
return content[key]
|
|
|
|
|
|
|
|
def __yml_set(self, key, value):
|
|
|
|
content, key = self.__yml_resolve(key)
|
|
|
|
content[key] = value
|
|
|
|
|
|
|
|
def __user_input(self, key, prompt):
|
2020-08-05 00:08:55 +00:00
|
|
|
# prompt user as per argument
|
2020-08-06 01:45:12 +00:00
|
|
|
result = input("{} [Current: {}] ".format(prompt, self.__yml_get(key))).strip()
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-06 01:45:12 +00:00
|
|
|
# store result if present
|
2020-08-04 18:24:19 +00:00
|
|
|
if result:
|
2020-08-06 01:45:12 +00:00
|
|
|
self.__yml_set(key, result)
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-05 00:08:55 +00:00
|
|
|
def user_input(self):
|
|
|
|
self.__user_input("version", "Choose kiwi-config version")
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-06 01:45:12 +00:00
|
|
|
self.__user_input("markers:project", "Enter marker string for project directories")
|
|
|
|
self.__user_input("markers:down", "Enter marker string for disabled projects")
|
|
|
|
|
|
|
|
self.__user_input("network:name", "Enter name for local docker network")
|
|
|
|
self.__user_input("network:cidr", "Enter CIDR block for local docker network")
|
|
|
|
|
|
|
|
self.__user_input("runtime:storage", "Enter main directory for local data")
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
yml_string = yaml.dump(self.__yml_content, default_flow_style=False, sort_keys=False)
|
|
|
|
yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE)
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-06 01:45:12 +00:00
|
|
|
with open(Config.KIWI_ROOT + "/default.kiwi.yml", 'r') as stream:
|
|
|
|
yml_header = stream.read().strip()
|
|
|
|
yml_header = re.sub(r'^[^#].*', r'', yml_header, flags=re.MULTILINE).strip()
|
|
|
|
yml_string = "{}\n{}".format(yml_header, yml_string)
|
2020-08-04 18:24:19 +00:00
|
|
|
|
2020-08-06 01:45:12 +00:00
|
|
|
print(yml_string)
|