From a682875c6aa8f5ad8f1ff8c1cfe7b18335883298 Mon Sep 17 00:00:00 2001
From: Wojciech Danilo <wojtek.danilo@gmail.com>
Date: Wed, 4 Dec 2013 01:55:53 +0100
Subject: [PATCH] Refactoring: extracting 'List.getPkgList' function out of
 'List.list' function. The 'getPkgList' function returns list of packages and
 be used by external programs, when cabal-install becomes a library.

---
 cabal-install/Distribution/Client/List.hs | 41 +++++++++++++++++------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/cabal-install/Distribution/Client/List.hs b/cabal-install/Distribution/Client/List.hs
index 269f4baa87..acee94c268 100644
--- a/cabal-install/Distribution/Client/List.hs
+++ b/cabal-install/Distribution/Client/List.hs
@@ -70,17 +70,16 @@ import System.Directory
          ( doesDirectoryExist )
 
 
--- |Show information about packages
-list :: Verbosity
-     -> PackageDBStack
-     -> [Repo]
-     -> Compiler
-     -> ProgramConfiguration
-     -> ListFlags
-     -> [String]
-     -> IO ()
-list verbosity packageDBs repos comp conf listFlags pats = do
-
+-- |Returns list of packages matching a search strings
+getPkgList :: Verbosity
+           -> PackageDBStack
+           -> [Repo]
+           -> Compiler
+           -> ProgramConfiguration
+           -> ListFlags
+           -> [String]
+           -> IO [PackageDisplayInfo]
+getPkgList verbosity packageDBs repos comp conf listFlags pats = do
     installedPkgIndex <- getInstalledPackages verbosity comp packageDBs conf
     sourcePkgDb       <- getSourcePackages    verbosity repos
     let sourcePkgIndex = packageIndex sourcePkgDb
@@ -104,6 +103,26 @@ list verbosity packageDBs repos comp conf listFlags pats = do
                   , not onlyInstalled || not (null installedPkgs)
                   , let pref        = prefs pkgname
                         selectedPkg = latestWithPref pref sourcePkgs ]
+    return matches
+  where
+    onlyInstalled = fromFlag (listInstalled listFlags)
+    matchingPackages search index =
+      [ pkg
+      | pat <- pats
+      , pkg <- search index pat ]
+
+
+-- |Show information about packages
+list :: Verbosity
+     -> PackageDBStack
+     -> [Repo]
+     -> Compiler
+     -> ProgramConfiguration
+     -> ListFlags
+     -> [String]
+     -> IO ()
+list verbosity packageDBs repos comp conf listFlags pats = do
+    matches <- getPkgList verbosity packageDBs repos comp conf listFlags pats
 
     if simpleOutput
       then putStr $ unlines
-- 
GitLab