Function git_credentials::program::main

source ·
pub fn main<CredentialsFn, E>(
    args: impl IntoIterator<Item = OsString>,
    stdin: impl Read,
    stdout: impl Write,
    credentials: CredentialsFn
) -> Result<(), Error>where
    CredentialsFn: FnOnce(Action, Context) -> Result<Option<Context>, E>,
    E: Error + Send + Sync + 'static,
Expand description

Invoke a custom credentials helper which receives program args, with the first argument being the action to perform (as opposed to the program name). Then read context information from stdin and if the action is Action::Get, then write the result to stdout. credentials is the API version of such call, whereOk(Some(context)) returns credentials, and Ok(None) indicates no credentials could be found for url, which is always set when called.

Call this function from a programs main, passing std::env::args_os(), stdin() and stdout accordingly, along with your own helper implementation.

Examples found in repository?
examples/custom-helper.rs (lines 5-23)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pub fn main() -> Result<(), git_credentials::program::main::Error> {
    git_credentials::program::main(
        std::env::args_os().skip(1),
        std::io::stdin(),
        std::io::stdout(),
        |action, context| -> std::io::Result<_> {
            match action {
                program::main::Action::Get => Ok(Some(protocol::Context {
                    username: Some("user".into()),
                    password: Some("pass".into()),
                    ..context
                })),
                program::main::Action::Erase => Err(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    "Refusing to delete credentials for demo purposes",
                )),
                program::main::Action::Store => Ok(None),
            }
        },
    )
}
More examples
Hide additional examples
examples/git-credential-lite.rs (lines 5-22)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pub fn main() -> Result<(), git_credentials::program::main::Error> {
    git_credentials::program::main(
        std::env::args_os().skip(1),
        std::io::stdin(),
        std::io::stdout(),
        |action, context| {
            use git_credentials::program::main::Action::*;
            git_credentials::helper::Cascade::default()
                .invoke(
                    match action {
                        Get => git_credentials::helper::Action::Get(context),
                        Erase => git_credentials::helper::Action::Erase(context.to_bstring()),
                        Store => git_credentials::helper::Action::Store(context.to_bstring()),
                    },
                    git_prompt::Options::default().apply_environment(true, true, true),
                )
                .map(|outcome| outcome.and_then(|outcome| (&outcome.next).try_into().ok()))
        },
    )
}