Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skeleton for niv status #279

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 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,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
-------------------------------------------------------------------------------
Expand Down
70 changes: 40 additions & 30 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 Expand Up @@ -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
Expand Down