|
| 1 | +module Distribution.Client.Mirror.CmdLine ( |
| 2 | + MirrorOpts(..) |
| 3 | + , validateOpts |
| 4 | + ) where |
| 5 | + |
| 6 | +-- stdlib |
| 7 | +import Control.Monad |
| 8 | +import Data.Maybe (fromMaybe) |
| 9 | +import System.Exit (exitSuccess) |
| 10 | +import System.Console.GetOpt |
| 11 | + |
| 12 | +-- Cabal |
| 13 | +import Distribution.Package |
| 14 | +import Distribution.Verbosity |
| 15 | +import Distribution.Simple.Utils hiding (warn) |
| 16 | + |
| 17 | +-- hackage |
| 18 | +import Distribution.Client (validatePackageIds) |
| 19 | +import Distribution.Client.Mirror.Config |
| 20 | + |
| 21 | +data MirrorOpts = MirrorOpts { |
| 22 | + mirrorConfig :: MirrorConfig, |
| 23 | + stateDir :: FilePath, |
| 24 | + selectedPkgs :: [PackageId], |
| 25 | + continuous :: Maybe Int, -- if so, interval in minutes |
| 26 | + mo_keepGoing :: Bool, |
| 27 | + mirrorUploaders :: Bool |
| 28 | + } |
| 29 | + |
| 30 | +data MirrorFlags = MirrorFlags { |
| 31 | + flagCacheDir :: Maybe FilePath, |
| 32 | + flagContinuous :: Bool, |
| 33 | + flagInterval :: Maybe String, |
| 34 | + flagKeepGoing :: Bool, |
| 35 | + flagMirrorUploaders :: Bool, |
| 36 | + flagVerbosity :: Verbosity, |
| 37 | + flagHelp :: Bool |
| 38 | +} |
| 39 | + |
| 40 | +defaultMirrorFlags :: MirrorFlags |
| 41 | +defaultMirrorFlags = MirrorFlags |
| 42 | + { flagCacheDir = Nothing |
| 43 | + , flagContinuous = False |
| 44 | + , flagInterval = Nothing |
| 45 | + , flagKeepGoing = False |
| 46 | + , flagMirrorUploaders = False |
| 47 | + , flagVerbosity = normal |
| 48 | + , flagHelp = False |
| 49 | + } |
| 50 | + |
| 51 | +mirrorFlagDescrs :: [OptDescr (MirrorFlags -> MirrorFlags)] |
| 52 | +mirrorFlagDescrs = |
| 53 | + [ Option ['h'] ["help"] |
| 54 | + (NoArg (\opts -> opts { flagHelp = True })) |
| 55 | + "Show this help text" |
| 56 | + |
| 57 | + , Option ['v'] [] |
| 58 | + (NoArg (\opts -> opts { flagVerbosity = moreVerbose (flagVerbosity opts) })) |
| 59 | + "Verbose mode (can be listed multiple times e.g. -vv)" |
| 60 | + |
| 61 | + , Option [] ["cache-dir"] |
| 62 | + (ReqArg (\dir opts -> opts { flagCacheDir = Just dir }) "DIR") |
| 63 | + "Where to put downloaded files (default ./mirror-cache/)" |
| 64 | + |
| 65 | + , Option [] ["continuous"] |
| 66 | + (NoArg (\opts -> opts { flagContinuous = True })) |
| 67 | + "Mirror continuously rather than just once." |
| 68 | + |
| 69 | + , Option [] ["interval"] |
| 70 | + (ReqArg (\int opts -> opts { flagInterval = Just int }) "MIN") |
| 71 | + "Set the mirroring interval in minutes (default 30)" |
| 72 | + |
| 73 | + , Option [] ["keep-going"] |
| 74 | + (NoArg (\opts -> opts { flagKeepGoing = True })) |
| 75 | + "Don't fail on mirroring errors, keep going." |
| 76 | + |
| 77 | + , Option [] ["mirror-uploaders"] |
| 78 | + (NoArg (\opts -> opts { flagMirrorUploaders = True })) |
| 79 | + "Mirror the original uploaders which requires that they are already registered on the target hackage." |
| 80 | + ] |
| 81 | + |
| 82 | +validateOpts :: [String] -> IO (Verbosity, MirrorOpts) |
| 83 | +validateOpts args = do |
| 84 | + let (flags0, args', errs) = getOpt Permute mirrorFlagDescrs args |
| 85 | + flags = accum flags0 defaultMirrorFlags |
| 86 | + |
| 87 | + when (flagHelp flags) printUsage |
| 88 | + when (not (null errs)) (printErrors errs) |
| 89 | + |
| 90 | + case args' of |
| 91 | + (configFile:pkgstrs) -> do |
| 92 | + mCfg <- readMirrorConfig configFile |
| 93 | + case mCfg of |
| 94 | + Left theError -> die theError |
| 95 | + Right config -> case (mpkgs, minterval) of |
| 96 | + (Left theError, _) -> die theError |
| 97 | + (_, Left theError) -> die theError |
| 98 | + (Right pkgs, Right interval) -> |
| 99 | + return (flagVerbosity flags, MirrorOpts { |
| 100 | + mirrorConfig = config, |
| 101 | + stateDir = fromMaybe "mirror-cache" (flagCacheDir flags), |
| 102 | + selectedPkgs = pkgs, |
| 103 | + continuous = if flagContinuous flags |
| 104 | + then Just interval |
| 105 | + else Nothing, |
| 106 | + mo_keepGoing = flagKeepGoing flags, |
| 107 | + mirrorUploaders = flagMirrorUploaders flags |
| 108 | + }) |
| 109 | + where |
| 110 | + mpkgs = validatePackageIds pkgstrs |
| 111 | + minterval = validateInterval (flagInterval flags) |
| 112 | + |
| 113 | + _ -> die $ "Expected path to a config file.\n" |
| 114 | + ++ "See hackage-mirror --help for details and an example." |
| 115 | + |
| 116 | + where |
| 117 | + printUsage = do |
| 118 | + putStrLn $ usageInfo usageHeader mirrorFlagDescrs ++ helpExampleStr |
| 119 | + exitSuccess |
| 120 | + usageHeader = helpDescrStr |
| 121 | + ++ "Usage: hackage-mirror configFile [packages] [options]\n" |
| 122 | + ++ "\n" |
| 123 | + ++ "configFile should be a path to a file with format:\n" |
| 124 | + ++ "\n" |
| 125 | + ++ " source \"name-of-source-repo\"\n" |
| 126 | + ++ " uri: http://example:port\n" |
| 127 | + ++ " type: secure\n" |
| 128 | + ++ " \n" |
| 129 | + ++ " target \"name-of-target-repo\"\n" |
| 130 | + ++ " uri: file:/path/to/local/repo\n" |
| 131 | + ++ " type: local\n" |
| 132 | + ++ " \n" |
| 133 | + ++ " post-mirror-hook: \"shell command to execute\"\n" |
| 134 | + ++ "\n" |
| 135 | + ++ "Recognized types are hackage2, secure and local.\n" |
| 136 | + ++ "The post-mirror-hook is optional.\n" |
| 137 | + ++ "\n" |
| 138 | + ++ "Options:" |
| 139 | + printErrors errs = die $ concat errs ++ "Try --help." |
| 140 | + |
| 141 | + accum flags = foldr (flip (.)) id flags |
| 142 | + |
| 143 | + validateInterval Nothing = return 30 --default 30 min |
| 144 | + validateInterval (Just str) = do |
| 145 | + int <- case reads str of |
| 146 | + [(int,"")] -> return int |
| 147 | + [(int,"m")] -> return int |
| 148 | + [(int,"h")] -> return (int * 60) |
| 149 | + _ -> Left ("expected a number of minutes, not '" ++ str ++ "'") |
| 150 | + if int < 0 |
| 151 | + then Left "a negative mirroring interval is meaningless" |
| 152 | + else return int |
| 153 | + |
| 154 | +helpDescrStr :: String |
| 155 | +helpDescrStr = unlines |
| 156 | + [ "The hackage-mirror client copies packages from one hackage server to another." |
| 157 | + , "By default it copies over all packages that exist on the source but not on" |
| 158 | + , "the destination server. You can also select just specific packages to mirror." |
| 159 | + , "It is also possible to run the mirror in a continuous mode, giving you" |
| 160 | + , "nearly-live mirroring.\n" |
| 161 | + ] |
| 162 | + |
| 163 | +helpExampleStr :: String |
| 164 | +helpExampleStr = unlines |
| 165 | + [ "\nExample:" |
| 166 | + , " Suppose we have:" |
| 167 | + , " - source server: hackage.haskell.org" |
| 168 | + , " - dest server: localhost:8080" |
| 169 | + , " Uploading packages almost always requires authentication, so suppose we have" |
| 170 | + , " a user account for our mirror client with username 'foo' and password 'bar'." |
| 171 | + , " We include the authentication details into the destination URL:" |
| 172 | + , " http://foo:bar@localhost:8080/" |
| 173 | + , " To test that it is working without actually syncing a Gb of data from" |
| 174 | + , " hackage.haskell.org, we will specify to mirror only the 'zlib' package." |
| 175 | + , " So overall we run:" |
| 176 | + , " hackage-mirror ./mirror.cfg zlib" |
| 177 | + , " This will synchronise all versions of the 'zlib' package and then exit." |
| 178 | + ] |
0 commit comments