module Distribution.Client.Upload (upload, uploadDoc, report) where

import Distribution.Client.Compat.Prelude
import qualified Prelude as Unsafe (read)

import Distribution.Client.HttpUtils
  ( HttpTransport (..)
  , remoteRepoTryUpgradeToHttps
  )
import Distribution.Client.Setup
  ( IsCandidate (..)
  , RepoContext (..)
  )
import Distribution.Client.Types.Credentials
  ( Auth
  , Password (..)
  , Token (..)
  , Username (..)
  )
import Distribution.Client.Types.Repo (RemoteRepo (..), Repo, maybeRepoRemote)
import Distribution.Client.Types.RepoName (unRepoName)

import Distribution.Client.Config
import Distribution.Simple.Utils (dieWithException, info, notice, toUTF8BS, warn)
import Distribution.Utils.String (trim)

import Distribution.Client.BuildReports.Anonymous (parseBuildReport)
import qualified Distribution.Client.BuildReports.Anonymous as BuildReport
import qualified Distribution.Client.BuildReports.Upload as BuildReport
import Distribution.Client.Errors
import Network.HTTP (Header (..), HeaderName (..))
import Network.URI (URI (uriAuthority, uriPath), URIAuth (uriRegName))
import System.Directory
import System.FilePath (dropExtension, takeExtension, takeFileName, (</>))
import qualified System.FilePath.Posix as FilePath.Posix ((</>))
import System.IO (hFlush, stdout)
import System.IO.Echo (withoutInputEcho)

-- > stripExtensions ["tar", "gz"] "foo.tar.gz"
-- Just "foo"
-- > stripExtensions ["tar", "gz"] "foo.gz.tar"
-- Nothing
stripExtensions :: [String] -> FilePath -> Maybe String
stripExtensions :: [[Char]] -> [Char] -> Maybe [Char]
stripExtensions [[Char]]
exts [Char]
path = ([Char] -> [Char] -> Maybe [Char])
-> [Char] -> [[Char]] -> Maybe [Char]
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM [Char] -> [Char] -> Maybe [Char]
f [Char]
path ([[Char]] -> [[Char]]
forall a. [a] -> [a]
reverse [[Char]]
exts)
  where
    f :: [Char] -> [Char] -> Maybe [Char]
f [Char]
p [Char]
e
      | [Char] -> [Char]
takeExtension [Char]
p [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char]
e = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ([Char] -> [Char]
dropExtension [Char]
p)
      | Bool
otherwise = Maybe [Char]
forall a. Maybe a
Nothing

upload
  :: Verbosity
  -> RepoContext
  -> Maybe Token
  -> Maybe Username
  -> Maybe Password
  -> IsCandidate
  -> [FilePath]
  -> IO ()
upload :: Verbosity
-> RepoContext
-> Maybe Token
-> Maybe Username
-> Maybe Password
-> IsCandidate
-> [[Char]]
-> IO ()
upload Verbosity
verbosity RepoContext
repoCtxt Maybe Token
mToken Maybe Username
mUsername Maybe Password
mPassword IsCandidate
isCandidate [[Char]]
paths = do
  let repos :: [Repo]
      repos :: [Repo]
repos = RepoContext -> [Repo]
repoContextRepos RepoContext
repoCtxt
  transport <- RepoContext -> IO HttpTransport
repoContextGetTransport RepoContext
repoCtxt
  targetRepo <-
    case [remoteRepo | Just remoteRepo <- map maybeRepoRemote repos] of
      [] -> Verbosity -> CabalInstallException -> IO RemoteRepo
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
 Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity CabalInstallException
NoRemoteRepositories
      (RemoteRepo
r : [RemoteRepo]
rs) -> Verbosity -> HttpTransport -> RemoteRepo -> IO RemoteRepo
remoteRepoTryUpgradeToHttps Verbosity
verbosity HttpTransport
transport (NonEmpty RemoteRepo -> RemoteRepo
forall a. NonEmpty a -> a
last (RemoteRepo
r RemoteRepo -> [RemoteRepo] -> NonEmpty RemoteRepo
forall a. a -> [a] -> NonEmpty a
:| [RemoteRepo]
rs))
  let targetRepoURI :: URI
      targetRepoURI = RemoteRepo -> URI
