module Cachix.Client.OptionsParser
  ( CachixCommand (..),
    CachixOptions (..),
    PushArguments (..),
    PushOptions (..),
    BinaryCacheName,
    getOpts,
  )
where

import qualified Cachix.Client.Config as Config
import qualified Cachix.Client.InstallationMode as InstallationMode
import Cachix.Client.URI (defaultCachixURI)
import Options.Applicative
import Protolude hiding (option)
import URI.ByteString
  ( Absolute,
    URIRef,
    parseURI,
    serializeURIRef',
    strictURIParserOptions,
  )

data CachixOptions
  = CachixOptions
      { CachixOptions -> URIRef Absolute
host :: URIRef Absolute,
        CachixOptions -> ConfigPath
configPath :: Config.ConfigPath,
        CachixOptions -> Bool
verbose :: Bool
      }
  deriving (Int -> CachixOptions -> ShowS
[CachixOptions] -> ShowS
CachixOptions -> ConfigPath
(Int -> CachixOptions -> ShowS)
-> (CachixOptions -> ConfigPath)
-> ([CachixOptions] -> ShowS)
-> Show CachixOptions
forall a.
(Int -> a -> ShowS)
-> (a -> ConfigPath) -> ([a] -> ShowS) -> Show a
showList :: [CachixOptions] -> ShowS
$cshowList :: [CachixOptions] -> ShowS
show :: CachixOptions -> ConfigPath
$cshow :: CachixOptions -> ConfigPath
showsPrec :: Int -> CachixOptions -> ShowS
$cshowsPrec :: Int -> CachixOptions -> ShowS
Show)

parserCachixOptions :: Config.ConfigPath -> Parser CachixOptions
parserCachixOptions :: ConfigPath -> Parser CachixOptions
parserCachixOptions defaultConfigPath :: ConfigPath
defaultConfigPath =
  URIRef Absolute -> ConfigPath -> Bool -> CachixOptions
CachixOptions
    (URIRef Absolute -> ConfigPath -> Bool -> CachixOptions)
-> Parser (URIRef Absolute)
-> Parser (ConfigPath -> Bool -> CachixOptions)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute) -> Parser (URIRef Absolute)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option
      ReadM (URIRef Absolute)
