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

Pack into a single command with subcommands, build with GHC 8.10, etc. #41

Merged
merged 4 commits into from May 16, 2020
Merged
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
46 changes: 41 additions & 5 deletions app/Main.hs
Expand Up @@ -23,15 +23,13 @@ limitations under the License.
import Control.Applicative ((<|>))
import Control.Arrow as Arrow
import Control.Exception (bracket)
import Control.Monad (when)
import Control.Monad (unless, when)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Reader (runReaderT)
import qualified Data.Aeson.Encode.Pretty as Json
import qualified Data.ByteString.Lazy as BL
import qualified Data.HashMap.Strict as HM
import Data.List (unzip)
import Data.Maybe (fromMaybe, maybeToList)
import Data.Monoid ((<>))
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Time.Calendar (fromGregorian)
Expand All @@ -41,6 +39,7 @@ import Data.Yaml as Yaml
import Safe (headMay)
import qualified System.Directory as Dir
import System.Envy (FromEnv, decodeEnv, env, fromEnv)
import System.Exit (die)
import System.FilePath ((</>))
import System.IO (BufferMode (NoBuffering), hGetEcho,
hPrint, hPutStrLn, hSetBuffering,
Expand All @@ -53,11 +52,14 @@ import qualified Web.Slack.User as User
import SlackLog.Html
import SlackLog.Pagination (chooseLatestPageOf, defaultPageSize,
paginateFiles)
import SlackLog.Types (ChannelId, TargetChannels,
TargetChannel(..),
import SlackLog.Types (ChannelId, TargetChannel (..),
TargetChannels,
Visibility (Private, Public),
targetChannels)
import SlackLog.Util (failWhenLeft, readJsonFile)
import UI.Butcher.Monadic (addCmd, addCmdImpl, addHelpCommand,
addSimpleBoolFlag, flagHelpStr,
mainFromCmdParserWithHelpDesc)
import Web.Slack.Instances ()


Expand All @@ -84,7 +86,17 @@ main = do
hSetBuffering stdin NoBuffering
hSetBuffering stdout NoBuffering
hSetBuffering stderr NoBuffering
mainFromCmdParserWithHelpDesc $ \helpDesc -> do
addHelpCommand helpDesc
addCmd "save" $ addCmdImpl saveCmd
addCmd "generate-html" $ do
onlyIndex <- addSimpleBoolFlag "i" ["only-index"] $ flagHelpStr "generates only index.html"
addCmdImpl $ generateHtmlCmd onlyIndex
addCmd "paginate-json" $ addCmdImpl paginateJsonCmd


saveCmd :: IO ()
saveCmd = do
config <- Yaml.decodeFileThrow "slack-log.yaml"
apiConfig <- Slack.mkSlackConfig =<< slackApiToken <$> (failWhenLeft =<< decodeEnv)

Expand Down Expand Up @@ -114,6 +126,30 @@ main = do
generateIndexHtml ws newNames


generateHtmlCmd :: Bool -> IO ()
generateHtmlCmd onlyIndex = do
logConfig <- Yaml.decodeFileThrow "slack-log.yaml"
Dir.withCurrentDirectory "docs" $ do
ws <- loadWorkspaceInfo logConfig "json"

namesByChannel <- for (HM.keys $ targetChannels logConfig) $ \chanId -> do
jsonPaths <- collectTargetJsons chanId
unless onlyIndex $
convertJsonsInChannel ws chanId jsonPaths
return (chanId, jsonPaths)

generateIndexHtml ws namesByChannel


paginateJsonCmd :: IO ()
paginateJsonCmd =
die $ unlines
[ "Sorry, this feature is currently disabled!"
, "Related issue: https://github.com/haskell-jp/slack-log/issues/40."
, "If you're interested in the original source code, see https://github.com/haskell-jp/slack-log/blob/1c155c0ca0860c6a0e8394b8e1a29de6cc00b245/app/paginate-old-jsons.hs"
]


saveUsersList :: Slack.SlackConfig -> IO ()
saveUsersList apiConfig =
Slack.usersList
Expand Down
35 changes: 0 additions & 35 deletions app/convert-old-jsons-to-htmls.hs

This file was deleted.

36 changes: 0 additions & 36 deletions app/paginate-old-jsons.hs

This file was deleted.

11 changes: 1 addition & 10 deletions package.yaml
Expand Up @@ -23,6 +23,7 @@ dependencies:
- aeson
- aeson-pretty
- bytestring
- butcher
- directory
- envy
- extra
Expand Down Expand Up @@ -50,16 +51,6 @@ executables:
source-dirs: app
dependencies:
- slack-log
paginate-old-jsons:
main: paginate-old-jsons.hs
source-dirs: app
dependencies:
- slack-log
convert-old-jsons-to-htmls:
main: convert-old-jsons-to-htmls.hs
source-dirs: app
dependencies:
- slack-log

tests:
slack-log-test:
Expand Down
2 changes: 1 addition & 1 deletion run.sample.sh
Expand Up @@ -5,7 +5,7 @@ set -eu
cd "$(dirname "$0")"

export SLACK_API_TOKEN='<ENTER_YOUR_LEGACY_API_TOKEN>'
stack build --exec slack-log
stack build --exec 'slack-log save'

git add docs
git commit -m"Slack log update at $(date)"
Expand Down
2 changes: 1 addition & 1 deletion src/SlackLog/Pagination.hs
Expand Up @@ -16,7 +16,7 @@ module SlackLog.Pagination
where


import Control.Monad.Fail (MonadFail, fail)
import Control.Monad.Fail (fail)
import Data.Aeson (FromJSON, ToJSON, eitherDecode)
import Data.Aeson.Encode.Pretty (encodePretty)
import qualified Data.ByteString as B
Expand Down
1 change: 0 additions & 1 deletion src/Web/Slack/Instances.hs
Expand Up @@ -11,7 +11,6 @@ import qualified Data.Aeson as Json
import Data.Maybe (fromJust)
import qualified Data.Text as T
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Data.Word (Word)
import qualified Test.QuickCheck as Q
import Test.QuickCheck.Instances ()
import qualified Web.Slack.Common as Slack
Expand Down