1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 04:43:00 +00:00

write out kiwi.yml

This commit is contained in:
Jörn-Michael Miehe 2020-08-06 03:45:12 +02:00
parent 9444597aae
commit e44c0a86f8
3 changed files with 55 additions and 25 deletions

View file

@ -1,9 +1,17 @@
######################################
# kiwi-config instance configuration #
######################################
version: version:
suffixes:
markers:
project: .project project: .project
down: .down down: .down
network: network:
name: kiwinet name: kiwinet
cidr: 10.22.46.0/24 cidr: 10.22.46.0/24
storage:
location: /var/kiwi runtime:
storage: /var/kiwi
env: null

View file

@ -20,7 +20,8 @@ def main():
print(args.command) print(args.command)
cf = Config.default() cf = Config.default()
cf.user_input() # cf.user_input()
cf.dump()
pass pass

View file

@ -1,58 +1,79 @@
import os import os
import re
import yaml import yaml
class Config: class Config:
KIWI_ROOT = os.getenv('KIWI_ROOT', '.') KIWI_ROOT = os.getenv('KIWI_ROOT', '.')
__ymlContent = None __yml_content = None
def __init__(self):
@classmethod @classmethod
def __from_file(cls, filename): def __from_file(cls, filename):
result = cls()
with open(filename, 'r') as stream: with open(filename, 'r') as stream:
try: try:
self.__ymlContent = yaml.safe_load(stream) result.__yml_content = yaml.safe_load(stream)
except yaml.YAMLError as exc: except yaml.YAMLError as exc:
print(exc) print(exc)
return result
@classmethod @classmethod
def default(cls): def default(cls):
result = cls.__from_file(cls.KIWI_ROOT + "/default.kiwi.yml") result = cls.__from_file(cls.KIWI_ROOT + "/default.kiwi.yml")
with open(cls.KIWI_ROOT + "/version-tag", 'r') as stream: 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 return result
def __user_input(self, key, prompt): def __yml_resolve(self, key):
""""""
# "a:b:c" => path = ['a', 'b'], key = 'c' # "a:b:c" => path = ['a', 'b'], key = 'c'
path = key.split(':') path = key.split(':')
(path, key) = (path[:-1], path[-1]) path, key = path[:-1], path[-1]
# resolve path # resolve path
content = self.__ymlContent content = self.__yml_content
for step in path: for step in path:
content = content[step] content = content[step]
# prompt user as per argument return content, key
result = input("{} [Default: {}] ".format(prompt, content[key])).strip()
# 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: if result:
content[key] = result self.__yml_set(key, result)
def user_input(self): def user_input(self):
self.__user_input("version", "Choose kiwi-config version") self.__user_input("version", "Choose kiwi-config version")
self.__user_input("suffixes:project", "Enter suffix for project directories") self.__user_input("markers:project", "Enter marker string for project directories")
self.__user_input("suffixes:down", "Enter suffix for disabled projects") 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:name", "Enter name for local docker network")
self.__user_input("network:cidr", "Enter ") 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)