Skip to content

Commit

Permalink
refactor: using the new amp-common and amp-client
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jan 28, 2025
1 parent 1358eaa commit d96e657
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 90 deletions.
298 changes: 227 additions & 71 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.9.5" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.9.6" }
amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.10.0" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.10.2" }
anyhow = "1.0.95"
clap = { version = "4.5.27", features = ["derive", "env"] }
clap-verbosity-flag = "3.0.2"
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Cli {
return delete(&ctx.client, id).await;
}

let playbooks = ctx.client.playbooks().list(None).map_err(Errors::ClientError)?;
let playbooks = ctx.client.playbooks().list(None).await.map_err(Errors::ClientError)?;
if playbooks.is_empty() {
println!("No playbooks found");
return Ok(());
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Display for OptionItem {
}

async fn delete(client: &Client, id: &str) -> Result<()> {
let status = client.playbooks().delete(id).map_err(Errors::ClientError)?;
let status = client.playbooks().delete(id).await.map_err(Errors::ClientError)?;
if status != 204 {
return Err(Errors::FailedDeletePlaybook(id.to_string()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Cli {}

impl Cli {
pub async fn exec(&self, ctx: Arc<Context>) -> Result<()> {
let playbooks = ctx.client.playbooks().list(None).map_err(Errors::ClientError)?;
let playbooks = ctx.client.playbooks().list(None).await.map_err(Errors::ClientError)?;

if playbooks.is_empty() {
println!("No playbooks found");
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ impl Cli {
// Create the playbook based on the options
let playbook: PlaybookSpec;
if let Some(repository) = &self.git {
playbook = pipeline::pull(&ctx, repository)?;
playbook = pipeline::pull(&ctx, repository).await?;
} else if let Some(name) = &self.name {
playbook = pipeline::fetch(&ctx, name)?;
playbook = pipeline::fetch(&ctx, name).await?;
} else {
opt.live = true;
playbook = pipeline::load(&ctx, &self.filename, opt.once).await?;
Expand Down
2 changes: 1 addition & 1 deletion src/ops/cleaner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub async fn try_cleanup_playbook(ctx: &Arc<Context>) -> Result<()> {

// Delete playbook from the server.
let pid = &playbook.as_ref().unwrap().id;
let status = ctx.client.playbooks().delete(pid).map_err(Errors::ClientError)?;
let status = ctx.client.playbooks().delete(pid).await.map_err(Errors::ClientError)?;
if status != 204 {
return Err(Errors::FailedDeletePlaybook(pid.to_string()));
}
Expand Down
15 changes: 9 additions & 6 deletions src/ops/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Options {
}

/// Create a playbook from the remote git repository.
pub fn pull(ctx: &Context, repository: &str) -> Result<PlaybookSpec> {
pub async fn pull(ctx: &Context, repository: &str) -> Result<PlaybookSpec> {
create(
ctx.client.playbooks(),
PlaybookPayload {
Expand All @@ -48,10 +48,11 @@ pub fn pull(ctx: &Context, repository: &str) -> Result<PlaybookSpec> {
preface: Preface::repository(repository),
},
)
.await
}

/// Create a playbook from the remote registry.
pub fn fetch(ctx: &Context, name: &str) -> Result<PlaybookSpec> {
pub async fn fetch(ctx: &Context, name: &str) -> Result<PlaybookSpec> {
create(
ctx.client.playbooks(),
PlaybookPayload {
Expand All @@ -60,6 +61,7 @@ pub fn fetch(ctx: &Context, name: &str) -> Result<PlaybookSpec> {
preface: Preface::registry(name, "hub", "latest"),
},
)
.await
}

/// Create a playbook from the local manifest file.
Expand All @@ -79,11 +81,12 @@ pub async fn load(ctx: &Context, filename: &Option<PathBuf>, once: bool) -> Resu
preface: Preface::manifest(&character),
},
)
.await
}

/// Create a playbook from the given payload.
pub fn create(client: Playbooks, payload: PlaybookPayload) -> Result<PlaybookSpec> {
let playbook = client.create(payload).map_err(Errors::FailedCreatePlaybook)?;
pub async fn create(client: Playbooks<'_>, payload: PlaybookPayload) -> Result<PlaybookSpec> {
let playbook = client.create(payload).await.map_err(Errors::FailedCreatePlaybook)?;

info!("The playbook begins to create...");
debug!("The created playbook is:\n {:#?}", playbook);
Expand All @@ -96,7 +99,7 @@ pub async fn run(ctx: &Arc<Context>, playbook: PlaybookSpec, options: Options) -
// wait playbook resolve finished.
sleep(Duration::from_secs(10)).await;

let playbook = ctx.client.playbooks().get(&playbook.id).map_err(Errors::ClientError)?;
let playbook = ctx.client.playbooks().get(&playbook.id).await.map_err(Errors::ClientError)?;
ctx.session.playbook.write().await.replace(playbook.clone());

let pid = Arc::new(playbook.id.clone());
Expand All @@ -106,7 +109,7 @@ pub async fn run(ctx: &Arc<Context>, playbook: PlaybookSpec, options: Options) -
if options.live {
info!("Syncing the full sources into the server...");
let workspace = ctx.session.workspace.read().await.clone().unwrap();
utils::upload(&ctx.client.actors(), &pid, &name, &workspace)?;
utils::upload(&ctx.client.actors(), &pid, &name, &workspace).await?;
}

// Watch file changes and sync the changed files.
Expand Down
6 changes: 3 additions & 3 deletions src/ops/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ pub async fn watch(workspace: &Path, client: &Client, pid: &str, name: &str) ->
continue;
}

handle(client, pid, name, workspace, event)?;
handle(client, pid, name, workspace, event).await?;
}

Ok(())
}

fn handle(client: &Client, pid: &str, name: &str, base: &Path, event: Event) -> Result<()> {
async fn handle(client: &Client, pid: &str, name: &str, base: &Path, event: Event) -> Result<()> {
trace!("Changed: {:?}", event);

let kind = EventKinds::from(event.kind);
Expand Down Expand Up @@ -86,7 +86,7 @@ fn handle(client: &Client, pid: &str, name: &str, base: &Path, event: Event) ->
}

debug!("The sync request is: {:?}", req);
client.actors().sync(pid, name, req).map_err(Errors::ClientError)?;
client.actors().sync(pid, name, req).await.map_err(Errors::ClientError)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tracing::debug;
use crate::errors::{Errors, Result};

/// Upload the given directory to the server.
pub fn upload(client: &Actors, pid: &str, name: &str, workspace: &Path) -> Result<()> {
pub async fn upload(client: &Actors<'_>, pid: &str, name: &str, workspace: &Path) -> Result<()> {
let mut paths: Vec<(PathBuf, PathBuf)> = vec![];

let base = workspace;
Expand All @@ -40,7 +40,7 @@ pub fn upload(client: &Actors, pid: &str, name: &str, workspace: &Path) -> Resul

let payload = archive(&paths)?;
let req = Synchronization { kind: EventKinds::Overwrite, paths: vec![], attributes: None, payload: Some(payload) };
client.sync(pid, name, req).map_err(Errors::ClientError)?;
client.sync(pid, name, req).await.map_err(Errors::ClientError)?;

Ok(())
}
Expand Down

0 comments on commit d96e657

Please sign in to comment.