Skip to content

Commit

Permalink
Add skeleton for niv status
Browse files Browse the repository at this point in the history
  • Loading branch information
nmattia committed Sep 7, 2020
1 parent b50a010 commit 8d5eb79
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/Niv/Cli.hs
Expand Up @@ -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
Expand Down Expand Up @@ -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
-------------------------------------------------------------------------------
Expand Down
9 changes: 5 additions & 4 deletions src/Niv/Sources.hs
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8d5eb79

Please sign in to comment.