Skip to content

Commit 906a390

Browse files
committed
Add skeletton for niv status
1 parent b50a010 commit 906a390

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/Niv/Cli.hs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ parseCommand =
9393
<> Opts.command "update" parseCmdUpdate
9494
<> Opts.command "modify" parseCmdModify
9595
<> Opts.command "drop" parseCmdDrop
96+
<> Opts.command "status" parseCmdStatus
9697
)
9798

9899
parsePackageName :: Opts.Parser PackageName
@@ -561,6 +562,36 @@ cmdDrop packageName = \case
561562
li $ setSources fsj $ Sources $
562563
HMS.insert packageName packageSpec sources
563564

565+
-------------------------------------------------------------------------------
566+
-- STATUS
567+
-------------------------------------------------------------------------------
568+
569+
parseCmdStatus :: Opts.ParserInfo (NIO ())
570+
parseCmdStatus =
571+
Opts.info
572+
( pure cmdStatus
573+
<**> Opts.helper
574+
)
575+
$ mconcat desc
576+
where
577+
desc =
578+
[ Opts.fullDesc,
579+
Opts.progDesc "Status of niv files"
580+
]
581+
582+
cmdStatus :: NIO ()
583+
cmdStatus = do
584+
sjs <- sourcesJsonStatus
585+
tsay $ "sources.json: " <> sjs
586+
where
587+
sourcesJsonStatus = do
588+
fsj <- getFindSourcesJson
589+
liftIO (getSourcesEither fsj) >>= \case
590+
Right (fp, _) -> pure (T.pack fp)
591+
Left SourcesDoesntExist -> pure "not found"
592+
Left SourceIsntJSON -> pure "not json"
593+
Left SpecIsntAMap -> pure "bad format, not a map"
594+
564595
-------------------------------------------------------------------------------
565596
-- Files and their content
566597
-------------------------------------------------------------------------------

src/Niv/Sources.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ newtype Sources
4848
{unSources :: HMS.HashMap PackageName PackageSpec}
4949
deriving newtype (FromJSON, ToJSON)
5050

51-
getSourcesEither :: FindSourcesJson -> IO (Either SourcesError Sources)
51+
getSourcesEither :: FindSourcesJson -> IO (Either SourcesError (FilePath, Sources))
5252
getSourcesEither fsj = do
53-
Dir.doesFileExist (pathNixSourcesJson fsj) >>= \case
53+
let path = pathNixSourcesJson fsj
54+
Dir.doesFileExist path >>= \case
5455
False -> pure $ Left SourcesDoesntExist
5556
True ->
5657
Aeson.decodeFileStrict (pathNixSourcesJson fsj) >>= \case
5758
Just value -> case valueToSources value of
5859
Nothing -> pure $ Left SpecIsntAMap
59-
Just srcs -> pure $ Right srcs
60+
Just srcs -> pure $ Right (path, srcs)
6061
Nothing -> pure $ Left SourceIsntJSON
6162
where
6263
valueToSources :: Aeson.Value -> Maybe Sources
@@ -76,7 +77,7 @@ getSourcesEither fsj = do
7677
getSources :: FindSourcesJson -> IO Sources
7778
getSources fsj = do
7879
warnIfOutdated
79-
getSourcesEither fsj
80+
fmap snd <$> getSourcesEither fsj
8081
>>= either
8182
( \case
8283
SourcesDoesntExist -> (abortSourcesDoesntExist fsj)

0 commit comments

Comments
 (0)