adds bash, fish, and zsh completions

This commit is contained in:
Keagan McClelland
2022-05-26 18:08:43 -06:00
parent 649a86fd5f
commit 3d8b4057df

View File

@@ -33,6 +33,7 @@ import Data.ByteArray.Encoding ( Base(..)
) )
import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as LB import qualified Data.ByteString.Lazy as LB
import Data.Conduit.Process ( system )
import Data.Default import Data.Default
import Data.Functor.Contravariant ( contramap ) import Data.Functor.Contravariant ( contramap )
import Data.HashMap.Internal.Strict ( HashMap import Data.HashMap.Internal.Strict ( HashMap
@@ -42,6 +43,7 @@ import Data.HashMap.Internal.Strict ( HashMap
, lookup , lookup
, traverseWithKey , traverseWithKey
) )
import Data.Text ( toLower )
import Dhall hiding ( void ) import Dhall hiding ( void )
import Dhall.Core ( pretty ) import Dhall.Core ( pretty )
import Handler.Admin ( IndexPkgReq(IndexPkgReq) import Handler.Admin ( IndexPkgReq(IndexPkgReq)
@@ -108,22 +110,29 @@ import Startlude ( ($)
, Semigroup((<>)) , Semigroup((<>))
, Show , Show
, String , String
, appendFile
, asum
, const , const
, decodeUtf8 , decodeUtf8
, exitWith , exitWith
, filter , filter
, flip , flip
, fmap , fmap
, for
, for_ , for_
, fromIntegral , fromIntegral
, fromMaybe , fromMaybe
, fst
, headMay
, panic , panic
, putStrLn , putStrLn
, show , show
, snd
, unlessM , unlessM
, void , void
, when , when
, writeFile , writeFile
, zip
) )
import System.Directory ( createDirectoryIfMissing import System.Directory ( createDirectoryIfMissing
, doesPathExist , doesPathExist
@@ -136,6 +145,7 @@ import System.FilePath ( (</>)
, takeDirectory , takeDirectory
, takeExtension , takeExtension
) )
import System.Process ( callCommand )
import System.ProgressBar ( Progress(..) import System.ProgressBar ( Progress(..)
, defStyle , defStyle
, newProgressBar , newProgressBar
@@ -181,8 +191,9 @@ instance ToDhall URI where
instance IsString URI where instance IsString URI where
fromString = fromMaybe (panic "Invalid URI for publish target") . parseURI fromString = fromMaybe (panic "Invalid URI for publish target") . parseURI
data Shell = Bash | Fish | Zsh deriving Show
data Command data Command
= CmdInit = CmdInit (Maybe Shell)
| CmdRegAdd String PublishCfgRepo | CmdRegAdd String PublishCfgRepo
| CmdRegDel String | CmdRegDel String
| CmdRegList | CmdRegList
@@ -194,9 +205,11 @@ data Command
cfgLocation :: IO FilePath cfgLocation :: IO FilePath
cfgLocation = getHomeDirectory <&> \d -> d </> ".embassy/publish.dhall" cfgLocation = getHomeDirectory <&> \d -> d </> ".embassy/publish.dhall"
parseInit :: Parser () parseInit :: Parser (Maybe Shell)
parseInit = parseInit = subparser $ command "init" (info go $ progDesc "Initializes embassy-publish config") <> metavar "init"
subparser $ command "init" (info (pure ()) $ progDesc "Initializes embassy-publish config") <> metavar "init" where
shells = [Bash, Fish, Zsh]
go = headMay . fmap fst . filter snd . zip shells <$> for shells (switch . long . toS . toLower . show)
parsePublish :: Parser Upload parsePublish :: Parser Upload
parsePublish = subparser $ command "upload" (info go $ progDesc "Publishes a .s9pk to a remote registry") <> metavar parsePublish = subparser $ command "upload" (info go $ progDesc "Publishes a .s9pk to a remote registry") <> metavar
@@ -271,7 +284,7 @@ parseListUnindexed = subparser $ command
parseCommand :: Parser Command parseCommand :: Parser Command
parseCommand = parseCommand =
(parseInit $> CmdInit) (CmdInit <$> parseInit)
<|> (CmdUpload <$> parsePublish) <|> (CmdUpload <$> parsePublish)
<|> subparser (command "reg" (info reg $ progDesc "Manage configured registries") <> metavar "reg") <|> subparser (command "reg" (info reg $ progDesc "Manage configured registries") <> metavar "reg")
<|> parseIndex <|> parseIndex
@@ -286,7 +299,7 @@ cliMain :: IO ()
cliMain = cliMain =
execParser opts execParser opts
>>= (\case >>= (\case
CmdInit -> init CmdInit sh -> init sh
CmdRegAdd s pcr -> regAdd s pcr CmdRegAdd s pcr -> regAdd s pcr
CmdRegDel s -> regRm s CmdRegDel s -> regRm s
CmdRegList -> regLs CmdRegList -> regLs
@@ -295,11 +308,25 @@ cliMain =
CmdListUnindexed name -> listUnindexed name CmdListUnindexed name -> listUnindexed name
) )
init :: IO () init :: Maybe Shell -> IO ()
init = do init sh = do
loc <- cfgLocation loc <- cfgLocation
createDirectoryIfMissing True (takeDirectory loc) createDirectoryIfMissing True (takeDirectory loc)
unlessM (doesPathExist loc) $ writeFile loc (pretty $ embed inject (def @PublishCfg)) unlessM (doesPathExist loc) $ do
writeFile loc (pretty $ embed inject (def @PublishCfg))
home <- getHomeDirectory
for_ sh $ \case
Bash -> do
let bashrc = home </> ".bashrc"
appendFile bashrc "source <(embassy-publish --bash-completion-script `which embassy-publish`)\n"
Fish -> do
let fishrc = home </> ".config" </> "fish" </> "config.fish"
appendFile fishrc "source <(embassy-publish --fish-compltion-script `which embassy-publish`)\n"
Zsh -> do
let zshrc = home </> ".zshrc"
appendFile zshrc "source <(embassy-publish --zsh-completion-script `which embassy-publish`)\n"
regAdd :: String -> PublishCfgRepo -> IO () regAdd :: String -> PublishCfgRepo -> IO ()
regAdd name val = do regAdd name val = do