uriOption
      ( ConfigPath -> Mod OptionFields (URIRef Absolute)
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "host"
          Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
forall a. Semigroup a => a -> a -> a
<> URIRef Absolute -> Mod OptionFields (URIRef Absolute)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value URIRef Absolute
defaultCachixURI
          Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields (URIRef Absolute)
forall (f :: * -> *) a. HasMetavar f => ConfigPath -> Mod f a
metavar "URI"
          Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
forall a. Semigroup a => a -> a -> a
<> (URIRef Absolute -> ConfigPath)
-> Mod OptionFields (URIRef Absolute)
forall a (f :: * -> *). (a -> ConfigPath) -> Mod f a
showDefaultWith (ByteString -> ConfigPath
forall a b. StringConv a b => a -> b
toS (ByteString -> ConfigPath)
-> (URIRef Absolute -> ByteString) -> URIRef Absolute -> ConfigPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URIRef Absolute -> ByteString
forall a. URIRef a -> ByteString
serializeURIRef')
          Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
-> Mod OptionFields (URIRef Absolute)
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields (URIRef Absolute)
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Host to connect to"
      )
    Parser (ConfigPath -> Bool -> CachixOptions)
-> Parser ConfigPath -> Parser (Bool -> CachixOptions)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod OptionFields ConfigPath -> Parser ConfigPath
forall s. IsString s => Mod OptionFields s -> Parser s
strOption
      ( ConfigPath -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "config"
          Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'c'
          Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value ConfigPath
defaultConfigPath
          Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. HasMetavar f => ConfigPath -> Mod f a
metavar "CONFIGPATH"
          Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields ConfigPath
forall a (f :: * -> *). Show a => Mod f a
showDefault
          Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Cachix configuration file"
      )
    Parser (Bool -> CachixOptions)
-> Parser Bool -> Parser CachixOptions
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
      ( ConfigPath -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "verbose"
          Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> Char -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'v'
          Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod FlagFields Bool
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Verbose mode"
      )

uriOption :: ReadM (URIRef Absolute)
uriOption :: ReadM (URIRef Absolute)
uriOption = (ConfigPath -> Either ConfigPath (URIRef Absolute))
-> ReadM (URIRef Absolute)
forall a. (ConfigPath -> Either ConfigPath a) -> ReadM a
eitherReader ((ConfigPath -> Either ConfigPath (URIRef Absolute))
 -> ReadM (URIRef Absolute))
-> (ConfigPath -> Either ConfigPath (URIRef Absolute))
-> ReadM (URIRef Absolute)
forall a b. (a -> b) -> a -> b
$ \s :: ConfigPath
s ->
  (URIParseError -> ConfigPath)
-> Either URIParseError (URIRef Absolute)
-> Either ConfigPath (URIRef Absolute)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first URIParseError -> ConfigPath
forall a b. (Show a, StringConv ConfigPath b) => a -> b
show (Either URIParseError (URIRef Absolute)
 -> Either ConfigPath (URIRef Absolute))
-> Either URIParseError (URIRef Absolute)
-> Either ConfigPath (URIRef Absolute)
forall a b. (a -> b) -> a -> b
$ URIParserOptions
-> ByteString -> Either URIParseError (URIRef Absolute)
parseURI URIParserOptions
strictURIParserOptions (ByteString -> Either URIParseError (URIRef Absolute))
-> ByteString -> Either URIParseError (URIRef Absolute)
forall a b. (a -> b) -> a -> b
$ ConfigPath -> ByteString
forall a b. StringConv a b => a -> b
toS ConfigPath
s

type BinaryCacheName = Text

data CachixCommand
  = AuthToken Text
  | Create BinaryCacheName
  | GenerateKeypair BinaryCacheName
  | Push PushArguments
  | Use BinaryCacheName InstallationMode.UseOptions
  | Version
  deriving (Int -> CachixCommand -> ShowS
[CachixCommand] -> ShowS
CachixCommand -> ConfigPath
(Int -> CachixCommand -> ShowS)
-> (CachixCommand -> ConfigPath)
-> ([CachixCommand] -> ShowS)
-> Show CachixCommand
forall a.
(Int -> a -> ShowS)
-> (a -> ConfigPath) -> ([a] -> ShowS) -> Show a
showList :: [CachixCommand] -> ShowS
$cshowList :: [CachixCommand] -> ShowS
show :: CachixCommand -> ConfigPath
$cshow :: CachixCommand -> ConfigPath
showsPrec :: Int -> CachixCommand -> ShowS
$cshowsPrec :: Int -> CachixCommand -> ShowS
Show)

data PushArguments
  = PushPaths PushOptions Text [Text]
  | PushWatchStore PushOptions Text
  deriving (Int -> PushArguments -> ShowS
[PushArguments] -> ShowS
PushArguments -> ConfigPath
(Int -> PushArguments -> ShowS)
-> (PushArguments -> ConfigPath)
-> ([PushArguments] -> ShowS)
-> Show PushArguments
forall a.
(Int -> a -> ShowS)
-> (a -> ConfigPath) -> ([a] -> ShowS) -> Show a
showList :: [PushArguments] -> ShowS
$cshowList :: [PushArguments] -> ShowS
show :: PushArguments -> ConfigPath
$cshow :: PushArguments -> ConfigPath
showsPrec :: Int -> PushArguments -> ShowS
$cshowsPrec :: Int -> PushArguments -> ShowS
Show)

data PushOptions
  = PushOptions
      { PushOptions -> Int
compressionLevel :: Int,
        PushOptions -> Int
numJobs :: Int,
        PushOptions -> Bool
omitDeriver :: Bool
      }
  deriving (Int -> PushOptions -> ShowS
[PushOptions] -> ShowS
PushOptions -> ConfigPath
(Int -> PushOptions -> ShowS)
-> (PushOptions -> ConfigPath)
-> ([PushOptions] -> ShowS)
-> Show PushOptions
forall a.
(Int -> a -> ShowS)
-> (a -> ConfigPath) -> ([a] -> ShowS) -> Show a
showList :: [PushOptions] -> ShowS
$cshowList :: [PushOptions] -> ShowS
show :: PushOptions -> ConfigPath
$cshow :: PushOptions -> ConfigPath
showsPrec :: Int -> PushOptions -> ShowS
$cshowsPrec :: Int -> PushOptions -> ShowS
Show)

parserCachixCommand :: Parser CachixCommand
parserCachixCommand :: Parser CachixCommand
parserCachixCommand =
  Mod CommandFields CachixCommand -> Parser CachixCommand
forall a. Mod CommandFields a -> Parser a
subparser (Mod CommandFields CachixCommand -> Parser CachixCommand)
-> Mod CommandFields CachixCommand -> Parser CachixCommand
forall a b. (a -> b) -> a -> b
$
    ConfigPath
-> ParserInfo CachixCommand -> Mod CommandFields CachixCommand
forall a. ConfigPath -> ParserInfo a -> Mod CommandFields a
command "authtoken" (Parser CachixCommand
-> InfoMod CachixCommand -> ParserInfo CachixCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
infoH Parser CachixCommand
authtoken (ConfigPath -> InfoMod CachixCommand
forall a. ConfigPath -> InfoMod a
progDesc "Configure token for authentication to cachix.org"))
      Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
forall a. Semigroup a => a -> a -> a
<> ConfigPath
-> ParserInfo CachixCommand -> Mod CommandFields CachixCommand
forall a. ConfigPath -> ParserInfo a -> Mod CommandFields a
command "create" (Parser CachixCommand
-> InfoMod CachixCommand -> ParserInfo CachixCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
infoH Parser CachixCommand
create (ConfigPath -> InfoMod CachixCommand
forall a. ConfigPath -> InfoMod a
progDesc "DEPRECATED: Go to https://cachix.org instead"))
      Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
forall a. Semigroup a => a -> a -> a
<> ConfigPath
-> ParserInfo CachixCommand -> Mod CommandFields CachixCommand
forall a. ConfigPath -> ParserInfo a -> Mod CommandFields a
command "generate-keypair" (Parser CachixCommand
-> InfoMod CachixCommand -> ParserInfo CachixCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
infoH Parser CachixCommand
generateKeypair (ConfigPath -> InfoMod CachixCommand
forall a. ConfigPath -> InfoMod a
progDesc "Generate keypair for a binary cache"))
      Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
forall a. Semigroup a => a -> a -> a
<> ConfigPath
-> ParserInfo CachixCommand -> Mod CommandFields CachixCommand
forall a. ConfigPath -> ParserInfo a -> Mod CommandFields a
command "push" (Parser CachixCommand
-> InfoMod CachixCommand -> ParserInfo CachixCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
infoH Parser CachixCommand
push (ConfigPath -> InfoMod CachixCommand
forall a. ConfigPath -> InfoMod a
progDesc "Upload Nix store paths to the binary cache"))
      Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
-> Mod CommandFields CachixCommand
forall a. Semigroup a => a -> a -> a
<> ConfigPath
-> ParserInfo CachixCommand -> Mod CommandFields CachixCommand
forall a. ConfigPath -> ParserInfo a -> Mod CommandFields a
command "use" (Parser CachixCommand
-> InfoMod CachixCommand -> ParserInfo CachixCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
infoH Parser CachixCommand
use (ConfigPath -> InfoMod CachixCommand
forall a. ConfigPath -> InfoMod a
progDesc "Configure nix.conf to enable binary cache during builds"))
  where
    nameArg :: Parser BinaryCacheName
nameArg = Mod ArgumentFields BinaryCacheName -> Parser BinaryCacheName
forall s. IsString s => Mod ArgumentFields s -> Parser s
strArgument (ConfigPath -> Mod ArgumentFields BinaryCacheName
forall (f :: * -> *) a. HasMetavar f => ConfigPath -> Mod f a
metavar "NAME")
    authtoken :: Parser CachixCommand
authtoken = BinaryCacheName -> CachixCommand
AuthToken (BinaryCacheName -> CachixCommand)
-> Parser BinaryCacheName -> Parser CachixCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mod ArgumentFields BinaryCacheName -> Parser BinaryCacheName
forall s. IsString s => Mod ArgumentFields s -> Parser s
strArgument (ConfigPath -> Mod ArgumentFields BinaryCacheName
forall (f :: * -> *) a. HasMetavar f => ConfigPath -> Mod f a
metavar "TOKEN")
    create :: Parser CachixCommand
create = BinaryCacheName -> CachixCommand
Create (BinaryCacheName -> CachixCommand)
-> Parser BinaryCacheName -> Parser CachixCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser BinaryCacheName
nameArg
    generateKeypair :: Parser CachixCommand
generateKeypair = BinaryCacheName -> CachixCommand
GenerateKeypair (BinaryCacheName -> CachixCommand)
-> Parser BinaryCacheName -> Parser CachixCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser BinaryCacheName
nameArg
    validatedLevel :: a -> ReadM a
validatedLevel l :: a
l =
      a
l a -> ReadM () -> ReadM a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> ReadM () -> ReadM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (a
l a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [0 .. 9]) (ConfigPath -> ReadM ()
forall a. ConfigPath -> ReadM a
readerError (ConfigPath -> ReadM ()) -> ConfigPath -> ReadM ()
forall a b. (a -> b) -> a -> b
$ "value " ConfigPath -> ShowS
forall a. Semigroup a => a -> a -> a
<> a -> ConfigPath
forall a b. (Show a, StringConv ConfigPath b) => a -> b
show a
l ConfigPath -> ShowS
forall a. Semigroup a => a -> a -> a
<> " not in expected range: [0..9]")
    pushOptions :: Parser PushOptions
    pushOptions :: Parser PushOptions
pushOptions =
      Int -> Int -> Bool -> PushOptions
PushOptions
        (Int -> Int -> Bool -> PushOptions)
-> Parser Int -> Parser (Int -> Bool -> PushOptions)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option
          (ReadM Int
forall a. Read a => ReadM a
auto ReadM Int -> (Int -> ReadM Int) -> ReadM Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> ReadM Int
forall a. (Eq a, Num a, Enum a, Show a) => a -> ReadM a
validatedLevel)
          ( ConfigPath -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "compression-level"
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'c'
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields Int
forall (f :: * -> *) a. HasMetavar f => ConfigPath -> Mod f a
metavar "[0..9]"
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields Int
forall (f :: * -> *) a. ConfigPath -> Mod f a
help
                "The compression level for XZ compression.\
                \ Take compressor *and* decompressor memory usage into account before using [7..9]!"
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields Int
forall a (f :: * -> *). Show a => Mod f a
showDefault
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Int -> Mod OptionFields Int
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value 2
          )
        Parser (Int -> Bool -> PushOptions)
-> Parser Int -> Parser (Bool -> PushOptions)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option
          ReadM Int
forall a. Read a => ReadM a
auto
          ( ConfigPath -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "jobs"
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'j'
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields Int
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Number of threads used for pushing store paths."
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields Int
forall a (f :: * -> *). Show a => Mod f a
showDefault
              Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Int -> Mod OptionFields Int
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value 4
          )
        Parser (Bool -> PushOptions) -> Parser Bool -> Parser PushOptions
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch (ConfigPath -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "omit-deriver" Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod FlagFields Bool
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Do not publish which derivations built the store paths.")
    push :: Parser CachixCommand
push = (\opts :: PushOptions
opts cache :: BinaryCacheName
cache f :: PushOptions -> BinaryCacheName -> PushArguments
f -> PushArguments -> CachixCommand
Push (PushArguments -> CachixCommand) -> PushArguments -> CachixCommand
forall a b. (a -> b) -> a -> b
$ PushOptions -> BinaryCacheName -> PushArguments
f PushOptions
opts BinaryCacheName
cache) (PushOptions
 -> BinaryCacheName
 -> (PushOptions -> BinaryCacheName -> PushArguments)
 -> CachixCommand)
-> Parser PushOptions
-> Parser
     (BinaryCacheName
      -> (PushOptions -> BinaryCacheName -> PushArguments)
      -> CachixCommand)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser PushOptions
pushOptions Parser
  (BinaryCacheName
   -> (PushOptions -> BinaryCacheName -> PushArguments)
   -> CachixCommand)
-> Parser BinaryCacheName
-> Parser
     ((PushOptions -> BinaryCacheName -> PushArguments)
      -> CachixCommand)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser BinaryCacheName
nameArg Parser
  ((PushOptions -> BinaryCacheName -> PushArguments)
   -> CachixCommand)
-> Parser (PushOptions -> BinaryCacheName -> PushArguments)
-> Parser CachixCommand
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser (PushOptions -> BinaryCacheName -> PushArguments)
pushPaths Parser (PushOptions -> BinaryCacheName -> PushArguments)
-> Parser (PushOptions -> BinaryCacheName -> PushArguments)
-> Parser (PushOptions -> BinaryCacheName -> PushArguments)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser (PushOptions -> BinaryCacheName -> PushArguments)
pushWatchStore)
    pushPaths :: Parser (PushOptions -> BinaryCacheName -> PushArguments)
pushPaths =
      (\paths :: [BinaryCacheName]
paths opts :: PushOptions
opts cache :: BinaryCacheName
cache -> PushOptions
-> BinaryCacheName -> [BinaryCacheName] -> PushArguments
PushPaths PushOptions
opts BinaryCacheName
cache [BinaryCacheName]
paths)
        ([BinaryCacheName]
 -> PushOptions -> BinaryCacheName -> PushArguments)
-> Parser [BinaryCacheName]
-> Parser (PushOptions -> BinaryCacheName -> PushArguments)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser BinaryCacheName -> Parser [BinaryCacheName]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Mod ArgumentFields BinaryCacheName -> Parser BinaryCacheName
forall s. IsString s => Mod ArgumentFields s -> Parser s
strArgument (ConfigPath -> Mod ArgumentFields BinaryCacheName
forall (f :: * -> *) a. HasMetavar f => ConfigPath -> Mod f a
metavar "PATHS..."))
    pushWatchStore :: Parser (PushOptions -> BinaryCacheName -> PushArguments)
