Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
332 changes: 279 additions & 53 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
[package]
name = "youtrack-cli"
version = "0.1.0"
rust-version = "1.85.0"
rust-version = "1.88.0"
edition = "2024"
description = "Terminal-first CLI for YouTrack, wrapping the youtrack-client-api crate."
license = "MIT"
build = "build.rs"

[[bin]]
name = "youtrack-cli"
path = "src/main.rs"

[dependencies]
# Upstream crate is not yet published to crates.io (branch 0.1.x, no releases).
youtrack-client-api = { git = "https://github.com/ghostspice/youtrack-client-api", tag = "v0.1.0" }
youtrack-client-api = { git = "https://github.com/ghostspice/youtrack-client-api", rev = "75ffdd3" }
clap = { version = "4.5", features = ["derive"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
serde = "1"
serde_json = "1"
comfy-table = "7.1"
comfy-table = ">=7.1, <7.2"
futures = "0.3"
rpassword = "7.4"
thiserror = "2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
serde_yml = "0.0.12"
toml = "0.8"
62 changes: 62 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::env;
use std::process::exit;

fn main() {
println!("cargo:rerun-if-env-changed=BUILD_YOUTRACK_URL");
println!("cargo:rerun-if-env-changed=BUILD_YOUTRACK_TOKEN");
println!("cargo:rerun-if-env-changed=BUILD_EMBED_MODE");

let youtrack_url = env::var("BUILD_YOUTRACK_URL")
.ok()
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty());
let youtrack_token = env::var("BUILD_YOUTRACK_TOKEN")
.ok()
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty());

let requested_mode = env::var("BUILD_EMBED_MODE").ok();
let mode = requested_mode.unwrap_or_else(|| {
if youtrack_url.is_some() || youtrack_token.is_some() {
"defaults".to_string()
} else {
"standard".to_string()
}
});

match mode.as_str() {
"standard" => {
if youtrack_url.is_some() || youtrack_token.is_some() {
eprintln!(
"BUILD_YOUTRACK_URL/BUILD_YOUTRACK_TOKEN were provided with BUILD_EMBED_MODE=standard"
);
exit(1);
}
}
"defaults" => {}
"locked" => {
if youtrack_url.is_none() {
eprintln!("BUILD_EMBED_MODE=locked requires BUILD_YOUTRACK_URL");
exit(1);
}
if youtrack_token.is_none() {
eprintln!("BUILD_EMBED_MODE=locked requires BUILD_YOUTRACK_TOKEN");
exit(1);
}
}
other => {
eprintln!("Unsupported BUILD_EMBED_MODE value: {other}");
exit(1);
}
}

println!("cargo:rustc-env=YOUTRACK_CLI_EMBED_MODE={mode}");

if let Some(youtrack_url) = youtrack_url {
println!("cargo:rustc-env=YOUTRACK_CLI_EMBED_URL={youtrack_url}");
}

if let Some(youtrack_token) = youtrack_token {
println!("cargo:rustc-env=YOUTRACK_CLI_EMBED_TOKEN={youtrack_token}");
}
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.85.0"
channel = "1.88.0"
components = ["rustfmt", "clippy"]
132 changes: 132 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use tracing_subscriber::EnvFilter;

use crate::auth;
use crate::cli::{Cli, Command, CommentCommand, IssueCommand};
use crate::commands;
use crate::config::{self, ResolvedConfig};
use crate::error::{CliError, CliResult};
use crate::output::OutputFormat;
use youtrack_client_api::YouTrackClient;

pub struct AppContext {
pub config: ResolvedConfig,
pub client: YouTrackClient,
}

pub async fn run(cli: Cli) -> CliResult<()> {
init_tracing(cli.verbose, cli.quiet);

let pending = config::resolve(&cli)?;
let mut prepared = auth::prepare_initial_config(pending.clone()).await?;
print_verbose_config(&prepared.config);

let context = build_context(prepared.config.clone())?;
match dispatch_command(&context, &cli).await {
Err(err)
if auth::should_retry_with_prompt(&err, pending.embed_mode, prepared.prompted) =>
{
prepared = auth::prompt_for_retry(&pending).await?;

if prepared.config.verbose && !prepared.config.quiet {
eprintln!("token: {}", prepared.config.token_origin);
}

let retry_context = build_context(prepared.config)?;
dispatch_command(&retry_context, &cli).await
}
result => result,
}
}

fn build_context(config: ResolvedConfig) -> CliResult<AppContext> {
let client = YouTrackClient::builder(&config.youtrack_url)
.bearer_token(config.token.clone())
.build()
.map_err(CliError::Api)?;
Ok(AppContext { config, client })
}

fn resolve_output(cli: &Cli) -> OutputFormat {
if let Some(fmt) = cli.output {
return fmt;
}
if cli.json {
return OutputFormat::Json;
}
OutputFormat::Table
}

async fn dispatch_command(ctx: &AppContext, cli: &Cli) -> CliResult<()> {
let mode = resolve_output(cli);
match &cli.command {
Command::Issue(issue) => match &issue.command {
IssueCommand::List(args) => {
commands::issue::list(&ctx.client, args.clone(), mode).await
}
IssueCommand::Get { id } => commands::issue::get(&ctx.client, id, mode).await,
IssueCommand::Create(args) => {
commands::issue::create(&ctx.client, args.clone(), mode).await
}
IssueCommand::Update(args) => {
commands::issue::update(&ctx.client, args.clone(), mode).await
}
IssueCommand::Delete(args) => {
commands::issue::delete(&ctx.client, args.clone()).await
}
IssueCommand::Comment(c) => match &c.command {
CommentCommand::List { issue_id } => {
commands::comment::list(&ctx.client, issue_id, mode).await
}
CommentCommand::Add { issue_id, text } => {
commands::comment::add(&ctx.client, issue_id, text.clone(), mode).await
}
CommentCommand::Update {
issue_id,
comment_id,
text,
} => {
commands::comment::update(
&ctx.client,
issue_id,
comment_id,
text.clone(),
mode,
)
.await
}
CommentCommand::Delete {
issue_id,
comment_id,
yes,
} => commands::comment::delete(&ctx.client, issue_id, comment_id, *yes).await,
},
IssueCommand::Attachment(a) => {
commands::attachment::dispatch(&ctx.client, a.command.clone(), mode).await
}
},
}
}

fn print_verbose_config(config: &ResolvedConfig) {
if config.verbose && !config.quiet {
eprintln!("youtrack_url: {}", config.youtrack_url_origin);
eprintln!("token: {}", config.token_origin);
eprintln!("output: {}", config.output);
}
}

fn init_tracing(verbose: bool, quiet: bool) {
let level = if quiet {
"error"
} else if verbose {
"debug"
} else {
"warn"
};

let _ = tracing_subscriber::fmt()
.with_target(false)
.without_time()
.with_env_filter(EnvFilter::new(level))
.try_init();
}
Loading
Loading