feat: begin designing cli

This commit is contained in:
DecDuck
2026-01-06 16:11:06 +07:00
parent aa21a779ff
commit 4e32c38948
6 changed files with 1660 additions and 49 deletions
+79
View File
@@ -0,0 +1,79 @@
use std::sync::LazyLock;
use anyhow::{Result, anyhow};
use dialoguer::{Confirm, Input, theme::ColorfulTheme};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use url::Url;
const TOKEN_CREATE_PAYLOAD: &str =
"eyJuYW1lIjoiZG93bnBvdXIgKGNsaSkiLCJhY2xzIjpbImRlcG90Om5ldyJdfQ==";
static CLIENT: LazyLock<Client> = LazyLock::new(|| reqwest::Client::new());
const REQUIRED_ACLS: [&str; 1] = ["depot:new"];
pub async fn interactive_configure(url: String) -> Result<()> {
let base_url = Url::parse(&url)?;
let mut token_create_url = base_url.join("/admin/settings/tokens")?;
{
let mut query = token_create_url.query_pairs_mut();
query.append_pair("payload", TOKEN_CREATE_PAYLOAD);
};
let confirm = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(format!(
"Open \"{}\" in your default browser?",
token_create_url.as_str()
))
.interact()?;
if !confirm {
return Err(anyhow!("User cancelled action"));
}
webbrowser::open(token_create_url.as_str())?;
let token: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("API token")
.interact_text()?;
validate_configuration(url, token).await?;
Ok(())
}
pub async fn validate_configuration(url: String, token: String) -> Result<()> {
let base_url = Url::parse(&url)?;
let token_check_url = base_url.join("/api/v1/token")?;
let acl_check = CLIENT
.get(token_check_url)
.bearer_auth(token)
.send()
.await?;
if !acl_check.status().is_success() {
return Err(anyhow!(
"ACL check failed with response code: {}",
acl_check.status()
));
}
let acls: Vec<String> = acl_check.json().await?;
for acl in REQUIRED_ACLS {
if !acls.contains(&acl.to_string()) {
return Err(anyhow!("Token missing {} acl", acl));
}
}
Ok(())
}
#[derive(Serialize, Deserialize)]
pub struct ServerConfiguration {
pub endpoint: String,
pub token: String,
}
+1
View File
@@ -0,0 +1 @@
pub mod configure;