feat(shell): add storage shell

This commit is contained in:
bandl 2021-09-28 22:43:11 +08:00
parent ad0cdedba9
commit 187280bc2c
1 changed files with 29 additions and 2 deletions

View File

@ -7,6 +7,7 @@ sysPath = os.getcwd()
tempPath = f"{sysPath}/pkg/structure/generate"
structurePath = f"{sysPath}/pkg/structure"
protobufPath = f"{sysPath}/protobuf"
storagePath = f"{sysPath}/storage"
class KeyMap(object):
@ -24,6 +25,15 @@ def to_camel(val: str) -> str:
return "".join([k.capitalize() for k in val.split('_')])
class KeyOption(object):
def __init__(self, key, option):
self.key = to_camel(key)
self.option = option
def __str__(self):
return "{}:{}".format(self.key, self.option)
def load_template(name: str) -> str:
with open(f"{tempPath}/{name}", "r", encoding="utf-8") as fp:
return fp.read()
@ -59,7 +69,7 @@ def set_structure_const_template(conf: dict):
template = Template(tem_text)
text = template.render(keys=keys, key_maps=key_map, sets=val_set)
temp_path = f"{tempPath}/structure.gen.go"
temp_path = f"{structurePath}/const.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
@ -79,14 +89,31 @@ def set_structure_interface(conf):
f.write(text)
def set_storage_server(server_conf):
mod_name = [i.replace("_", "").lower() for i in server_conf.keys()]
option = []
for key, item in server_conf.items():
option.append(KeyOption(key, item))
text = load_template("storage.template")
template = Template(text)
text = template.render(option=option, mod_name=mod_name)
temp_path = f"{storagePath}/server/single.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
def format_code_go():
go_fmt(f"{structurePath}/interface.gen.go")
go_fmt(f"{structurePath}/generate/structure.gen.go")
go_fmt(f"{structurePath}/const.gen.go")
go_fmt(f"{storagePath}/server/*.go")
if __name__ == "__main__":
conf, cfg_camel = load_conf()
set_structure_const_template(conf)
set_structure_interface(cfg_camel)
set_storage_server(cfg_camel)
# 格式化代码
format_code_go()