diff --git a/src/default.kiwi.yml b/src/default.kiwi.yml index b1edc12..0001927 100644 --- a/src/default.kiwi.yml +++ b/src/default.kiwi.yml @@ -1,9 +1,17 @@ +###################################### +# kiwi-config instance configuration # +###################################### + version: -suffixes: + +markers: project: .project down: .down + network: name: kiwinet cidr: 10.22.46.0/24 -storage: - location: /var/kiwi + +runtime: + storage: /var/kiwi + env: null \ No newline at end of file diff --git a/src/kiwi-config.py b/src/kiwi-config.py index ab1183c..e4c41b7 100755 --- a/src/kiwi-config.py +++ b/src/kiwi-config.py @@ -20,7 +20,8 @@ def main(): print(args.command) cf = Config.default() - cf.user_input() + # cf.user_input() + cf.dump() pass diff --git a/src/kiwi/config.py b/src/kiwi/config.py index 3ba490e..36321aa 100644 --- a/src/kiwi/config.py +++ b/src/kiwi/config.py @@ -1,58 +1,79 @@ import os +import re import yaml class Config: KIWI_ROOT = os.getenv('KIWI_ROOT', '.') - __ymlContent = None - - def __init__(self): - + __yml_content = None @classmethod def __from_file(cls, filename): + result = cls() + with open(filename, 'r') as stream: try: - self.__ymlContent = yaml.safe_load(stream) - + result.__yml_content = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) + return result + @classmethod def default(cls): result = cls.__from_file(cls.KIWI_ROOT + "/default.kiwi.yml") with open(cls.KIWI_ROOT + "/version-tag", 'r') as stream: - result.__ymlContent["version"] = stream.read().strip() + result.__yml_content["version"] = stream.read().strip() return result - def __user_input(self, key, prompt): - """""" - + def __yml_resolve(self, key): # "a:b:c" => path = ['a', 'b'], key = 'c' path = key.split(':') - (path, key) = (path[:-1], path[-1]) + path, key = path[:-1], path[-1] # resolve path - content = self.__ymlContent + content = self.__yml_content for step in path: content = content[step] - # prompt user as per argument - result = input("{} [Default: {}] ".format(prompt, content[key])).strip() + 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): + # prompt user as per argument + result = input("{} [Current: {}] ".format(prompt, self.__yml_get(key))).strip() + + # store result if present if result: - content[key] = result + self.__yml_set(key, result) def user_input(self): self.__user_input("version", "Choose kiwi-config version") - self.__user_input("suffixes:project", "Enter suffix for project directories") - self.__user_input("suffixes:down", "Enter suffix for disabled projects") + 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 docker network") - self.__user_input("network:cidr", "Enter ") + 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("storage:location", "Enter ") + 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) + + 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) + + print(yml_string)