Skip to content

Commit

Permalink
feat: adds --confirm to graph delete
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Oct 6, 2021
1 parent 62101d7 commit edae344
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
13 changes: 12 additions & 1 deletion src/command/graph/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rover_client::operations::graph::delete::{self, GraphDeleteInput};
use rover_client::shared::GraphRef;

use crate::command::RoverOutput;
use crate::utils::client::StudioClientConfig;
use crate::utils::{self, client::StudioClientConfig};
use crate::Result;

#[derive(Debug, Serialize, StructOpt)]
Expand All @@ -21,18 +21,29 @@ pub struct Delete {
#[structopt(long = "profile", default_value = "default")]
#[serde(skip_serializing)]
profile_name: String,

/// Skips the step where the command asks for user confirmation before
/// deleting the graph.
#[structopt(long)]
confirm: bool,
}

impl Delete {
pub fn run(&self, client_config: StudioClientConfig) -> Result<RoverOutput> {
let client = client_config.get_authenticated_client(&self.profile_name)?;
let graph_ref = self.graph.to_string();

eprintln!(
"Deleting {} using credentials from the {} profile.",
Cyan.normal().paint(&graph_ref),
Yellow.normal().paint(&self.profile_name)
);

if !self.confirm && !utils::confirm_delete()? {
eprintln!("Delete cancelled by user");
return Ok(RoverOutput::EmptySuccess);
}

delete::run(
GraphDeleteInput {
graph_ref: self.graph.clone(),
Expand Down
15 changes: 2 additions & 13 deletions src/command/subgraph/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Serialize;
use structopt::StructOpt;

use crate::command::RoverOutput;
use crate::utils::client::StudioClientConfig;
use crate::utils::{self, client::StudioClientConfig};
use crate::Result;

use rover_client::operations::subgraph::delete::{self, SubgraphDeleteInput};
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Delete {
.print();

// I chose not to error here, since this is a perfectly valid path
if !confirm_delete()? {
if !utils::confirm_delete()? {
eprintln!("Delete cancelled by user");
return Ok(RoverOutput::EmptySuccess);
}
Expand All @@ -92,14 +92,3 @@ impl Delete {
})
}
}

fn confirm_delete() -> Result<bool> {
eprintln!("Would you like to continue [y/n]");
let term = console::Term::stdout();
let confirm = term.read_line()?;
if confirm.to_lowercase() == *"y" {
Ok(true)
} else {
Ok(false)
}
}
8 changes: 6 additions & 2 deletions src/error/metadata/suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ impl Display for Suggestion {
"Try running the command on a valid federated graph, or use the appropriate `rover graph` command instead of `rover subgraph`.".to_string()
}
Suggestion::CheckGraphNameAndAuth => {
"Make sure your graph name is typed correctly, and that your API key is valid. (Are you using the right profile?)".to_string()
format!(
"Make sure your graph name is typed correctly, and that your API key is valid.\n You can run {} to check if you are authenticated.\n If you are trying to create a new graph, you must do so online at {}, by clicking \"New Graph\".",
Yellow.normal().paint("`rover config whoami`"),
Cyan.normal().paint("https://studio.apollographql.com")
)
}
Suggestion::ProvideValidSubgraph(valid_subgraphs) => {
format!(
Expand All @@ -97,7 +101,7 @@ impl Display for Suggestion {
let num_valid_variants = valid_variants.len();
let color_graph_name = Cyan.normal().paint(&graph_ref.name);
match num_valid_variants {
0 => format!("Graph {} exists, but has no variants. You can create a new variant by running {}.", &color_graph_name, Yellow.normal().paint("`rover graph publish`")),
0 => format!("Graph {} exists, but has no variants. You can create a new monolithic variant by running {} for your graph schema, or a new federated variant by running {} for all of your subgraph schemas.", &color_graph_name, Yellow.normal().paint("`rover graph publish`"), Yellow.normal().paint("`rover subgraph publish`")),
1 => format!("The only existing variant for graph {} is {}.", &color_graph_name, Cyan.normal().paint(&valid_variants[0])),
2 => format!("The existing variants for graph {} are {} and {}.", &color_graph_name, Cyan.normal().paint(&valid_variants[0]), Cyan.normal().paint(&valid_variants[1])),
3 ..= 10 => {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ pub mod stringify;
pub mod table;
pub mod telemetry;
pub mod version;

pub fn confirm_delete() -> std::io::Result<bool> {
eprintln!("Would you like to continue? [y/n]");
let term = console::Term::stdout();
let confirm = term.read_line()?;
if confirm.to_lowercase() == *"y" {
Ok(true)
} else {
Ok(false)
}
}

0 comments on commit edae344

Please sign in to comment.