fj/src/cli/gist.rs
Stephen Way de49c33921
batch: release, label, workflow, search, browse, status, org, keys, alias, config, extension, gist
* New top-level groups, each with full CRUD where the API supports it:
  - release: list/view/create/edit/delete/upload/download
  - label: list/create/edit/delete
  - run: workflow runs (list/view/rerun/cancel)
  - secret + variable: Actions secrets/vars (list/set/delete)
  - search: cross-cutting (repos/issues/prs/users)
  - browse: open repo/path on the web
  - status: notifications inbox + mark-all-read
  - org: list/view/teams
  - ssh-key, gpg-key: list/add/delete on your account
  - alias: user-defined shortcuts (e.g. `fj alias set co "pr checkout"`)
  - config: local prefs (editor, pager, browser, etc.)
  - extension: discover and run `fj-<name>` plugin binaries on PATH
  - gist: thin wrapper over `gist-*` repos
* main.rs now expands aliases before clap and dispatches to plugins for
  unknown subcommands (matching gh).
* New API modules: release, label, notification, search, org, workflow,
  with the corresponding strongly-typed wrappers.
* Release asset upload uses reqwest multipart (feature flag added).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 08:29:31 -07:00

107 lines
2.8 KiB
Rust

//! `fj gist` — Forgejo Gists (snippets).
//!
//! Forgejo doesn't ship a Gist surface separate from regular repos; we expose
//! a thin wrapper that lists/views the current user's "fj-gist-*" repos and
//! creates new ones. Power users should fall through to `fj api`.
use anyhow::Result;
use clap::{Args, Subcommand};
use crate::api;
use crate::client::Client;
use crate::output;
#[derive(Debug, Args)]
pub struct GistCmd {
#[command(subcommand)]
pub command: GistSub,
}
#[derive(Debug, Subcommand)]
pub enum GistSub {
/// List your gist-style repos (any repo prefixed `gist-`).
List(ListArgs),
/// Create a new gist (private repo with a single file).
Create(CreateArgs),
}
#[derive(Debug, Args)]
pub struct ListArgs {
#[arg(long)]
pub json: bool,
}
#[derive(Debug, Args)]
pub struct CreateArgs {
/// Gist name (used as the repo name, prefixed with `gist-`).
pub name: String,
/// Optional description.
#[arg(short = 'd', long)]
pub description: Option<String>,
/// Make it private (the default).
#[arg(long, default_value_t = true)]
pub private: bool,
}
pub async fn run(cmd: GistCmd, host: Option<&str>) -> Result<()> {
let client = Client::connect(host)?;
match cmd.command {
GistSub::List(args) => list(&client, args).await,
GistSub::Create(args) => create(&client, args).await,
}
}
async fn list(client: &Client, args: ListArgs) -> Result<()> {
let page = api::repo::list_for_user(
client,
api::repo::ListOptions {
limit: 50,
page: 1,
query: None,
},
)
.await?;
let gists: Vec<_> = page
.items
.into_iter()
.filter(|r| r.name.starts_with("gist-"))
.collect();
if args.json {
return output::print_json(&serde_json::to_value(&gists)?);
}
if gists.is_empty() {
println!("(no gists)");
return Ok(());
}
let rows: Vec<Vec<String>> = gists
.iter()
.map(|g| {
vec![
g.full_name.clone(),
g.description.clone(),
output::dim(&output::relative_time(g.updated_at)),
]
})
.collect();
print!(
"{}",
output::render_table(&["NAME", "DESCRIPTION", "UPDATED"], &rows)
);
Ok(())
}
async fn create(client: &Client, args: CreateArgs) -> Result<()> {
let name = format!("gist-{}", args.name);
let body = api::repo::CreateRepo {
name: &name,
description: args.description.as_deref(),
private: args.private,
default_branch: None,
auto_init: true,
};
let repo = api::repo::create_for_current_user(client, &body).await?;
println!("✓ Created gist {}", repo.full_name);
println!("{}", repo.html_url);
Ok(())
}