Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: No string manipulation on app.yaml #4976

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 37 additions & 40 deletions lib/cli/src/commands/app/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,22 @@ impl CmdAppDeploy {
async fn get_owner(
&self,
client: &WasmerClient,
app: &serde_yaml::Value,
app_config_path: &PathBuf,
app: &mut serde_yaml::Value,
maybe_edge_app: Option<&DeployApp>,
) -> anyhow::Result<(String, String)> {
let r_ret = serde_yaml::to_string(&app)?;

) -> anyhow::Result<String> {
if let Some(owner) = &self.owner {
return Ok((owner.clone(), r_ret));
return Ok(owner.clone());
}

if let Some(serde_yaml::Value::String(owner)) = &app.get("owner") {
return Ok((owner.clone(), r_ret));
return Ok(owner.clone());
}

if let Some(edge_app) = maybe_edge_app {
let new_raw_config = format!("{r_ret}\nowner: {}\n", edge_app.owner.global_name);
return Ok((edge_app.owner.global_name.clone(), new_raw_config));
app.as_mapping_mut()
.unwrap()
.insert("owner".into(), edge_app.owner.global_name.clone().into());
return Ok(edge_app.owner.global_name.clone());
};

if self.non_interactive {
Expand All @@ -195,12 +194,11 @@ impl CmdAppDeploy {
Some(&user),
)?;

let new_raw_config = format!("{r_ret}\nowner: {owner}\n");

std::fs::write(app_config_path, &new_raw_config)
.with_context(|| format!("Could not write file: '{}'", app_config_path.display()))?;
app.as_mapping_mut()
.unwrap()
.insert("owner".into(), owner.clone().into());

Ok((owner.clone(), new_raw_config))
Ok(owner.clone())
}
async fn create(&self) -> anyhow::Result<()> {
eprintln!("It seems you are trying to create a new app!");
Expand Down Expand Up @@ -272,7 +270,7 @@ impl AsyncCliCommand for CmdAppDeploy {
.with_context(|| format!("Could not read file '{}'", &app_config_path.display()))?;

// We want to allow the user to specify the app name interactively.
let app_yaml: serde_yaml::Value = serde_yaml::from_str(&config_str)?;
let mut app_yaml: serde_yaml::Value = serde_yaml::from_str(&config_str)?;
let maybe_edge_app = if let Some(app_id) = app_yaml.get("app_id").and_then(|s| s.as_str()) {
wasmer_api::query::get_app_by_id(&client, app_id.to_owned())
.await
Expand All @@ -281,24 +279,24 @@ impl AsyncCliCommand for CmdAppDeploy {
None
};

let (owner, mut config_str) = self
.get_owner(
&client,
&app_yaml,
&app_config_path,
maybe_edge_app.as_ref(),
)
let owner = self
.get_owner(&client, &mut app_yaml, maybe_edge_app.as_ref())
.await?;

// We want to allow the user to specify the app name interactively.
let app_yaml: serde_yaml::Value = serde_yaml::from_str(&config_str)?;

if app_yaml.get("name").is_none() && self.app_name.is_some() {
config_str = format!("{config_str}name: {}", self.app_name.as_ref().unwrap());
app_yaml.as_mapping_mut().unwrap().insert(
"name".into(),
self.app_name.as_ref().unwrap().to_string().into(),
);
} else if app_yaml.get("name").is_none() && maybe_edge_app.is_some() {
let edge_app = maybe_edge_app.as_ref().unwrap();
config_str = format!("{config_str}name: {}", edge_app.name);
println!("config_str: {config_str}");
app_yaml.as_mapping_mut().unwrap().insert(
"name".into(),
maybe_edge_app
.as_ref()
.map(|v| v.name.to_string())
.unwrap()
.into(),
);
} else if app_yaml.get("name").is_none() {
if !self.non_interactive {
let default_name = std::env::current_dir().ok().and_then(|dir| {
Expand All @@ -314,14 +312,10 @@ impl AsyncCliCommand for CmdAppDeploy {
)
.await?;

std::fs::write(
&app_config_path,
format!("{}name: {}", config_str, app_name),
)?;

config_str = std::fs::read_to_string(&app_config_path).with_context(|| {
format!("Could not read file '{}'", &app_config_path.display())
})?;
app_yaml
.as_mapping_mut()
.unwrap()
.insert("name".into(), app_name.into());
} else {
if !self.quiet {
eprintln!("The app.yaml does not specify any app name.");
Expand All @@ -338,9 +332,12 @@ impl AsyncCliCommand for CmdAppDeploy {
}
}

let original_app_config: AppConfigV1 = AppConfigV1::parse_yaml(&config_str)?;
std::fs::write(&app_config_path, config_str)
.with_context(|| format!("Could not write file: '{}'", app_config_path.display()))?;
let original_app_config: AppConfigV1 = serde_yaml::from_value(app_yaml.clone())?;
std::fs::write(
&app_config_path,
serde_yaml::to_string(&original_app_config)?,
)
.with_context(|| format!("Could not write file: '{}'", app_config_path.display()))?;

let mut app_config = original_app_config.clone();

Expand Down
Loading