pushWatchStore =
      (\() opts :: PushOptions
opts cache :: BinaryCacheName
cache -> PushOptions -> BinaryCacheName -> PushArguments
PushWatchStore PushOptions
opts BinaryCacheName
cache)
        (() -> PushOptions -> BinaryCacheName -> PushArguments)
-> Parser ()
-> Parser (PushOptions -> BinaryCacheName -> PushArguments)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> () -> Mod FlagFields () -> Parser ()
forall a. a -> Mod FlagFields a -> Parser a
flag'
          ()
          ( ConfigPath -> Mod FlagFields ()
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "watch-store"
              Mod FlagFields () -> Mod FlagFields () -> Mod FlagFields ()
forall a. Semigroup a => a -> a -> a
<> Char -> Mod FlagFields ()
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'w'
              Mod FlagFields () -> Mod FlagFields () -> Mod FlagFields ()
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod FlagFields ()
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Run in daemon mode and push store paths as they are added to /nix/store"
          )
    use :: Parser CachixCommand
use =
      BinaryCacheName -> UseOptions -> CachixCommand
Use (BinaryCacheName -> UseOptions -> CachixCommand)
-> Parser BinaryCacheName -> Parser (UseOptions -> CachixCommand)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser BinaryCacheName
nameArg
        Parser (UseOptions -> CachixCommand)
