diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index a0f508b..d17a71f 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,46 @@ 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 + sourcesJsonStatus + sourcesNixStatus' + where + sourcesJsonStatus = do + fsj <- getFindSourcesJson + liftIO (getSourcesEither fsj) >>= \case + 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 -> 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 2395f3c..49ea846 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) @@ -244,36 +245,45 @@ pathNixSourcesNix = "nix" "sources.nix" warnIfOutdated :: IO () warnIfOutdated = do + 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 + | 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 + +-- | Get the status of the sources.nix +sourcesNixStatus :: IO SourcesNixStatus +sourcesNixStatus = do tryAny (BL8.readFile pathNixSourcesNix) >>= \case - Left e -> - twarn $ - T.unlines - [ T.unwords ["Could not read", T.pack pathNixSourcesNix], - T.unwords [" ", "(", tshow e, ")"] - ] + 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 () - Just 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" - ] + Nothing -> pure SourcesNixCustom + Just v -> pure $ SourcesNixFound v -- | Glue code between nix and sources.json initNixSourcesNixContent :: B.ByteString