remoteRepoURI RemoteRepo
targetRepo
      domain :: String
      domain = [Char] -> (URIAuth -> [Char]) -> Maybe URIAuth -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"Hackage" URIAuth -> [Char]
uriRegName (Maybe URIAuth -> [Char]) -> Maybe URIAuth -> [Char]
forall a b. (a -> b) -> a -> b
$ URI -> Maybe URIAuth
uriAuthority URI
targetRepoURI
      rootIfEmpty [Char]
x = if [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
x then [Char]
"/" else [Char]
x
      uploadURI :: URI
      uploadURI =
        URI
targetRepoURI
          { uriPath =
              rootIfEmpty (uriPath targetRepoURI)
                FilePath.Posix.</> case isCandidate of
                  IsCandidate
IsCandidate -> [Char]
"packages/candidates"
                  IsCandidate
IsPublished -> [Char]
"upload"
          }
      packageURI [Char]
pkgid =
        URI
targetRepoURI
          { uriPath =
              rootIfEmpty (uriPath targetRepoURI)
                FilePath.Posix.</> concat
                  [ "package/"
                  , pkgid
                  , case isCandidate of
                      IsCandidate
IsCandidate -> [Char]
"/candidate"
                      IsCandidate
IsPublished -> [Char]
""
                  ]
          }
  auth <- Just <$> createAuth domain mToken mUsername mPassword
  for_ paths $ \[Char]
path -> do
    Verbosity -> [Char] -> IO ()
notice Verbosity
verbosity ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Uploading " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
path [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"... "
    case ([Char] -> [Char]) -> Maybe [Char] -> Maybe [Char]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Char] -> [Char]
takeFileName ([[Char]] -> [Char] -> Maybe [Char]
stripExtensions [[Char]
"tar", [Char]
"gz"] [Char]
path) of
      Just [Char]
pkgid ->
        HttpTransport
-> Verbosity
-> URI
-> URI
-> Maybe Auth
-> IsCandidate
-> [Char]
-> IO ()
handlePackage
          HttpTransport
transport
          Verbosity
verbosity
          URI
uploadURI
          ([Char] -> URI
packageURI [Char]
pkgid)
          Maybe Auth
auth
          IsCandidate
isCandidate
          [Char]
path
      -- This case shouldn't really happen, since we check in Main that we
      -- only pass tar.gz files to upload.
      Maybe [Char]
Nothing -> Verbosity -> CabalInstallException -> IO ()
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
 Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity (CabalInstallException -> IO ()) -> CabalInstallException -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> CabalInstallException
NotATarDotGzFile [Char]
path

uploadDoc
  :: Verbosity
  -> RepoContext
  -> Maybe Token
  -> Maybe Username
  -> Maybe Password
  -> IsCandidate
  -> FilePath
  -> IO ()
uploadDoc :: Verbosity
-> RepoContext
-> Maybe Token
-> Maybe Username
-> Maybe Password
-> IsCandidate
-> [Char]
-> IO ()
uploadDoc Verbosity
verbosity RepoContext
repoCtxt Maybe Token
mToken Maybe Username
mUsername Maybe Password
mPassword IsCandidate
isCandidate [Char]
path = do
  let repos :: [Repo]
repos = RepoContext -> [Repo]
repoContextRepos RepoContext
repoCtxt
  transport <- RepoContext -> IO HttpTransport
repoContextGetTransport RepoContext
repoCtxt
  targetRepo <-
    case [remoteRepo | Just remoteRepo <- map maybeRepoRemote repos] of
      [] -> Verbosity -> CabalInstallException -> IO RemoteRepo
forall a1 a.
(HasCallStack, Show a1, Typeable a1,
 Exception (VerboseException a1)) =>
Verbosity -> a1 -> IO a
dieWithException Verbosity
verbosity CabalInstallException
NoRemoteRepositories
      (RemoteRepo
r : [RemoteRepo]
rs) -> Verbosity -> HttpTransport -> RemoteRepo -> IO RemoteRepo
remoteRepoTryUpgradeToHttps Verbosity
verbosity HttpTransport
transport (NonEmpty RemoteRepo -> RemoteRepo
forall a. NonEmpty a -> a
last (RemoteRepo
r RemoteRepo -> [RemoteRepo] -> NonEmpty RemoteRepo
forall a. a -> [a] -> NonEmpty a
:| [RemoteRepo]
rs))
  let targetRepoURI = RemoteRepo -> URI
remoteRepoURI RemoteRepo
targetRepo
      domain = [Char] -> (URIAuth -> [Char]) -> Maybe URIAuth -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"Hackage" URIAuth -> [Char]
uriRegName (Maybe URIAuth -> [Char]) -> Maybe URIAuth -> [Char]
forall a b. (a -> b) -> a -> b
$ URI -> Maybe URIAuth
uriAuthority URI
targetRepoURI
      rootIfEmpty [Char]
x = if [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
x then [Char]
"/" else [Char]
x
      uploadURI =
        URI
targetRepoURI
          { uriPath =
              rootIfEmpty (uriPath targetRepoURI)
                FilePath.Posix.</> concat
                  [ "package/"
                  , pkgid
                  , case isCandidate of
                      IsCandidate
IsCandidate -> [Char]
"/candidate"
                      IsCandidate
IsPublished -> [Char]
""
                  , "/docs"
                  ]
          }
      packageUri =
        URI
targetRepoURI
          { uriPath =
              rootIfEmpty (uriPath targetRepoURI)
                FilePath.Posix.</> concat
                  [ "package/"
                  , pkgid
                  , case isCandidate of
                      IsCandidate
IsCandidate -> [Char]
"/candidate"
                      IsCandidate
IsPublished -> [Char]
""
                  ]
          }
      (reverseSuffix, reversePkgid) =
        break
          (== '-')
          (reverse (takeFileName path))
      pkgid = [Char] -> [Char]
forall a. [a] -> [a]
reverse ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ Int -> [Char] -> [Char]
forall a. Int -> [a] -> [a]
drop Int
1 [Char]
reversePkgid
  when
    ( reverse reverseSuffix /= "docs.tar.gz"
        || ( case reversePkgid of
              [] -> Bool
True
              (Char
c : [Char]
_) -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'-'
           )
    )
    $ dieWithException verbosity ExpectedMatchingFileName

  auth <- Just <$> createAuth domain mToken mUsername mPassword

  let headers =
        [ HeaderName -> [Char] -> Header
Header HeaderName
HdrContentType [Char]
"application/x-tar"
        , HeaderName -> [Char] -> Header
Header HeaderName
HdrContentEncoding [Char]
"gzip"
        ]
  notice verbosity $ "Uploading documentation " ++ path ++ "... "
  resp <- putHttpFile transport verbosity uploadURI path auth headers
  case resp of
    -- Hackage responds with 204 No Content when docs are uploaded
    -- successfully.
    (Int
code, [Char]
_) | Int
code Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int
200, Int
204] -> do
      Verbosity -> [Char] -> IO ()
notice Verbosity
verbosity ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ URI -> [Char]
forall {a}. Show a => a -> [Char]
okMessage URI
packageUri
    (Int
code, [Char]
err) -> do
      Verbosity -> [Char] -> IO ()
notice Verbosity
verbosity ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
        [Char]
"Error uploading documentation "
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
path
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": "
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"http code "
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall {a}. Show a => a -> [Char]
show Int
code
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n"
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
err
      IO ()
forall a. IO a
exitFailure
  where
    okMessage :: a -> [Char]
okMessage a
packageUri = case IsCandidate
isCandidate of
      IsCandidate
IsCandidate ->
        [Char]
"Documentation successfully uploaded for package candidate. "
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"You can now preview the result at '"
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall {a}. Show a => a -> [Char]
show a
packageUri
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'. To upload non-candidate documentation, use 'cabal upload --publish'."
      IsCandidate
IsPublished ->
        [Char]
"Package documentation successfully published. You can now view it at '"
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall {a}. Show a => a -> [Char]
show a
packageUri
          [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'."

promptUsername :: String -> IO Username
promptUsername :: [Char] -> IO Username
promptUsername [Char]
domain = do
  [Char] -> IO ()
putStr ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
domain [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" username: "
  Handle -> IO ()
hFlush Handle
stdout
  ([Char] -> Username) -> IO [Char] -> IO Username
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Char] -> Username
Username IO [Char]
getLine

promptPassword :: String -> IO Password
promptPassword :: [Char] -> IO Password
promptPassword [Char]
domain = do
  [Char] -> IO ()
putStr ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
domain [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" password: "
  Handle -> IO ()
hFlush Handle
stdout
  -- save/restore the terminal echoing status (no echoing for entering the password)
  passwd <- IO Password -> IO Password
forall a. IO a -> IO a
withoutInputEcho (IO Password -> IO Password) -> IO Password -> IO Password
forall a b. (a -> b) -> a -> b
$ ([Char] -> Password) -> IO [Char] -> IO Password
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Char] -> Password
Password IO [Char]
getLine
  putStrLn ""
  return passwd

report :: Verbosity -> RepoContext -> Maybe Token -> Maybe Username -> Maybe Password -> IO ()
report :: Verbosity
-> RepoContext
-> Maybe Token
-> Maybe Username
-> Maybe Password
-> IO ()
report Verbosity
verbosity RepoContext
repoCtxt Maybe Token
mToken Maybe Username
mUsername Maybe Password
mPassword = do
  let repos :: [Repo]
      repos :: [Repo]
repos = RepoContext -> [Repo]
repoContextRepos RepoContext
repoCtxt
      remoteRepos :: [RemoteRepo]
      remoteRepos :: [RemoteRepo]
remoteRepos = (Repo -> Maybe RemoteRepo) -> [Repo] -> [RemoteRepo]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Repo -> Maybe RemoteRepo
maybeRepoRemote [Repo]
repos
  [RemoteRepo] -> (RemoteRepo -> IO ()) -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [RemoteRepo]
remoteRepos ((RemoteRepo -> IO ()) -> IO ()) -> (RemoteRepo -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \RemoteRepo
remoteRepo -> do
    let domain :: [Char]
domain = [Char] -> (URIAuth -> [Char]) -> Maybe URIAuth -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"Hackage" URIAuth -> [Char]
uriRegName (Maybe URIAuth -> [Char]) -> Maybe URIAuth -> [Char]
forall a b. (a -> b) -> a -> b
$ URI -> Maybe URIAuth
uriAuthority (RemoteRepo -> URI
remoteRepoURI RemoteRepo
remoteRepo)
    auth <- [Char]
-> Maybe Token -> Maybe Username -> Maybe Password -> IO Auth
createAuth [Char]
domain Maybe Token
mToken Maybe Username
mUsername Maybe Password
mPassword

    reportsDir <- defaultReportsDir
    let srcDir :: FilePath
        srcDir = [Char]
reportsDir [Char] -> [Char] -> [Char]
</> RepoName -> [Char]
unRepoName (RemoteRepo -> RepoName
remoteRepoName RemoteRepo
remoteRepo)
    -- We don't want to bomb out just because we haven't built any packages
    -- from this repo yet.
    srcExists <- doesDirectoryExist srcDir
    when srcExists $ do
      contents <- getDirectoryContents srcDir
      for_ (filter (\[Char]
c -> [Char] -> [Char]
takeExtension [Char]
c [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
".log") contents) $ \[Char]
logFile ->
        do
          inp <- [Char] -> IO [Char]
readFile ([Char]
srcDir [Char] -> [Char] -> [Char]
</> [Char]
logFile)
          let (reportStr, buildLog) = Unsafe.read inp :: (String, String) -- TODO: eradicateNoParse
          case parseBuildReport (toUTF8BS reportStr) of
            Left [Char]
errs -> Verbosity -> [Char] -> IO ()
warn Verbosity
verbosity ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Errors: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
errs -- FIXME
            Right BuildReport
report' ->
              do
                Verbosity -> [Char] -> IO ()
info Verbosity
verbosity ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
                  [Char]
"Uploading report for "
                    [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ PackageIdentifier -> [Char]
forall a. Pretty a => a -> [Char]
prettyShow (BuildReport -> PackageIdentifier
BuildReport.package BuildReport
report')
                Verbosity
-> RepoContext
-> Auth
-> URI
-> [(BuildReport, Maybe [Char])]
-> IO ()
BuildReport.uploadReports
                  Verbosity
verbosity
                  RepoContext
repoCtxt
                  Auth
auth
                  (RemoteRepo -> URI
remoteRepoURI RemoteRepo
remoteRepo)
                  [(BuildReport
report', [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
buildLog)]
                () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

handlePackage
  :: HttpTransport
  -> Verbosity
  -> URI
  -> URI
  -> Maybe Auth
  -> IsCandidate
  -> FilePath
  -> IO ()
handlePackage :: HttpTransport
-> Verbosity
-> URI
-> URI
-> Maybe Auth
-> IsCandidate
-> [Char]
-> IO ()
handlePackage HttpTransport
transport Verbosity
verbosity URI
uri URI
packageUri Maybe Auth
auth IsCandidate
isCandidate [Char]
path =
  do
    resp <- HttpTransport
-> Verbosity -> URI -> [Char] -> Maybe Auth -> IO (Int, [Char])
postHttpFile HttpTransport
transport Verbosity
verbosity URI
uri [Char]
path Maybe Auth
auth
    case resp of
      (Int
code, [Char]
warnings)
        | Int
code Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int
200, Int
204] ->
            Verbosity -> [Char] -> IO ()
notice Verbosity
verbosity ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
              IsCandidate -> [Char]
okMessage IsCandidate
isCandidate
                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ if [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
warnings then [Char]
"" else [Char]
"\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
formatWarnings ([Char] -> [Char]
trim [Char]
warnings)
      (Int
code, [Char]
err) -> do
        Verbosity -> [Char] -> IO ()
notice Verbosity
verbosity ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char]
"Error uploading "
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
path
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": "
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"http code "
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall {a}. Show a => a -> [Char]
show Int
code
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n"
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
err
        IO ()
forall a. IO a
exitFailure
  where
    okMessage :: IsCandidate -> String
    okMessage :: IsCandidate -> [Char]
okMessage IsCandidate
IsCandidate =
      [Char]
"Package successfully uploaded as candidate. "
        [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"You can now preview the result at '"
        [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ URI -> [Char]
forall {a}. Show a => a -> [Char]
show URI
packageUri
        [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'. To publish the candidate, use 'cabal upload --publish'."
    okMessage IsCandidate
IsPublished =
      [Char]
"Package successfully published. You can now view it at '"
        [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ URI -> [Char]
forall {a}. Show a => a -> [Char]
show URI
packageUri
        [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'."

formatWarnings :: String -> String
formatWarnings :: [Char] -> [Char]
formatWarnings [Char]
x = [Char]
"Warnings:\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ([[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char]
"- " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines) [Char]
x

createAuth
  :: String
  -> Maybe Token
  -> Maybe Username
  -> Maybe Password
  -> IO Auth
createAuth :: [Char]
-> Maybe Token -> Maybe Username -> Maybe Password -> IO Auth
createAuth [Char]
domain Maybe Token
mToken Maybe Username
mUsername Maybe Password
mPassword = case Maybe Token
mToken of
  Just Token
token -> Auth -> IO Auth
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Auth -> IO Auth) -> Auth -> IO Auth
forall a b. (a -> b) -> a -> b
$ [Char] -> Auth
forall a b. b -> Either a b
Right ([Char] -> Auth) -> [Char] -> Auth
forall a b. (a -> b) -> a -> b
$ Token -> [Char]
unToken Token
token
  -- Use username and password if no token is provided
  Maybe Token
Nothing -> do
    Username username <- IO Username
-> (Username -> IO Username) -> Maybe Username -> IO Username
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> IO Username
promptUsername [Char]
domain) Username -> IO Username
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Username
mUsername
    Password password <- maybe (promptPassword domain) return mPassword
    return $ Left (username, password)