From 4a0397b9a20e9995c0ec21785a0debfd41026234 Mon Sep 17 00:00:00 2001
From: Kai Chen <kaichen120@gmail.com>
Date: Thu, 8 Nov 2018 02:04:12 -0800
Subject: [PATCH] Add support to export list of org repo

Add option to export list of org repos into different formats.

Closes https://github.com/ksdme/org-status/issues/6
---
 org_status/export/Exporter.py | 15 ++++++++++++++
 org_status/export/__init__.py |  0
 org_status/export/gitman.py   | 37 +++++++++++++++++++++++++++++++++++
 org_status/org_status.py      | 18 +++++++++++++++++
 requirements.txt              |  1 +
 5 files changed, 71 insertions(+)
 create mode 100644 org_status/export/Exporter.py
 create mode 100644 org_status/export/__init__.py
 create mode 100644 org_status/export/gitman.py

diff --git a/org_status/export/Exporter.py b/org_status/export/Exporter.py
new file mode 100644
index 0000000..59331e3
--- /dev/null
+++ b/org_status/export/Exporter.py
@@ -0,0 +1,15 @@
+class Exporter:
+    NAME = None
+    OUTPUT_FILE = None
+
+    def save_org_repos_to_file(self, repos):
+        raise NotImplementedError()
+
+    def use_org_list_to_check_status(self):
+        raise NotImplementedError()
+
+
+def get_all_supported_exporters():
+    from org_status.export.gitman import GitManExporter
+
+    return [GitManExporter]
diff --git a/org_status/export/__init__.py b/org_status/export/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/org_status/export/gitman.py b/org_status/export/gitman.py
new file mode 100644
index 0000000..b05148e
--- /dev/null
+++ b/org_status/export/gitman.py
@@ -0,0 +1,37 @@
+import os
+
+from furl import furl
+import yaml
+
+from org_status.export.Exporter import Exporter
+
+
+class GitManExporter(Exporter):
+    NAME = 'gitman'
+    OUTPUT_FILE = f'{os.getcwd()}/gitman.yml'
+
+    @classmethod
+    def save_org_repos_to_file(cls, repos):
+        repo_data = []
+
+        for repo in repos:
+            name = furl(repo.repo_url).path.segments[1]
+            repo_data.append({'name': name,
+                              'repo': repo.repo_url,
+                              'rev': 'master'})
+
+        with open(cls.OUTPUT_FILE, 'r') as yml:
+            config_yml = yaml.load(yml)
+
+        with open(cls.OUTPUT_FILE, 'w') as yml:
+            if config_yml['sources'] is None:
+                config_yml['sources'] = []
+
+            for repo in repo_data:
+                config_yml['sources'].append(repo)
+
+            yaml.dump(config_yml, yml, default_flow_style=False)
+
+    @classmethod
+    def use_org_list_to_check_status(cls):
+        pass
diff --git a/org_status/org_status.py b/org_status/org_status.py
index 8b42510..5b341bf 100644
--- a/org_status/org_status.py
+++ b/org_status/org_status.py
@@ -6,6 +6,7 @@
 
 from org_status.status_providers import Status
 from org_status.org_hosts import get_all_supported_hosts
+from org_status.export.Exporter import get_all_supported_exporters
 
 
 def get_host_token(host_name):
@@ -75,10 +76,24 @@ def get_argument_parser():
     parser.add_argument('--verbose', '-v', action='store_true')
     parser.add_argument('--hosts-only', '-o', action='store_true')
     parser.add_argument('--skip-host-checks', action='store_true')
+    parser.add_argument('--exporter', type=str)
 
     return parser
 
 
+def export(repo_data, exporter_name):
+    exporters = get_all_supported_exporters()
+
+    for exporter in exporters:
+        if exporter.NAME == exporter_name:
+            try:
+                exporter.save_org_repos_to_file(repo_data)
+            except NotImplementedError:
+                print('exporter does not support exporting results')
+        else:
+            print('exporter format not found')
+
+
 def main():
     parser = get_argument_parser()
     args = parser.parse_args()
@@ -137,4 +152,7 @@ def main():
 
         org_host = Host(token, org, verbose=args.verbose)
         org_status = aggregate_org_status(org_host, threads=args.threads)
+
+        export(org_status, args.exporter)
+
         present_status(org_status, args.no_color)
diff --git a/requirements.txt b/requirements.txt
index 78da91f..53fceeb 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
 git+https://gitlab.com/gitmate/open-source/IGitt.git#egg=IGitt
 requests
 termcolor
+furl