Main.hs 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
{-|
Module      : Main.hs
Description : Gargantext Import Corpus
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX

Import a corpus binary.

 -}

{-# LANGUAGE Strict            #-}

module Main where

18
import Data.Either (Either(..))
19
import Gargantext.API.Dev (withDevEnv, runCmdDev)
20
import Gargantext.API.Node () -- instances only
21
import Gargantext.API.Prelude (GargError)
22
import Gargantext.Core.Types.Individu (User(..), arbitraryNewUsers, NewUser(..), arbitraryUsername, GargPassword(..))
23 24
import Gargantext.Database.Action.Flow (getOrMkRoot, getOrMk_RootWithCorpus)
import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
25
import Gargantext.Database.Admin.Trigger.Init (initFirstTriggers, initLastTriggers)
26
import Gargantext.Database.Admin.Types.Hyperdata (HyperdataCorpus)
27
import Gargantext.Database.Admin.Types.Node
28
import Gargantext.Database.Prelude (Cmd, )
29 30
import Gargantext.Database.Query.Table.Node (getOrMkList)
import Gargantext.Database.Query.Table.User (insertNewUsers, )
31
import Gargantext.Prelude
32
import Gargantext.Prelude.Config (GargConfig(..), readConfig)
33
import Prelude (getLine)
34
import System.Environment (getArgs)
35

Alexandre Delanoë's avatar
Alexandre Delanoë committed
36

37 38
main :: IO ()
main = do
39 40 41 42 43
  params@[iniPath] <- getArgs

  _ <- if length params /= 1
      then panic "USAGE: ./gargantext-init gargantext.ini"
      else pure ()
44

45 46 47 48 49 50
  putStrLn "Enter master user (gargantua) _password_ :"
  password  <- getLine

  putStrLn "Enter master user (gargantua) _email_ :"
  email     <- getLine

51 52
  cfg       <- readConfig         iniPath
  let secret = _gc_secretkey cfg
53

54
  let createUsers :: Cmd GargError Int64
55 56 57
      createUsers = insertNewUsers (NewUser "gargantua" (cs email) (GargPassword $ cs password)
                                   : arbitraryNewUsers
                                   )
Alexandre Delanoë's avatar
Alexandre Delanoë committed
58

59
  let
60
    mkRoots :: Cmd GargError [(UserId, RootId)]
61
    mkRoots = mapM getOrMkRoot $ map UserName ("gargantua" : arbitraryUsername)
62
    -- TODO create all users roots
63

64 65 66
  let
    initMaster :: Cmd GargError (UserId, RootId, CorpusId, ListId)
    initMaster = do
67 68 69 70
      (masterUserId, masterRootId, masterCorpusId)
                  <- getOrMk_RootWithCorpus (UserName userMaster)
                                            (Left corpusMasterName)
                                            (Nothing :: Maybe HyperdataCorpus)
71
      masterListId <- getOrMkList masterCorpusId masterUserId
72
      _triggers    <- initLastTriggers masterListId
73
      pure (masterUserId, masterRootId, masterCorpusId, masterListId)
74 75

  withDevEnv iniPath $ \env -> do
Alexandre Delanoë's avatar
Alexandre Delanoë committed
76
    _ <- runCmdDev env (initFirstTriggers secret :: Cmd GargError [Int64])
77
    _ <- runCmdDev env createUsers
78
    x <- runCmdDev env initMaster
Alexandre Delanoë's avatar
Alexandre Delanoë committed
79
    _ <- runCmdDev env mkRoots
80
    putStrLn $ show x
81
    pure ()