|
| 1 | +{-# LANGUAGE QuasiQuotes #-} |
| 2 | +module Elegit.Cli.Action.AcquireRepository |
| 3 | + ( cli |
| 4 | + , cmd |
| 5 | + ) where |
| 6 | + |
| 7 | +import Control.Monad.Free.Class |
| 8 | +import Data.String.QQ |
| 9 | +import Elegit.Cli.Command |
| 10 | +import qualified Elegit.Git.Action as GA |
| 11 | +import Fmt |
| 12 | +import Options.Applicative |
| 13 | +import qualified Options.Applicative.Help.Pretty as OA |
| 14 | +import Universum |
| 15 | + |
| 16 | + |
| 17 | +site:: Text |
| 18 | +site = "placeholder" |
| 19 | + |
| 20 | +purpose :: OA.Doc |
| 21 | +purpose = OA.text "Configures the current local Git repository." |
| 22 | + |
| 23 | +description :: OA.Doc |
| 24 | +description = OA.string $ [s| |
| 25 | +Applies the "basics", "standards", "aliases", and "signature" configurations |
| 26 | +to the current Git repository using `git config --local`. The command asks to |
| 27 | +provide information that is needed for the current repository configuration. |
| 28 | + |
| 29 | +The behavior of the command varies depend on `git elegant acquire-git` |
| 30 | +execution (a global configuration). If the global configuration is applied, |
| 31 | +then this command configures repository-related staffs only, otherwise, it |
| 32 | +applies all configurations to the current local repository. |
| 33 | + |
| 34 | +To find out what will be configured, please visit |
| 35 | +|] ++ (fmt ""+|site|+"/en/latest/configuration/") |
| 36 | + |
| 37 | + |
| 38 | +cli :: Mod CommandFields ElegitCommand |
| 39 | +cli = command "acquire-repository" $ info (pure AcquireRepositoryCommand) $ |
| 40 | + mconcat [ progDescDoc (Just purpose ) |
| 41 | + , footerDoc (Just description ) |
| 42 | + ] |
| 43 | + |
| 44 | + |
| 45 | +data ConfigKey |
| 46 | + = UserNameKey |
| 47 | + | UserEmailKey |
| 48 | + | CoreEditorKey |
| 49 | + | DefaultBranchKey |
| 50 | + | ProtectedBranchesKey |
| 51 | + |
| 52 | + |
| 53 | +configName :: ConfigKey -> Text |
| 54 | +configName UserNameKey = "user.name" |
| 55 | +configName UserEmailKey = "user.email" |
| 56 | +configName CoreEditorKey = "core.editor" |
| 57 | +configName DefaultBranchKey = "elegant-git.default-branch" |
| 58 | +configName ProtectedBranchesKey = "elegant-git.protected-branches" |
| 59 | + |
| 60 | + |
| 61 | +configPrompt :: ConfigKey -> Text |
| 62 | +configPrompt UserNameKey = "What is your user name?" |
| 63 | +configPrompt UserEmailKey = "What is your email?" |
| 64 | +configPrompt CoreEditorKey = "What is the command to launching an editor?" |
| 65 | +configPrompt DefaultBranchKey = "What is the default branch?" |
| 66 | +configPrompt ProtectedBranchesKey = "What are protected branches (split with space)" |
| 67 | + |
| 68 | + |
| 69 | +configDefault :: (MonadFree GA.GitF m) => ConfigKey -> m (Maybe Text) |
| 70 | +configDefault cKey = case cKey of |
| 71 | + UserNameKey -> getFromConfig |
| 72 | + UserEmailKey -> getFromConfig |
| 73 | + CoreEditorKey -> getFromConfig |
| 74 | + DefaultBranchKey -> return $ Just "master" |
| 75 | + ProtectedBranchesKey -> return $ Just "master" |
| 76 | + |
| 77 | + where |
| 78 | + getFromConfig :: (MonadFree GA.GitF m) => m (Maybe Text) |
| 79 | + getFromConfig = GA.readConfig GA.AutoConfig (configName cKey) |
| 80 | + |
| 81 | + |
| 82 | +configureBasics :: (MonadFree GA.GitF m) => GA.ConfigScope -> m () |
| 83 | +configureBasics cScope = do |
| 84 | + for_ basicConfigs $ \cKey -> do |
| 85 | + keyDefault <- configDefault cKey |
| 86 | + newValue <- GA.promptDefault (configPrompt cKey) keyDefault |
| 87 | + GA.setConfigVerbose cScope (configName cKey) newValue |
| 88 | + |
| 89 | + where |
| 90 | + basicConfigs :: [ConfigKey] |
| 91 | + basicConfigs = |
| 92 | + [ UserNameKey |
| 93 | + , UserEmailKey |
| 94 | + , CoreEditorKey |
| 95 | + , DefaultBranchKey |
| 96 | + , ProtectedBranchesKey |
| 97 | + ] |
| 98 | + |
| 99 | + |
| 100 | +configureStandards :: (MonadFree GA.GitF m) => GA.ConfigScope -> m () |
| 101 | +configureStandards cScope = |
| 102 | + for_ standardConfigs $ \(cKey,cValue) -> do |
| 103 | + GA.setConfigVerbose cScope cKey cValue |
| 104 | + where |
| 105 | + standardConfigs :: [(Text, Text)] |
| 106 | + standardConfigs = |
| 107 | + [ ("core.commentChar", "|") |
| 108 | + , ("apply.whitespace", "fix") |
| 109 | + , ("fetch.prune", "true") |
| 110 | + , ("fetch.pruneTags", "false") |
| 111 | + , ("core.autocrlf", "input") |
| 112 | + , ("pull.rebase", "true") |
| 113 | + , ("rebase.autoStash", "false") |
| 114 | + , ("credential.helper", "osxkeychain") |
| 115 | + ] |
| 116 | + |
| 117 | + |
| 118 | +-- | Execution description of the AcquireRepository action |
| 119 | +cmd :: (MonadFree GA.GitF m) => m () |
| 120 | +cmd = do |
| 121 | + GA.removeObsoleteConfiguration GA.LocalConfig |
| 122 | + GA.print =<< GA.formatInfoBox "Configuring basics..." |
| 123 | + configureBasics GA.LocalConfig |
| 124 | + unlessM GA.isGitAcquired $ do |
| 125 | + GA.print =<< GA.formatInfoBox "Configuring standards..." |
| 126 | + configureStandards GA.LocalConfig |
| 127 | + -- GA.print =<< GA.formatInfoBox "Configuring aliases..." |
| 128 | + -- TODO: Setup aliases |
| 129 | + -- GA.print =<< GA.formatInfoBox "Configuring signature..." |
| 130 | + -- TODO: Setup gpg key |
0 commit comments