Skip to content

Commit

Permalink
update volts-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Nov 24, 2022
1 parent 64ecd15 commit e45f2d7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 31 deletions.
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion volts-back/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ diesel = { version = "2.0.2", features = ["postgres", "chrono"] }
diesel-async = { version = "0.1.1", features = ["postgres", "deadpool"] }
async-session = "3.0.0"
headers = "0.3"
axum = { version = "0.6.0-rc.2", features = ["headers"] }
axum = { version = "0.6.0-rc.4", features = ["headers"] }
reqwest = { version = "0.11.12", features = ["json"] }
oauth2 = "4.2.3"
anyhow = "1.0.66"
Expand Down
28 changes: 23 additions & 5 deletions volts-back/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::db::{

const VOLT_MANIFEST: &str = "volt.toml";
const VOLT_ARCHIVE: &str = "plugin.volt";
const OLD_VOLT_ARCHIVE: &str = "volt.tar.gz";

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -248,7 +249,20 @@ pub async fn download(
}

let s3_path = format!("{}/{}/{}/{VOLT_ARCHIVE}", user.gh_login, name, version.num);
bucket.presign_get(&s3_path, 60, None).unwrap()
if bucket
.head_object(&s3_path)
.await
.map(|(_, code)| code == 200)
.unwrap_or(false)
{
bucket.presign_get(&s3_path, 60, None).unwrap()
} else {
let old_s3_path = format!(
"{}/{}/{}/{OLD_VOLT_ARCHIVE}",
user.gh_login, name, version.num
);
bucket.presign_get(&old_s3_path, 60, None).unwrap()
}
}

pub async fn readme(
Expand Down Expand Up @@ -573,7 +587,7 @@ pub async fn publish(
let volt_archive = dest_volt_archive.clone();
tokio::task::spawn_blocking(move || {
let volt_archive = std::fs::File::create(volt_archive).unwrap();
let encoder = Encoder::new(volt_archive, 0).unwrap();
let encoder = Encoder::new(volt_archive, 0).unwrap().auto_finish();
let mut tar = tar::Builder::new(encoder);
tar.append_dir_all(".", dest.path()).unwrap();
})
Expand All @@ -583,7 +597,11 @@ pub async fn publish(

let volt_content = tokio::fs::read(&dest_volt_archive).await.unwrap();
bucket
.put_object(format!("{s3_folder}/{VOLT_ARCHIVE}"), &volt_content)
.put_object_with_content_type(
format!("{s3_folder}/{VOLT_ARCHIVE}"),
&volt_content,
"application/zstd",
)
.await
.unwrap();

Expand Down Expand Up @@ -632,7 +650,7 @@ where
pub async fn yank(
TypedHeader(token): TypedHeader<headers::Authorization<Bearer>>,
State(db_pool): State<DbPool>,
Path((name, version)): Path<(String, String)>,
Path((author, name, version)): Path<(String, String, String)>,
) -> impl IntoResponse {
let api_token = {
let mut conn = db_pool.write.get().await.unwrap();
Expand All @@ -659,7 +677,7 @@ pub async fn yank(
pub async fn unyank(
TypedHeader(token): TypedHeader<headers::Authorization<Bearer>>,
State(db_pool): State<DbPool>,
Path((name, version)): Path<(String, String)>,
Path((author, name, version)): Path<(String, String, String)>,
) -> impl IntoResponse {
let api_token = {
let mut conn = db_pool.write.get().await.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions volts-back/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub fn build_router() -> Router<AppState> {
let plugins_routes = Router::with_state(state.clone())
.route("/", get(plugin::search))
.route("/new", put(plugin::publish))
.route("/:name/:version/yank", post(plugin::yank))
.route("/:name/:version/unyank", post(plugin::unyank))
.route("/:author/:name/:version/yank", post(plugin::yank))
.route("/:author/:name/:version/unyank", post(plugin::unyank))
.route("/:author/:name/:version", get(plugin::meta))
.route("/:author/:name/:version/download", get(plugin::download))
.route("/:author/:name/:version/readme", get(plugin::readme))
Expand Down
2 changes: 1 addition & 1 deletion volts-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "volts"
description = "Cli tool for publishing and managing Lapce plugins"
license = "Apache-2.0"
version = "0.1.2"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
7 changes: 2 additions & 5 deletions volts-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) fn publish(cli: &Cli) {

{
let archive = File::create(&archive_path).unwrap();
let encoder = Encoder::new(archive, 0).unwrap();
let encoder = Encoder::new(archive, 0).unwrap().auto_finish();
let mut tar = Builder::new(encoder);

let volt_path = PathBuf::from("volt.toml");
Expand Down Expand Up @@ -124,10 +124,7 @@ pub(crate) fn publish(cli: &Cli) {
}

let resp = reqwest::blocking::Client::new()
.request(
Method::PUT,
"https://plugins.lapce.dev/api/v1/me/plugins/new",
)
.request(Method::PUT, "http://127.0.0.1:8080/api/v1/plugins/new")
.bearer_auth(token.trim())
.body(File::open(&archive_path).unwrap())
.send()
Expand Down
20 changes: 8 additions & 12 deletions volts-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ pub fn cli() {
}

fn auth_token(cli: &Cli) -> String {
let api_credential = keyring::Entry::new("lapce-volts", "registry-api");
if let Some(token) = &cli.token {
token.to_owned()
} else {
let api_credential = keyring::Entry::new("lapce-volts", "registry-api");
if let Ok(token) = api_credential.get_password() {
return token;
}

return if cli.token.is_none() && api_credential.get_password().is_err() {
println!("Please paste the API Token you created on https://plugins.lapce.dev/");
let mut token = String::new();
stdin().read_line(&mut token).unwrap();
Expand All @@ -69,14 +74,5 @@ fn auth_token(cli: &Cli) -> String {
};

token
} else if let Some(token) = &cli.token {
if api_credential.get_password().is_err() {
if let Err(why) = api_credential.set_password(token) {
eprintln!("Failed to save token in system credential store: {why}");
};
}
token.to_owned()
} else {
api_credential.get_password().unwrap()
};
}
}

0 comments on commit e45f2d7

Please sign in to comment.