Browse Source

add more cli support

Make it possible to create a blank local.conf
Sean Dague 2 years ago
parent
commit
be8cb74021
2 changed files with 75 additions and 4 deletions
  1. 62
    4
      devstack/cmd.py
  2. 13
    0
      devstack/dsconf.py

+ 62
- 4
devstack/cmd.py View File

@@ -34,36 +34,92 @@ def iniuncomment(inifile, args):
34 34
     inifile.uncomment(args.section, args.name)
35 35
 
36 36
 
37
+def extract_local(local_conf, args):
38
+    local_conf.extract_localrc(args.local_rc)
39
+
40
+
41
+def extract_config(local_conf, args):
42
+    local_conf.extract(args.group, args.conf, args.local_rc)
43
+
44
+
45
+def setlc(local_conf, args):
46
+    local_conf.set_local(args.name, args.value)
47
+
48
+
49
+def setlc_conf(local_conf, args):
50
+    local_conf.set(args.group, args.conf, args.section, args.name, args.value)
51
+
52
+
37 53
 def parse_args(argv):
38 54
     parser = argparse.ArgumentParser(prog='dsconf')
39 55
     subparsers = parser.add_subparsers(title='commands',
40 56
                                        help='sub-command help')
41 57
 
42
-    parser_iniset = subparsers.add_parser('iniset', help='iniset help')
58
+    parser_iniset = subparsers.add_parser('iniset',
59
+                                          help='set item in ini file')
43 60
     parser_iniset.set_defaults(func=iniset)
44 61
     parser_iniset.add_argument('inifile', help='name of file')
45 62
     parser_iniset.add_argument('section', help='name of section')
46 63
     parser_iniset.add_argument('name', help='name')
47 64
     parser_iniset.add_argument('value', help='value')
48 65
 
49
-    parser_inicomment = subparsers.add_parser('inicomment', help='inicomment help')
66
+    parser_inicomment = subparsers.add_parser(
67
+        'inicomment',
68
+        help='comment item in ini file')
50 69
     parser_inicomment.set_defaults(func=inicomment)
51 70
     parser_inicomment.add_argument('inifile', help='name of file')
52 71
     parser_inicomment.add_argument('section', help='name of section')
53 72
     parser_inicomment.add_argument('name', help='name')
54 73
 
55
-    parser_iniuncomment = subparsers.add_parser('iniuncomment', help='iniuncomment help')
74
+    parser_iniuncomment = subparsers.add_parser(
75
+        'iniuncomment',
76
+        help='uncomment item in ini file')
56 77
     parser_iniuncomment.set_defaults(func=iniuncomment)
57 78
     parser_iniuncomment.add_argument('inifile', help='name of file')
58 79
     parser_iniuncomment.add_argument('section', help='name of section')
59 80
     parser_iniuncomment.add_argument('name', help='name')
60 81
 
61
-    parser_inirm = subparsers.add_parser('inirm', help='inirm help')
82
+    parser_inirm = subparsers.add_parser(
83
+        'inirm',
84
+        help='delete item from ini file')
62 85
     parser_inirm.set_defaults(func=inirm)
63 86
     parser_inirm.add_argument('inifile', help='name of file')
64 87
     parser_inirm.add_argument('section', help='name of section')
65 88
     parser_inirm.add_argument('name', help='name')
66 89
 
90
+    parser_extract_local = subparsers.add_parser(
91
+        'extract-localrc',
92
+        help='extract localrc from local.conf')
93
+    parser_extract_local.set_defaults(func=extract_local)
94
+    parser_extract_local.add_argument('local_conf')
95
+    parser_extract_local.add_argument('local_rc')
96
+
97
+    parser_extract = subparsers.add_parser(
98
+        'extract',
99
+        help='extract and merge config from local.conf')
100
+    parser_extract.set_defaults(func=extract_config)
101
+    parser_extract.add_argument('local_conf')
102
+    parser_extract.add_argument('group')
103
+    parser_extract.add_argument('conf')
104
+    parser_extract.add_argument('local_rc')
105
+
106
+    parser_setlc = subparsers.add_parser(
107
+        'setlc', help='set variable in localrc of local.conf')
108
+    parser_setlc.set_defaults(func=setlc)
109
+    parser_setlc.add_argument('local_conf')
110
+    parser_setlc.add_argument('name')
111
+    parser_setlc.add_argument('value')
112
+
113
+    parser_setlc_conf = subparsers.add_parser(
114
+        'setlc_conf', help='set variable in localrc of local.conf')
115
+    parser_setlc_conf.set_defaults(func=setlc_conf)
116
+    parser_setlc_conf.add_argument('local_conf')
117
+    parser_setlc_conf.add_argument('group')
118
+    parser_setlc_conf.add_argument('conf')
119
+    parser_setlc_conf.add_argument('section')
120
+    parser_setlc_conf.add_argument('name')
121
+    parser_setlc_conf.add_argument('value')
122
+
67 123
     return parser.parse_args()
68 124
 
69 125
 
@@ -71,5 +127,7 @@ def main(argv=None):
71 127
     args = parse_args(argv or sys.argv)
72 128
     if hasattr(args, 'inifile'):
73 129
         f = devstack.dsconf.IniFile(args.inifile)
130
+    elif hasattr(args, 'local_conf'):
131
+        f = devstack.dsconf.LocalConf(args.local_conf)
74 132
     args.func(f, args)
75 133
     return

+ 13
- 0
devstack/dsconf.py View File

@@ -208,6 +208,12 @@ class LocalConf(object):
208 208
                 func(writer, None)
209 209
 
210 210
     def set_local(self, name, value):
211
+        if not os.path.exists(self.fname):
212
+            with open(self.fname, "w+") as writer:
213
+                writer.write("[[local|localrc]]\n")
214
+                writer.write("%s = %s\n" % (name, value))
215
+                return
216
+
211 217
         def _do_set(writer, line):
212 218
             writer.write("%s = %s\n" % (name, value))
213 219
         self._at_insert_point_local(name, _do_set)
@@ -267,6 +273,13 @@ class LocalConf(object):
267 273
                 func(writer, None)
268 274
 
269 275
     def set(self, group, conf, section, name, value):
276
+        if not os.path.exists(self.fname):
277
+            with open(self.fname, "w+") as writer:
278
+                writer.write("[[%s|%s]]\n" % (group, conf))
279
+                writer.write("[%s]\n" % section)
280
+                writer.write("%s = %s\n" % (name, value))
281
+                return
282
+
270 283
         def _do_set(writer, line):
271 284
             writer.write("%s = %s\n" % (name, value))
272 285
         self._at_insert_point(group, conf, section, name, _do_set)

Loading…
Cancel
Save