Skip to content

Commit

Permalink
fix: Failure to run when no configuration file is found and when the …
Browse files Browse the repository at this point in the history
…file format is illegal
  • Loading branch information
wangeguo committed Jan 23, 2024
1 parent c532a2e commit 0cc098f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 53 deletions.
2 changes: 1 addition & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "amp"
description = "Amphitheatre's official command line tool"
version = "0.6.2"
version = "0.6.3"
edition = "2021"
license = "Apache-2.0"
homepage = "https://amphitheatre.app"
Expand Down
12 changes: 7 additions & 5 deletions src/cmd/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use amp_common::filesystem::Finder;
use clap::Args;
use std::path::PathBuf;
use std::sync::Arc;

use crate::context::Context;
use crate::errors::Result;
use crate::errors::{Errors, Result};
use crate::ops::pipeline::Options;
use crate::ops::{cleaner, pipeline};

Expand All @@ -34,7 +36,7 @@ pub struct Cli {

/// Path or URL to the Amphitheatre config file
#[arg(short, long, env = "AMP_FILENAME")]
filename: Option<String>,
filename: Option<PathBuf>,

/// Activate profiles by name (prefixed with `-` to disable a profile)
#[arg(short, long, env = "AMP_PROFILE")]
Expand All @@ -55,9 +57,9 @@ impl Cli {
cleaner::setup_signal_handler(ctx.clone(), self.cleanup);

// Create the playbook from the local character manifest.
if let Some(filename) = &self.filename {
ctx.session.load(filename).await?;
}
// load the character from the local character manifest.
let path = &self.filename.clone().unwrap_or(Finder::new().find().map_err(Errors::NotFoundManifest)?);
ctx.session.load(path).await?;

// Define the options for the pipeline.
let opt = Options {
Expand Down
11 changes: 6 additions & 5 deletions src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use amp_common::filesystem::Finder;
use amp_common::resource::PlaybookSpec;
use clap::Args;
use std::path::PathBuf;
use std::sync::Arc;

use crate::context::Context;
use crate::errors::Result;
use crate::errors::{Errors, Result};
use crate::ops::pipeline::Options;
use crate::ops::{cleaner, pipeline};

Expand All @@ -35,7 +37,7 @@ pub struct Cli {

/// Path or URL to the Amphitheatre config file
#[arg(short, long, env = "AMP_FILENAME")]
filename: Option<String>,
filename: Option<PathBuf>,

/// The URL of the remote git repository for your character where you want to run
#[arg(long, env = "AMP_GIT")]
Expand All @@ -60,9 +62,8 @@ impl Cli {
cleaner::setup_signal_handler(ctx.clone(), self.cleanup);

// load the character from the local character manifest.
if let Some(filename) = &self.filename {
ctx.session.load(filename).await?;
}
let path = &self.filename.clone().unwrap_or(Finder::new().find().map_err(Errors::NotFoundManifest)?);
ctx.session.load(path).await?;

// Define the options for the pipeline.
let opt = Options {
Expand Down
42 changes: 3 additions & 39 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ use std::{path::PathBuf, sync::Arc};
use amp_client::client::Client;
use amp_common::{
config::{Cluster, Configuration},
filesystem::Finder,
resource::{ActorSpec, PlaybookSpec},
schema::Character,
};
use tokio::sync::RwLock;
use tracing::debug;

use crate::errors::{Errors, Result};

Expand All @@ -36,25 +34,10 @@ pub struct Session {
}

impl Session {
pub fn init() -> Session {
// Try to load the character from the current or parent directory.
let (workspace, character) = try_load_character()
.map(|(workspace, character)| (Some(workspace), Some(character)))
.unwrap_or((None, None));

Session {
workspace: RwLock::new(workspace),
character: RwLock::new(character),
playbook: RwLock::new(None),
actor: RwLock::new(None),
}
}

/// Load the character from the specified file.
pub async fn load(&self, filename: &str) -> Result<()> {
let path = PathBuf::from(filename);
pub async fn load(&self, path: &PathBuf) -> Result<()> {
let workspace = path.parent().unwrap().to_path_buf();
let character = Character::load(&path).map_err(|e| Errors::FailedLoadManifest(e.to_string()))?;
let character = Character::load(path).map_err(Errors::FailedLoadManifest)?;

self.workspace.write().await.replace(workspace);
self.character.write().await.replace(character);
Expand All @@ -63,25 +46,6 @@ impl Session {
}
}

/// Try to load the character from the current directory
fn try_load_character() -> Option<(PathBuf, Character)> {
let path = Finder::new().find();
if let Err(err) = path {
debug!("Not found character in current or parent directories: {}", err);
return None;
}

let path = path.unwrap();
let workspace = path.parent().unwrap().to_path_buf();
let character = Character::load(&path);
if let Err(err) = character {
debug!("Failed to read character manifest: {}", err);
return None;
}

Some((workspace, character.unwrap()))
}

/// Context holds the current context state
pub struct Context {
pub configuration: RwLock<Configuration>,
Expand All @@ -101,7 +65,7 @@ impl Context {
Ok(Context {
configuration: RwLock::new(configuration),
cluster: RwLock::new(cluster),
session: Session::init(),
session: Session::default(),
client: Arc::new(client),
})
}
Expand Down
7 changes: 5 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::path::StripPrefixError;

use amp_common::http;
use amp_common::{filesystem, http};
use thiserror::Error;

pub type Result<T> = std::result::Result<T, Errors>;
Expand All @@ -34,7 +34,7 @@ pub enum Errors {
ClientError(http::HTTPError),

#[error("Failed to load manifest: {0}")]
FailedLoadManifest(String),
FailedLoadManifest(anyhow::Error),

#[error("Failed to delete playbook: {0}")]
FailedDeletePlaybook(String),
Expand Down Expand Up @@ -87,4 +87,7 @@ pub enum Errors {

#[error("Failed to add context: {0}")]
FailedAddContext(anyhow::Error),

#[error("Not found character in current or parent directories: {0}")]
NotFoundManifest(filesystem::Error),
}

0 comments on commit 0cc098f

Please sign in to comment.