From 8d5eb797244e366f68e17aa57bcbd25975cf33ec Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Mon, 7 Sep 2020 17:47:53 +0200 Subject: [PATCH 1/3] Add skeleton for niv status --- src/Niv/Cli.hs | 31 +++++++++++++++++++++++++++++++ src/Niv/Sources.hs | 9 +++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index a0f508b..51d7cc6 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -93,6 +93,7 @@ parseCommand = <> Opts.command "update" parseCmdUpdate <> Opts.command "modify" parseCmdModify <> Opts.command "drop" parseCmdDrop + <> Opts.command "status" parseCmdStatus ) parsePackageName :: Opts.Parser PackageName @@ -561,6 +562,36 @@ cmdDrop packageName = \case li $ setSources fsj $ Sources $ HMS.insert packageName packageSpec sources +------------------------------------------------------------------------------- +-- STATUS +------------------------------------------------------------------------------- + +parseCmdStatus :: Opts.ParserInfo (NIO ()) +parseCmdStatus = + Opts.info + ( pure cmdStatus + <**> Opts.helper + ) + $ mconcat desc + where + desc = + [ Opts.fullDesc, + Opts.progDesc "Status of niv files" + ] + +cmdStatus :: NIO () +cmdStatus = do + sjs <- sourcesJsonStatus + tsay $ "sources.json: " <> sjs + where + sourcesJsonStatus = do + fsj <- getFindSourcesJson + liftIO (getSourcesEither fsj) >>= \case + Right (fp, _) -> pure (T.pack fp) + Left SourcesDoesntExist -> pure "not found" + Left SourceIsntJSON -> pure "not json" + Left SpecIsntAMap -> pure "bad format, not a map" + ------------------------------------------------------------------------------- -- Files and their content ------------------------------------------------------------------------------- diff --git a/src/Niv/Sources.hs b/src/Niv/Sources.hs index 2395f3c..9a50ed9 100644 --- a/src/Niv/Sources.hs +++ b/src/Niv/Sources.hs @@ -48,15 +48,16 @@ newtype Sources {unSources :: HMS.HashMap PackageName PackageSpec} deriving newtype (FromJSON, ToJSON) -getSourcesEither :: FindSourcesJson -> IO (Either SourcesError Sources) +getSourcesEither :: FindSourcesJson -> IO (Either SourcesError (FilePath, Sources)) getSourcesEither fsj = do - Dir.doesFileExist (pathNixSourcesJson fsj) >>= \case + let path = pathNixSourcesJson fsj + Dir.doesFileExist path >>= \case False -> pure $ Left SourcesDoesntExist True -> Aeson.decodeFileStrict (pathNixSourcesJson fsj) >>= \case Just value -> case valueToSources value of Nothing -> pure $ Left SpecIsntAMap - Just srcs -> pure $ Right srcs + Just srcs -> pure $ Right (path, srcs) Nothing -> pure $ Left SourceIsntJSON where valueToSources :: Aeson.Value -> Maybe Sources @@ -76,7 +77,7 @@ getSourcesEither fsj = do getSources :: FindSourcesJson -> IO Sources getSources fsj = do warnIfOutdated - getSourcesEither fsj + fmap snd <$> getSourcesEither fsj >>= either ( \case SourcesDoesntExist -> (abortSourcesDoesntExist fsj) From 723d4e11039f783bc29b0135b746f30e9ae10674 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Mon, 7 Sep 2020 17:58:11 +0200 Subject: [PATCH 2/3] Add basic sources.nix info to niv status --- src/Niv/Cli.hs | 6 ++++++ src/Niv/Sources.hs | 33 +++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index 51d7cc6..3a7a295 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -583,6 +583,8 @@ cmdStatus :: NIO () cmdStatus = do sjs <- sourcesJsonStatus tsay $ "sources.json: " <> sjs + sns <- sourcesNixStatus' + tsay $ "sources.nix: " <> sns where sourcesJsonStatus = do fsj <- getFindSourcesJson @@ -591,6 +593,10 @@ cmdStatus = do Left SourcesDoesntExist -> pure "not found" Left SourceIsntJSON -> pure "not json" Left SpecIsntAMap -> pure "bad format, not a map" + sourcesNixStatus' = liftIO sourcesNixStatus >>= \case + SourcesNixNotFound -> pure "not found" + SourcesNixCustom -> pure "custom" + SourcesNixFound v -> pure (sourcesVersionToText v) -- TODO add path ------------------------------------------------------------------------------- -- Files and their content diff --git a/src/Niv/Sources.hs b/src/Niv/Sources.hs index 9a50ed9..36bcf89 100644 --- a/src/Niv/Sources.hs +++ b/src/Niv/Sources.hs @@ -245,18 +245,11 @@ pathNixSourcesNix = "nix" "sources.nix" warnIfOutdated :: IO () warnIfOutdated = do - tryAny (BL8.readFile pathNixSourcesNix) >>= \case - Left e -> - twarn $ - T.unlines - [ T.unwords ["Could not read", T.pack pathNixSourcesNix], - T.unwords [" ", "(", tshow e, ")"] - ] - Right content -> do - case md5ToSourcesVersion (T.pack $ show $ MD5.md5 content) of - -- This is a custom or newer version, we don't do anything - Nothing -> pure () - Just v + sourcesNixStatus >>= \case + SourcesNixNotFound -> + twarn $ T.unwords ["Could not read", T.pack pathNixSourcesNix] + SourcesNixCustom -> pure () + SourcesNixFound v -- The file is the latest | v == maxBound -> pure () -- The file is older than than latest @@ -276,6 +269,22 @@ warnIfOutdated = do " # niv: no_update" ] +data SourcesNixStatus = + SourcesNixNotFound | + SourcesNixCustom | + SourcesNixFound SourcesNixVersion + +-- | Get the status of the sources.nix +sourcesNixStatus :: IO SourcesNixStatus +sourcesNixStatus = do + tryAny (BL8.readFile pathNixSourcesNix) >>= \case + Left {} -> pure SourcesNixNotFound + Right content -> do + case md5ToSourcesVersion (T.pack $ show $ MD5.md5 content) of + -- This is a custom or newer version, we don't do anything + Nothing -> pure SourcesNixCustom + Just v -> pure $ SourcesNixFound v + -- | Glue code between nix and sources.json initNixSourcesNixContent :: B.ByteString initNixSourcesNixContent = $(embedFile "nix/sources.nix") From 02fa67aa345d1ddfed629901adb07e0ce8103e1e Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Tue, 8 Sep 2020 17:42:24 +0200 Subject: [PATCH 3/3] Show more info about sources --- src/Niv/Cli.hs | 26 +++++++++++++++----------- src/Niv/Sources.hs | 44 ++++++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index 3a7a295..d17a71f 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -581,22 +581,26 @@ parseCmdStatus = cmdStatus :: NIO () cmdStatus = do - sjs <- sourcesJsonStatus - tsay $ "sources.json: " <> sjs - sns <- sourcesNixStatus' - tsay $ "sources.nix: " <> sns + sourcesJsonStatus + sourcesNixStatus' where sourcesJsonStatus = do fsj <- getFindSourcesJson liftIO (getSourcesEither fsj) >>= \case - Right (fp, _) -> pure (T.pack fp) - Left SourcesDoesntExist -> pure "not found" - Left SourceIsntJSON -> pure "not json" - Left SpecIsntAMap -> pure "bad format, not a map" + Right (fp, sources) -> do + tsay $ "sources.json: " <> (T.pack fp) + tsay $ "sources.json # of packages: " <> tshow (HMS.size (unSources sources)) + Left SourcesDoesntExist -> tsay "sources.json: not found" + Left SourceIsntJSON -> tsay "sources.json: not json" + Left SpecIsntAMap -> tsay "sources.json: bad format, not a map" sourcesNixStatus' = liftIO sourcesNixStatus >>= \case - SourcesNixNotFound -> pure "not found" - SourcesNixCustom -> pure "custom" - SourcesNixFound v -> pure (sourcesVersionToText v) -- TODO add path + SourcesNixNotFound -> tsay $ "sources.nix: not found" + SourcesNixCustom -> do + tsay $ "sources.nix: " <> T.pack pathNixSourcesNix + tsay $ "sources.nix version: custom" + SourcesNixFound v -> do + tsay $ "sources.nix: " <> T.pack pathNixSourcesNix + tsay $ "sources.nix version: " <> sourcesVersionToText v ------------------------------------------------------------------------------- -- Files and their content diff --git a/src/Niv/Sources.hs b/src/Niv/Sources.hs index 36bcf89..49ea846 100644 --- a/src/Niv/Sources.hs +++ b/src/Niv/Sources.hs @@ -250,29 +250,29 @@ warnIfOutdated = do twarn $ T.unwords ["Could not read", T.pack pathNixSourcesNix] SourcesNixCustom -> pure () SourcesNixFound v - -- The file is the latest - | v == maxBound -> pure () - -- The file is older than than latest - | otherwise -> do - tsay $ - T.unlines - [ T.unwords - [ tbold $ tblue "INFO:", - "new sources.nix available:", - sourcesVersionToText v, - "->", - sourcesVersionToText maxBound - ], - " Please run 'niv init' or add the following line in the " - <> T.pack pathNixSourcesNix - <> " file:", - " # niv: no_update" - ] + -- The file is the latest + | v == maxBound -> pure () + -- The file is older than than latest + | otherwise -> do + tsay $ + T.unlines + [ T.unwords + [ tbold $ tblue "INFO:", + "new sources.nix available:", + sourcesVersionToText v, + "->", + sourcesVersionToText maxBound + ], + " Please run 'niv init' or add the following line in the " + <> T.pack pathNixSourcesNix + <> " file:", + " # niv: no_update" + ] -data SourcesNixStatus = - SourcesNixNotFound | - SourcesNixCustom | - SourcesNixFound SourcesNixVersion +data SourcesNixStatus + = SourcesNixNotFound + | SourcesNixCustom + | SourcesNixFound SourcesNixVersion -- | Get the status of the sources.nix sourcesNixStatus :: IO SourcesNixStatus