Support Forgejo servers
This commit is contained in:
@@ -4,7 +4,7 @@ mod work_items;
|
||||
use std::{env, error::Error, process};
|
||||
|
||||
use config::{Config, RepositoryScope, Selection, server_url};
|
||||
use gotcha_gitea::{Client, Method, Repository, ServerVersion, User};
|
||||
use gotcha_gitea::{Client, Method, Provider, Repository, ServerVersion, User};
|
||||
use serde_json::Value;
|
||||
|
||||
const ROOT_HELP: &str = "\
|
||||
@@ -15,13 +15,13 @@ Usage:
|
||||
|
||||
Commands:
|
||||
auth Manage server authentication
|
||||
server Inspect the selected Gitea server
|
||||
server Inspect the selected Gitea or Forgejo server
|
||||
user Work with the authenticated user
|
||||
repo Work with repositories
|
||||
issue Work with issues and comments
|
||||
milestone Work with repository milestones
|
||||
pull Work with pull requests
|
||||
api Send a raw Gitea API request
|
||||
api Send a raw server API request
|
||||
|
||||
Run `gotcha COMMAND` to list that command's subcommands.
|
||||
|
||||
@@ -33,7 +33,8 @@ const AUTH_HELP: &str = "\
|
||||
Usage: gotcha auth SUBCOMMAND
|
||||
|
||||
Subcommands:
|
||||
login SERVER Verify and store a token read from standard input
|
||||
login SERVER [--provider gitea|forgejo]
|
||||
Verify and store a token read from standard input
|
||||
list List configured servers without showing tokens
|
||||
status Show the selected server and repository scope
|
||||
logout SERVER Remove a stored server and token";
|
||||
@@ -42,7 +43,7 @@ const SERVER_HELP: &str = "\
|
||||
Usage: gotcha server SUBCOMMAND
|
||||
|
||||
Subcommands:
|
||||
version Show the selected Gitea server version";
|
||||
version Show the selected server version";
|
||||
|
||||
const USER_HELP: &str = "\
|
||||
Usage: gotcha user SUBCOMMAND
|
||||
@@ -82,24 +83,26 @@ async fn run() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
let mut config = Config::load()?;
|
||||
match args.command.as_slice() {
|
||||
[domain, command, name] if domain == "auth" && command == "login" => {
|
||||
let url = server_url(name)?;
|
||||
let token = rpassword::prompt_password("Token: ")?;
|
||||
if token.is_empty() {
|
||||
return Err("token must not be empty".into());
|
||||
}
|
||||
let user = Client::new(&url, Some(&token))?.current_user().await?;
|
||||
config.login(name, &token)?;
|
||||
println!(
|
||||
"Logged in to {url} as {} ({name}).",
|
||||
user.login.as_deref().unwrap_or("unknown")
|
||||
);
|
||||
return Ok(());
|
||||
if let Some((name, fallback)) = login_command(&args.command)? {
|
||||
let url = server_url(name)?;
|
||||
let token = rpassword::prompt_password("Token: ")?;
|
||||
if token.is_empty() {
|
||||
return Err("token must not be empty".into());
|
||||
}
|
||||
let client = Client::discover(&url, Some(&token), fallback).await?;
|
||||
let user = client.current_user().await?;
|
||||
config.login(name, &token, client.provider())?;
|
||||
println!(
|
||||
"Logged in to {url} as {} ({name}, {}).",
|
||||
user.login.as_deref().unwrap_or("unknown"),
|
||||
client.provider()
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
match args.command.as_slice() {
|
||||
[domain, command] if domain == "auth" && command == "list" => {
|
||||
for (name, server) in &config.servers {
|
||||
println!("{name}\t{}", server.url);
|
||||
println!("{name}\t{}\t{}", server.provider, server.url);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
@@ -118,7 +121,11 @@ async fn run() -> Result<(), Box<dyn Error>> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let client = Client::new(&selection.url, selection.token.as_deref())?;
|
||||
let client = Client::with_provider(
|
||||
&selection.url,
|
||||
selection.token.as_deref(),
|
||||
selection.provider,
|
||||
)?;
|
||||
if work_items::run(&args.command, &selection, &client).await? {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -160,6 +167,20 @@ fn print_version(version: &ServerVersion) {
|
||||
println!("{}", version.version.as_deref().unwrap_or("unknown"));
|
||||
}
|
||||
|
||||
fn login_command(command: &[String]) -> Result<Option<(&str, Provider)>, Box<dyn Error>> {
|
||||
match command {
|
||||
[domain, action, name] if domain == "auth" && action == "login" => {
|
||||
Ok(Some((name, Provider::Gitea)))
|
||||
}
|
||||
[domain, action, name, option, provider]
|
||||
if domain == "auth" && action == "login" && option == "--provider" =>
|
||||
{
|
||||
Ok(Some((name, provider.parse()?)))
|
||||
}
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn print_user(user: &User) {
|
||||
field("login", user.login.as_deref());
|
||||
field("name", user.full_name.as_deref());
|
||||
@@ -354,7 +375,7 @@ fn domain_help(domain: &str) -> Option<&'static str> {
|
||||
fn subcommand_help(domain: &str, subcommand: &str) -> Option<&'static str> {
|
||||
match (domain, subcommand) {
|
||||
("auth", "login") => Some(
|
||||
"Usage: gotcha auth login SERVER\n\nPrompts for a token, verifies it, and stores the server in the YAML config.",
|
||||
"Usage: gotcha auth login SERVER [--provider gitea|forgejo]\n\nPrompts for a token, discovers and verifies the provider, and stores the server in the YAML config.",
|
||||
),
|
||||
("auth", "list") => {
|
||||
Some("Usage: gotcha auth list\n\nLists configured servers without tokens.")
|
||||
@@ -365,7 +386,7 @@ fn subcommand_help(domain: &str, subcommand: &str) -> Option<&'static str> {
|
||||
("auth", "logout") => Some(
|
||||
"Usage: gotcha auth logout SERVER\n\nRemoves the server and its token from the YAML config.",
|
||||
),
|
||||
("server", "version") => Some("Usage: gotcha server version\n\nShows the Gitea version."),
|
||||
("server", "version") => Some("Usage: gotcha server version\n\nShows the server version."),
|
||||
("user", "show") => Some("Usage: gotcha user show\n\nShows the authenticated user."),
|
||||
("repo", "list") => Some(
|
||||
"Usage: gotcha repo list\n\nLists repositories belonging to the authenticated user.",
|
||||
@@ -402,6 +423,7 @@ fn print_status(selection: &Selection) {
|
||||
"no"
|
||||
}
|
||||
);
|
||||
println!("provider: {}", selection.provider);
|
||||
if let Some(scope) = &selection.repository {
|
||||
println!("repository: {}/{}", scope.owner, scope.repository);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user