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)