Skip to content

Commit

Permalink
Update when no cached version is available
Browse files Browse the repository at this point in the history
  • Loading branch information
gutjuri committed Oct 12, 2020
1 parent eb1df29 commit bfad30d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ library:
- containers
- http-conduit
- zip-archive
- time

ghc-options:
- -Wall
Expand Down
22 changes: 22 additions & 0 deletions src/Tldr/App/Handler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import Data.List (intercalate)
import Data.Semigroup ((<>))
import qualified Data.Set as Set
import Data.Version (showVersion)
import Data.Time.Clock

import Control.Monad (when)

import Options.Applicative
import Paths_tldr (version)
Expand All @@ -26,6 +29,8 @@ import System.Directory
, createDirectory
, removePathForcibly
, doesFileExist
, doesDirectoryExist
, getModificationTime
, getXdgDirectory
)
import System.Environment (lookupEnv, getExecutablePath)
Expand Down Expand Up @@ -68,6 +73,8 @@ handleTldrOpts opts@TldrOpts {..} =
UpdateIndex -> updateTldrPages
About -> handleAboutFlag
ViewPage voptions pages -> do
performUpdate <- updateNecessary
when performUpdate updateTldrPages
let npage = intercalate "-" pages
locale <-
case languageOption voptions of
Expand All @@ -87,6 +94,21 @@ handleTldrOpts opts@TldrOpts {..} =
ViewPage (englishViewOptions voptions) pages
})

-- We update if the data directory does not exist.
-- We also update if the cached pages version is older than 7 days.
-- TODO: Make the auto-update interval configurable.
-- TODO: Add command line option to skip auto update.
updateNecessary :: IO Bool
updateNecessary = do
dataDir <- getXdgDirectory XdgData tldrDirName
dataDirExists <- doesDirectoryExist dataDir
if not dataDirExists
then return True
else do
lastCachedTime <- getModificationTime dataDir
currentTime <- getCurrentTime
return $ currentTime `diffUTCTime` lastCachedTime > 7 * nominalDay

updateTldrPages :: IO ()
updateTldrPages = do
dataDir <- getXdgDirectory XdgData tldrDirName
Expand Down
9 changes: 5 additions & 4 deletions tldr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 21691ba2cc19cdb8ae6d8a3bd0e84803e28b8866530a0da9fe44e6187df4fec1
-- hash: 15cea8fa6220a4fef978ba47b6907236174a550cef396d6b25755d1549b6a5a5

name: tldr
version: 0.8.1
Expand Down Expand Up @@ -50,7 +50,7 @@ library
Paths_tldr
hs-source-dirs:
src
ghc-options: -Wall
ghc-options: -Wall -O2
build-depends:
ansi-terminal
, base >=4.7 && <5
Expand All @@ -63,6 +63,7 @@ library
, optparse-applicative
, semigroups
, text
, time
, zip-archive
default-language: Haskell2010

Expand All @@ -72,7 +73,7 @@ executable tldr
Paths_tldr
hs-source-dirs:
app
ghc-options: -Wall
ghc-options: -Wall -O2
build-depends:
base
, tldr
Expand All @@ -90,7 +91,7 @@ test-suite tldr-test
Paths_tldr
hs-source-dirs:
test
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
ghc-options: -Wall -O2 -threaded -rtsopts -with-rtsopts=-N
build-depends:
base
, tasty
Expand Down

0 comments on commit bfad30d

Please sign in to comment.