-> Parser UseOptions -> Parser CachixCommand
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ( Maybe InstallationMode -> ConfigPath -> UseOptions
InstallationMode.UseOptions
                (Maybe InstallationMode -> ConfigPath -> UseOptions)
-> Parser (Maybe InstallationMode)
-> Parser (ConfigPath -> UseOptions)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser InstallationMode -> Parser (Maybe InstallationMode)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional
                  ( ReadM InstallationMode
-> Mod OptionFields InstallationMode -> Parser InstallationMode
forall a. ReadM a -> Mod OptionFields a -> Parser a
option
                      ((ConfigPath -> Maybe InstallationMode) -> ReadM InstallationMode
forall a. (ConfigPath -> Maybe a) -> ReadM a
maybeReader ConfigPath -> Maybe InstallationMode
InstallationMode.fromString)
                      ( ConfigPath -> Mod OptionFields InstallationMode
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "mode"
                          Mod OptionFields InstallationMode
-> Mod OptionFields InstallationMode
-> Mod OptionFields InstallationMode
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields InstallationMode
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'm'
                          Mod OptionFields InstallationMode
-> Mod OptionFields InstallationMode
-> Mod OptionFields InstallationMode
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields InstallationMode
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Mode in which to configure binary caches for Nix. Supported values: nixos, root-nixconf, user-nixconf"
                      )
                  )
                Parser (ConfigPath -> UseOptions)
-> Parser ConfigPath -> Parser UseOptions
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod OptionFields ConfigPath -> Parser ConfigPath
forall s. IsString s => Mod OptionFields s -> Parser s
strOption
                  ( ConfigPath -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "nixos-folder"
                      Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'd'
                      Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Base directory for NixOS configuration"
                      Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod OptionFields ConfigPath
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value "/etc/nixos/"
                      Mod OptionFields ConfigPath
-> Mod OptionFields ConfigPath -> Mod OptionFields ConfigPath
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields ConfigPath
forall a (f :: * -> *). Show a => Mod f a
showDefault
                  )
            )

