Преглед изворни кода

#6529 - Adjusted the arguments. Added documentation

Daniel Sheppard пре 4 година
родитељ
комит
7c3318df92
2 измењених фајлова са 19 додато и 5 уклоњено
  1. 14 0
      docs/customization/custom-scripts.md
  2. 5 5
      netbox/extras/management/commands/runscript.py

+ 14 - 0
docs/customization/custom-scripts.md

@@ -259,6 +259,20 @@ http://netbox/api/extras/scripts/example.MyReport/ \
 --data '{"data": {"foo": "somevalue", "bar": 123}, "commit": true}'
 ```
 
+### Via the CLI
+
+Scripts can be run on the CLI by invoking the management command:
+
+```
+python3 manage.py runscript [--commit] [--loglevel {debug,info,warning,error,critical}] --script <module>.<script> <data>
+```
+
+The required ``--script <module>.<script>`` argument is the report to run where ``<module>`` is the name of the python file in the ``scripts`` directory without the ``.py`` extension and ``<script>`` is the name of the script class in the ``<module>`` to run.
+
+The optional ``--loglevel`` argument is the desired logging level to output to the console.
+
+The optional ``--commit`` argument will commit any changes in the script to the database.
+
 ## Example
 
 Below is an example script that creates new objects for a planned site. The user is prompted for three variables:

+ 5 - 5
netbox/extras/management/commands/runscript.py

@@ -28,10 +28,10 @@ class Command(BaseCommand):
             dest='loglevel',
             default='info',
             choices=['debug', 'info', 'warning', 'error', 'critical'])
-        parser.add_argument('--script', help="Script to run", dest='script', required=True)
-        parser.add_argument('--commit', help="Commit this script to database", dest='commit')
-        parser.add_argument('--user', help="User script is running as", dest='user')
-        parser.add_argument('data', help="Data as a JSON blob")
+        parser.add_argument('--script', help="Script to run", required=True)
+        parser.add_argument('--commit', help="Commit this script to database", action='store_true')
+        parser.add_argument('--user', help="User script is running as")
+        parser.add_argument('data', help="Data as a string encapsulated JSON blob")
 
     def handle(self, *args, **options):
         def _run_script():
@@ -68,8 +68,8 @@ class Command(BaseCommand):
         # Params
         script = options['script']
         loglevel = options['loglevel']
+        commit = options['commit']
         data = json.loads(options['data']) if options['data'] is not None else None
-        commit = True if options['commit'] in ['1', 'true', 'True'] else False
 
         module, name = script.split('.', 1)