Skip to content

Commit 1b043a4

Browse files
factor out CheckConfigOpts
1 parent d26ebd8 commit 1b043a4

File tree

7 files changed

+75
-73
lines changed

7 files changed

+75
-73
lines changed

crates/rover-client/src/shared/check_response.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::shared::GraphRef;
66
use crate::RoverClientError;
77

88
use prettytable::format::consts::FORMAT_BOX_CHARS;
9-
use serde::Serialize;
9+
use serde::{Deserialize, Serialize};
1010

1111
use prettytable::{cell, row, Table};
1212
use serde_json::{json, Value};
@@ -162,7 +162,7 @@ pub struct CheckConfig {
162162
pub validation_period: Option<ValidationPeriod>,
163163
}
164164

165-
#[derive(Debug, PartialEq, Eq, Serialize, Clone)]
165+
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
166166
pub struct ValidationPeriod {
167167
pub from: Period,
168168
pub to: Period,
@@ -187,7 +187,7 @@ impl FromStr for ValidationPeriod {
187187
}
188188
}
189189

190-
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
190+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
191191
pub enum Period {
192192
Now,
193193
Past(i64),

src/command/fed2/supergraph/compose.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use crate::utils::{
2-
client::StudioClientConfig,
3-
parsers::{parse_file_descriptor, FileDescriptorType},
4-
};
51
use crate::Suggestion;
62
use crate::{anyhow, command::RoverOutput, error::RoverError, Result};
3+
use crate::{
4+
options::ProfileOpt,
5+
utils::{
6+
client::StudioClientConfig,
7+
parsers::{parse_file_descriptor, FileDescriptorType},
8+
},
9+
};
710

811
use serde::Serialize;
912
use structopt::StructOpt;
@@ -15,10 +18,9 @@ pub struct Compose {
1518
#[serde(skip_serializing)]
1619
supergraph_yaml: FileDescriptorType,
1720

18-
/// Name of configuration profile to use
19-
#[structopt(long = "profile", default_value = "default")]
20-
#[serde(skip_serializing)]
21-
_profile_name: String,
21+
#[structopt(flatten)]
22+
#[allow(unused)]
23+
profile: ProfileOpt,
2224
}
2325

2426
impl Compose {

src/command/graph/check.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use serde::Serialize;
22
use structopt::StructOpt;
33

44
use rover_client::operations::graph::check::{self, GraphCheckInput};
5-
use rover_client::shared::{CheckConfig, GitContext, ValidationPeriod};
5+
use rover_client::shared::{CheckConfig, GitContext};
66

77
use crate::command::RoverOutput;
8-
use crate::options::{GraphRefOpt, ProfileOpt, SchemaOpt};
8+
use crate::options::{CheckConfigOpts, GraphRefOpt, ProfileOpt, SchemaOpt};
99
use crate::utils::client::StudioClientConfig;
10-
use crate::utils::parsers::{parse_query_count_threshold, parse_query_percentage_threshold};
1110
use crate::Result;
1211

1312
#[derive(Debug, Serialize, StructOpt)]
@@ -22,20 +21,8 @@ pub struct Check {
2221
#[serde(skip_serializing)]
2322
schema: SchemaOpt,
2423

25-
/// The minimum number of times a query or mutation must have been executed
26-
/// in order to be considered in the check operation
27-
#[structopt(long, parse(try_from_str = parse_query_count_threshold))]
28-
query_count_threshold: Option<i64>,
29-
30-
/// Minimum percentage of times a query or mutation must have been executed
31-
/// in the time window, relative to total request count, for it to be
32-
/// considered in the check. Valid numbers are in the range 0 <= x <= 100
33-
#[structopt(long, parse(try_from_str = parse_query_percentage_threshold))]
34-
query_percentage_threshold: Option<f64>,
35-
36-
/// Size of the time window with which to validate schema against (i.e "24h" or "1w 2d 5h")
37-
#[structopt(long)]
38-
validation_period: Option<ValidationPeriod>,
24+
#[structopt(flatten)]
25+
config: CheckConfigOpts,
3926
}
4027

4128
impl Check {
@@ -60,9 +47,9 @@ impl Check {
6047
proposed_schema,
6148
git_context,
6249
config: CheckConfig {
63-
query_count_threshold: self.query_count_threshold,
64-
query_count_threshold_percentage: self.query_percentage_threshold,
65-
validation_period: self.validation_period.clone(),
50+
query_count_threshold: self.config.query_count_threshold,
51+
query_count_threshold_percentage: self.config.query_percentage_threshold,
52+
validation_period: self.config.validation_period.clone(),
6653
},
6754
},
6855
&client,

src/command/subgraph/check.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use serde::Serialize;
22
use structopt::StructOpt;
33

44
use rover_client::operations::subgraph::check::{self, SubgraphCheckInput};
5-
use rover_client::shared::{CheckConfig, GitContext, ValidationPeriod};
5+
use rover_client::shared::{CheckConfig, GitContext};
66

77
use crate::command::RoverOutput;
8-
use crate::options::{GraphRefOpt, ProfileOpt, SchemaOpt, SubgraphOpt};
8+
use crate::options::{CheckConfigOpts, GraphRefOpt, ProfileOpt, SchemaOpt, SubgraphOpt};
99
use crate::utils::client::StudioClientConfig;
10-
use crate::utils::parsers::{parse_query_count_threshold, parse_query_percentage_threshold};
1110
use crate::Result;
1211

1312
#[derive(Debug, Serialize, StructOpt)]
@@ -25,20 +24,8 @@ pub struct Check {
2524
#[serde(skip_serializing)]
2625
schema: SchemaOpt,
2726

28-
/// The minimum number of times a query or mutation must have been executed
29-
/// in order to be considered in the check operation
30-
#[structopt(long, parse(try_from_str = parse_query_count_threshold))]
31-
query_count_threshold: Option<i64>,
32-
33-
/// Minimum percentage of times a query or mutation must have been executed
34-
/// in the time window, relative to total request count, for it to be
35-
/// considered in the check. Valid numbers are in the range 0 <= x <= 100
36-
#[structopt(long, parse(try_from_str = parse_query_percentage_threshold))]
37-
query_percentage_threshold: Option<f64>,
38-
39-
/// Size of the time window with which to validate schema against (i.e "24h" or "1w 2d 5h")
40-
#[structopt(long)]
41-
validation_period: Option<ValidationPeriod>,
27+
#[structopt(flatten)]
28+
config: CheckConfigOpts,
4229
}
4330

4431
impl Check {
@@ -65,9 +52,9 @@ impl Check {
6552
subgraph: self.subgraph.subgraph_name.clone(),
6653
git_context,
6754
config: CheckConfig {
68-
query_count_threshold: self.query_count_threshold,
69-
query_count_threshold_percentage: self.query_percentage_threshold,
70-
validation_period: self.validation_period.clone(),
55+
query_count_threshold: self.config.query_count_threshold,
56+
query_count_threshold_percentage: self.config.query_percentage_threshold,
57+
validation_period: self.config.validation_period.clone(),
7158
},
7259
},
7360
&client,

src/options/check.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use serde::{Deserialize, Serialize};
2+
use structopt::StructOpt;
3+
4+
use rover_client::shared::ValidationPeriod;
5+
6+
use crate::{error::RoverError, Result};
7+
8+
#[derive(Debug, Serialize, Deserialize, StructOpt)]
9+
pub struct CheckConfigOpts {
10+
/// The minimum number of times a query or mutation must have been executed
11+
/// in order to be considered in the check operation
12+
#[structopt(long, parse(try_from_str = parse_query_count_threshold))]
13+
pub query_count_threshold: Option<i64>,
14+
15+
/// Minimum percentage of times a query or mutation must have been executed
16+
/// in the time window, relative to total request count, for it to be
17+
/// considered in the check. Valid numbers are in the range 0 <= x <= 100
18+
#[structopt(long, parse(try_from_str = parse_query_percentage_threshold))]
19+
pub query_percentage_threshold: Option<f64>,
20+
21+
/// Size of the time window with which to validate schema against (i.e "24h" or "1w 2d 5h")
22+
#[structopt(long)]
23+
pub validation_period: Option<ValidationPeriod>,
24+
}
25+
26+
fn parse_query_count_threshold(threshold: &str) -> Result<i64> {
27+
let threshold = threshold.parse::<i64>()?;
28+
if threshold < 1 {
29+
Err(RoverError::parse_error(
30+
"The number of queries must be a positive integer.",
31+
))
32+
} else {
33+
Ok(threshold)
34+
}
35+
}
36+
37+
fn parse_query_percentage_threshold(threshold: &str) -> Result<f64> {
38+
let threshold = threshold.parse::<i64>()?;
39+
if !(0..=100).contains(&threshold) {
40+
Err(RoverError::parse_error(
41+
"Valid numbers are in the range 0 <= x <= 100",
42+
))
43+
} else {
44+
Ok((threshold / 100) as f64)
45+
}
46+
}

src/options/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
mod check;
12
mod graph;
23
mod profile;
34
mod schema;
45
mod subgraph;
56

7+
pub(crate) use check::CheckConfigOpts;
68
pub(crate) use graph::GraphRefOpt;
79
pub(crate) use profile::ProfileOpt;
810
pub(crate) use schema::SchemaOpt;

src/utils/parsers.rs

-22
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,6 @@ pub fn parse_file_descriptor(input: &str) -> Result<FileDescriptorType> {
8585
}
8686
}
8787

88-
pub fn parse_query_count_threshold(threshold: &str) -> Result<i64> {
89-
let threshold = threshold.parse::<i64>()?;
90-
if threshold < 1 {
91-
Err(RoverError::parse_error(
92-
"The number of queries must be a positive integer.",
93-
))
94-
} else {
95-
Ok(threshold)
96-
}
97-
}
98-
99-
pub fn parse_query_percentage_threshold(threshold: &str) -> Result<f64> {
100-
let threshold = threshold.parse::<i64>()?;
101-
if !(0..=100).contains(&threshold) {
102-
Err(RoverError::parse_error(
103-
"Valid numbers are in the range 0 <= x <= 100",
104-
))
105-
} else {
106-
Ok((threshold / 100) as f64)
107-
}
108-
}
109-
11088
/// Parses a key:value pair from a string and returns a tuple of key:value.
11189
/// If a full key:value can't be parsed, it will error.
11290
pub fn parse_header(header: &str) -> Result<(String, String)> {

0 commit comments

Comments
 (0)