add more cli support
Make it possible to create a blank local.conf
This commit is contained in:
parent
36b1e0ff34
commit
be8cb74021
|
@ -34,36 +34,92 @@ def iniuncomment(inifile, args):
|
||||||
inifile.uncomment(args.section, args.name)
|
inifile.uncomment(args.section, args.name)
|
||||||
|
|
||||||
|
|
||||||
|
def extract_local(local_conf, args):
|
||||||
|
local_conf.extract_localrc(args.local_rc)
|
||||||
|
|
||||||
|
|
||||||
|
def extract_config(local_conf, args):
|
||||||
|
local_conf.extract(args.group, args.conf, args.local_rc)
|
||||||
|
|
||||||
|
|
||||||
|
def setlc(local_conf, args):
|
||||||
|
local_conf.set_local(args.name, args.value)
|
||||||
|
|
||||||
|
|
||||||
|
def setlc_conf(local_conf, args):
|
||||||
|
local_conf.set(args.group, args.conf, args.section, args.name, args.value)
|
||||||
|
|
||||||
|
|
||||||
def parse_args(argv):
|
def parse_args(argv):
|
||||||
parser = argparse.ArgumentParser(prog='dsconf')
|
parser = argparse.ArgumentParser(prog='dsconf')
|
||||||
subparsers = parser.add_subparsers(title='commands',
|
subparsers = parser.add_subparsers(title='commands',
|
||||||
help='sub-command help')
|
help='sub-command help')
|
||||||
|
|
||||||
parser_iniset = subparsers.add_parser('iniset', help='iniset help')
|
parser_iniset = subparsers.add_parser('iniset',
|
||||||
|
help='set item in ini file')
|
||||||
parser_iniset.set_defaults(func=iniset)
|
parser_iniset.set_defaults(func=iniset)
|
||||||
parser_iniset.add_argument('inifile', help='name of file')
|
parser_iniset.add_argument('inifile', help='name of file')
|
||||||
parser_iniset.add_argument('section', help='name of section')
|
parser_iniset.add_argument('section', help='name of section')
|
||||||
parser_iniset.add_argument('name', help='name')
|
parser_iniset.add_argument('name', help='name')
|
||||||
parser_iniset.add_argument('value', help='value')
|
parser_iniset.add_argument('value', help='value')
|
||||||
|
|
||||||
parser_inicomment = subparsers.add_parser('inicomment', help='inicomment help')
|
parser_inicomment = subparsers.add_parser(
|
||||||
|
'inicomment',
|
||||||
|
help='comment item in ini file')
|
||||||
parser_inicomment.set_defaults(func=inicomment)
|
parser_inicomment.set_defaults(func=inicomment)
|
||||||
parser_inicomment.add_argument('inifile', help='name of file')
|
parser_inicomment.add_argument('inifile', help='name of file')
|
||||||
parser_inicomment.add_argument('section', help='name of section')
|
parser_inicomment.add_argument('section', help='name of section')
|
||||||
parser_inicomment.add_argument('name', help='name')
|
parser_inicomment.add_argument('name', help='name')
|
||||||
|
|
||||||
parser_iniuncomment = subparsers.add_parser('iniuncomment', help='iniuncomment help')
|
parser_iniuncomment = subparsers.add_parser(
|
||||||
|
'iniuncomment',
|
||||||
|
help='uncomment item in ini file')
|
||||||
parser_iniuncomment.set_defaults(func=iniuncomment)
|
parser_iniuncomment.set_defaults(func=iniuncomment)
|
||||||
parser_iniuncomment.add_argument('inifile', help='name of file')
|
parser_iniuncomment.add_argument('inifile', help='name of file')
|
||||||
parser_iniuncomment.add_argument('section', help='name of section')
|
parser_iniuncomment.add_argument('section', help='name of section')
|
||||||
parser_iniuncomment.add_argument('name', help='name')
|
parser_iniuncomment.add_argument('name', help='name')
|
||||||
|
|
||||||
parser_inirm = subparsers.add_parser('inirm', help='inirm help')
|
parser_inirm = subparsers.add_parser(
|
||||||
|
'inirm',
|
||||||
|
help='delete item from ini file')
|
||||||
parser_inirm.set_defaults(func=inirm)
|
parser_inirm.set_defaults(func=inirm)
|
||||||
parser_inirm.add_argument('inifile', help='name of file')
|
parser_inirm.add_argument('inifile', help='name of file')
|
||||||
parser_inirm.add_argument('section', help='name of section')
|
parser_inirm.add_argument('section', help='name of section')
|
||||||
parser_inirm.add_argument('name', help='name')
|
parser_inirm.add_argument('name', help='name')
|
||||||
|
|
||||||
|
parser_extract_local = subparsers.add_parser(
|
||||||
|
'extract-localrc',
|
||||||
|
help='extract localrc from local.conf')
|
||||||
|
parser_extract_local.set_defaults(func=extract_local)
|
||||||
|
parser_extract_local.add_argument('local_conf')
|
||||||
|
parser_extract_local.add_argument('local_rc')
|
||||||
|
|
||||||
|
parser_extract = subparsers.add_parser(
|
||||||
|
'extract',
|
||||||
|
help='extract and merge config from local.conf')
|
||||||
|
parser_extract.set_defaults(func=extract_config)
|
||||||
|
parser_extract.add_argument('local_conf')
|
||||||
|
parser_extract.add_argument('group')
|
||||||
|
parser_extract.add_argument('conf')
|
||||||
|
parser_extract.add_argument('local_rc')
|
||||||
|
|
||||||
|
parser_setlc = subparsers.add_parser(
|
||||||
|
'setlc', help='set variable in localrc of local.conf')
|
||||||
|
parser_setlc.set_defaults(func=setlc)
|
||||||
|
parser_setlc.add_argument('local_conf')
|
||||||
|
parser_setlc.add_argument('name')
|
||||||
|
parser_setlc.add_argument('value')
|
||||||
|
|
||||||
|
parser_setlc_conf = subparsers.add_parser(
|
||||||
|
'setlc_conf', help='set variable in localrc of local.conf')
|
||||||
|
parser_setlc_conf.set_defaults(func=setlc_conf)
|
||||||
|
parser_setlc_conf.add_argument('local_conf')
|
||||||
|
parser_setlc_conf.add_argument('group')
|
||||||
|
parser_setlc_conf.add_argument('conf')
|
||||||
|
parser_setlc_conf.add_argument('section')
|
||||||
|
parser_setlc_conf.add_argument('name')
|
||||||
|
parser_setlc_conf.add_argument('value')
|
||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,5 +127,7 @@ def main(argv=None):
|
||||||
args = parse_args(argv or sys.argv)
|
args = parse_args(argv or sys.argv)
|
||||||
if hasattr(args, 'inifile'):
|
if hasattr(args, 'inifile'):
|
||||||
f = devstack.dsconf.IniFile(args.inifile)
|
f = devstack.dsconf.IniFile(args.inifile)
|
||||||
|
elif hasattr(args, 'local_conf'):
|
||||||
|
f = devstack.dsconf.LocalConf(args.local_conf)
|
||||||
args.func(f, args)
|
args.func(f, args)
|
||||||
return
|
return
|
||||||
|
|
|
@ -208,6 +208,12 @@ class LocalConf(object):
|
||||||
func(writer, None)
|
func(writer, None)
|
||||||
|
|
||||||
def set_local(self, name, value):
|
def set_local(self, name, value):
|
||||||
|
if not os.path.exists(self.fname):
|
||||||
|
with open(self.fname, "w+") as writer:
|
||||||
|
writer.write("[[local|localrc]]\n")
|
||||||
|
writer.write("%s = %s\n" % (name, value))
|
||||||
|
return
|
||||||
|
|
||||||
def _do_set(writer, line):
|
def _do_set(writer, line):
|
||||||
writer.write("%s = %s\n" % (name, value))
|
writer.write("%s = %s\n" % (name, value))
|
||||||
self._at_insert_point_local(name, _do_set)
|
self._at_insert_point_local(name, _do_set)
|
||||||
|
@ -267,6 +273,13 @@ class LocalConf(object):
|
||||||
func(writer, None)
|
func(writer, None)
|
||||||
|
|
||||||
def set(self, group, conf, section, name, value):
|
def set(self, group, conf, section, name, value):
|
||||||
|
if not os.path.exists(self.fname):
|
||||||
|
with open(self.fname, "w+") as writer:
|
||||||
|
writer.write("[[%s|%s]]\n" % (group, conf))
|
||||||
|
writer.write("[%s]\n" % section)
|
||||||
|
writer.write("%s = %s\n" % (name, value))
|
||||||
|
return
|
||||||
|
|
||||||
def _do_set(writer, line):
|
def _do_set(writer, line):
|
||||||
writer.write("%s = %s\n" % (name, value))
|
writer.write("%s = %s\n" % (name, value))
|
||||||
self._at_insert_point(group, conf, section, name, _do_set)
|
self._at_insert_point(group, conf, section, name, _do_set)
|
||||||
|
|
Loading…
Reference in New Issue