getOpts :: IO (CachixOptions, CachixCommand)
getOpts :: IO (CachixOptions, CachixCommand)
getOpts = do
  ConfigPath
configpath <- IO ConfigPath
Config.getDefaultFilename
  ParserPrefs
-> ParserInfo (CachixOptions, CachixCommand)
-> IO (CachixOptions, CachixCommand)
forall a. ParserPrefs -> ParserInfo a -> IO a
customExecParser (PrefsMod -> ParserPrefs
prefs PrefsMod
showHelpOnEmpty) (ConfigPath -> ParserInfo (CachixOptions, CachixCommand)
optsInfo ConfigPath
configpath)

optsInfo :: Config.ConfigPath -> ParserInfo (CachixOptions, CachixCommand)
optsInfo :: ConfigPath -> ParserInfo (CachixOptions, CachixCommand)
optsInfo configpath :: ConfigPath
configpath = Parser (CachixOptions, CachixCommand)
-> InfoMod (CachixOptions, CachixCommand)
-> ParserInfo (CachixOptions, CachixCommand)
forall a. Parser a -> InfoMod a -> ParserInfo a
infoH Parser (CachixOptions, CachixCommand)
parser InfoMod (CachixOptions, CachixCommand)
forall a. InfoMod a
desc
  where
    parser :: Parser (CachixOptions, CachixCommand)
