summaryrefslogtreecommitdiffstats
path: root/src/auth.rs
diff options
context:
space:
mode:
authorCyborus <cyborus@cyborus.xyz>2023-11-17 20:18:48 +0100
committerCyborus <cyborus@cyborus.xyz>2023-11-17 20:18:48 +0100
commit7a525a5f3f0c73241f4c26da3ef8fe199ec04206 (patch)
treefe6946f554de8fe2aa5f4b6cd4bbc2f5df147e62 /src/auth.rs
parentMerge pull request 'move `forgejo-api` to its own repo' (#1) from api-repo in... (diff)
downloadforgejo-cli-7a525a5f3f0c73241f4c26da3ef8fe199ec04206.tar.xz
forgejo-cli-7a525a5f3f0c73241f4c26da3ef8fe199ec04206.zip
improve organization
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/auth.rs b/src/auth.rs
new file mode 100644
index 0000000..b9f4e49
--- /dev/null
+++ b/src/auth.rs
@@ -0,0 +1,59 @@
+use clap::Subcommand;
+
+#[derive(Subcommand, Clone, Debug)]
+pub enum AuthCommand {
+ Login,
+ Logout {
+ host: String,
+ },
+ AddKey {
+ /// The domain name of the forgejo instance.
+ host: String,
+ /// The user that the key is associated with
+ user: String,
+ /// The key to add. If not present, the key will be read in from stdin.
+ key: Option<String>,
+ },
+ List,
+}
+
+impl AuthCommand {
+ pub async fn run(self, keys: &mut crate::KeyInfo) -> eyre::Result<()> {
+ match self {
+ AuthCommand::Login => {
+ todo!();
+ // let user = readline("username: ").await?;
+ // let pass = readline("password: ").await?;
+ }
+ AuthCommand::Logout { host } => {
+ let info_opt = keys.hosts.remove(&host);
+ if let Some(info) = info_opt {
+ eprintln!("signed out of {}@{}", &info.username(), host);
+ } else {
+ eprintln!("already not signed in to {host}");
+ }
+ }
+ AuthCommand::AddKey { host, user, key } => {
+ let key = match key {
+ Some(key) => key,
+ None => crate::readline("new key: ").await?,
+ };
+ if keys.hosts.get(&user).is_none() {
+ keys.hosts.insert(host, crate::keys::LoginInfo::new(user, key));
+ } else {
+ println!("key for {} already exists", host);
+ }
+ }
+ AuthCommand::List => {
+ if keys.hosts.is_empty() {
+ println!("No logins.");
+ }
+ for (host_url, login_info) in &keys.hosts {
+ println!("{}@{}", login_info.username(), host_url);
+ }
+ }
+ }
+ Ok(())
+ }
+}
+