kiwi-scp/src/kiwi/config.py

59 lines
1.5 KiB
Python
Raw Normal View History

2020-08-04 18:24:19 +00:00
import os
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', '.')
__ymlContent = None
def __init__(self):
2020-08-04 14:52:30 +00:00
2020-08-05 00:08:55 +00:00
@classmethod
def __from_file(cls, filename):
2020-08-04 14:52:30 +00:00
with open(filename, 'r') as stream:
try:
2020-08-05 00:08:55 +00:00
self.__ymlContent = yaml.safe_load(stream)
2020-08-04 18:24:19 +00:00
2020-08-04 14:52:30 +00:00
except yaml.YAMLError as exc:
print(exc)
@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:
result.__ymlContent["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-05 00:08:55 +00:00
def __user_input(self, key, prompt):
""""""
2020-08-04 18:24:19 +00:00
2020-08-05 00:08:55 +00:00
# "a:b:c" => path = ['a', 'b'], key = 'c'
path = key.split(':')
(path, key) = (path[:-1], path[-1])
2020-08-04 18:24:19 +00:00
2020-08-05 00:08:55 +00:00
# resolve path
content = self.__ymlContent
2020-08-04 18:24:19 +00:00
for step in path:
content = content[step]
2020-08-05 00:08:55 +00:00
# prompt user as per argument
result = input("{} [Default: {}] ".format(prompt, content[key])).strip()
2020-08-04 18:24:19 +00:00
2020-08-05 00:08:55 +00:00
#
2020-08-04 18:24:19 +00:00
if result:
content[key] = result
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-05 00:08:55 +00:00
self.__user_input("suffixes:project", "Enter suffix for project directories")
self.__user_input("suffixes:down", "Enter suffix for disabled projects")
2020-08-04 18:24:19 +00:00
2020-08-05 00:08:55 +00:00
self.__user_input("network:name", "Enter name for docker network")
self.__user_input("network:cidr", "Enter ")
2020-08-04 18:24:19 +00:00
2020-08-05 00:08:55 +00:00
self.__user_input("storage:location", "Enter ")