parser = (,) (CachixOptions -> CachixCommand -> (CachixOptions, CachixCommand))
-> Parser CachixOptions
-> Parser (CachixCommand -> (CachixOptions, CachixCommand))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ConfigPath -> Parser CachixOptions
parserCachixOptions ConfigPath
configpath Parser (CachixCommand -> (CachixOptions, CachixCommand))
-> Parser CachixCommand -> Parser (CachixOptions, CachixCommand)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser CachixCommand
parserCachixCommand Parser CachixCommand
-> Parser CachixCommand -> Parser CachixCommand
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser CachixCommand
versionParser)
    versionParser :: Parser CachixCommand
    versionParser :: Parser CachixCommand
versionParser =
      CachixCommand
-> Mod FlagFields CachixCommand -> Parser CachixCommand
forall a. a -> Mod FlagFields a -> Parser a
flag'
        CachixCommand
Version
        ( ConfigPath -> Mod FlagFields CachixCommand
forall (f :: * -> *) a. HasName f => ConfigPath -> Mod f a
long "version"
            Mod FlagFields CachixCommand
-> Mod FlagFields CachixCommand -> Mod FlagFields CachixCommand
forall a. Semigroup a => a -> a -> a
<> Char -> Mod FlagFields CachixCommand
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short 'V'
            Mod FlagFields CachixCommand
-> Mod FlagFields CachixCommand -> Mod FlagFields CachixCommand
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> Mod FlagFields CachixCommand
forall (f :: * -> *) a. ConfigPath -> Mod f a
help "Show cachix version"
        )

desc :: InfoMod a
desc :: InfoMod a
desc =
  InfoMod a
forall a. InfoMod a
fullDesc
    InfoMod a -> InfoMod a -> InfoMod a
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> InfoMod a
forall a. ConfigPath -> InfoMod a
progDesc "Sign into https://cachix.org to get started."
    InfoMod a -> InfoMod a -> InfoMod a
forall a. Semigroup a => a -> a -> a
<> ConfigPath -> InfoMod a
forall a. ConfigPath -> InfoMod a
header "cachix.org command interface"

-- TODO: usage footer
infoH :: Parser a -> InfoMod a -> ParserInfo a
infoH :: Parser a -> InfoMod a -> ParserInfo a
infoH a :: Parser a
a = Parser a -> InfoMod a -> ParserInfo a
forall a. Parser a -> InfoMod a -> ParserInfo a
info (Parser (a -> a)
forall a. Parser (a -> a)
helper Parser (a -> a) -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser a
a)