Skip to content

Commit

Permalink
feat(cli): add feature to make context a required field
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-prosser committed Apr 3, 2024
1 parent dcda277 commit 069f72f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased
- Add option to make context option a required field
- Make context option a required field for internal users

# v0.23.0

- Add `get emails`
Expand Down
14 changes: 13 additions & 1 deletion cli/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::Path;
use structopt::StructOpt;

use crate::{
config::{self, ContextConfig, ReinferConfig},
config::{self, write_reinfer_config, ContextConfig, ReinferConfig},
utils,
};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -68,6 +68,14 @@ pub enum ConfigArgs {
/// The name of the context.
name: String,
},

#[structopt(name = "set-context-required")]
/// Set whether context is a required field
SetContextRequired {
// Whether the context is a required field
#[structopt(name = "is-required", parse(try_from_str))]
is_required: bool,
},
}

pub fn run(
Expand All @@ -76,6 +84,10 @@ pub fn run(
config_path: impl AsRef<Path>,
) -> Result<ReinferConfig> {
match args {
ConfigArgs::SetContextRequired { is_required } => {
config.context_is_required = *is_required;
write_reinfer_config(config_path, &config)?
}
ConfigArgs::ListContexts { tokens } if config.num_contexts() > 0 => {
let mut contexts = config.get_all_contexts().clone();
contexts.sort_unstable_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
Expand Down
2 changes: 2 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::{
pub struct ReinferConfig {
current_context: Option<String>,
contexts: Vec<ContextConfig>,
#[serde(default)]
pub context_is_required: bool,
}

impl ReinferConfig {
Expand Down
36 changes: 34 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,46 @@ fn client_from_args(args: &Args, config: &ReinferConfig) -> Result<Client> {
backoff_factor: 2.0,
};

Client::new(ClientConfig {
let client = Client::new(ClientConfig {
endpoint,
token,
accept_invalid_certificates,
proxy,
retry_config: Some(retry_config),
})
.context("Failed to initialise the HTTP client.")
.context("Failed to initialise the HTTP client.")?;

check_if_context_is_a_required_field(config, &client, &args)?;

Ok(client)
}

const DOMAINS_THAT_REQUIRE_CONTEXT: [&str; 2] = ["uipath.com", "reinfer.io"];

fn check_if_context_is_a_required_field(
config: &ReinferConfig,
client: &Client,
args: &Args,
) -> Result<()> {
if config.context_is_required {
return Err(anyhow!(
"Please provide a context with the `re -c <context>` option"
));
}

let current_user = client.get_current_user()?;

if DOMAINS_THAT_REQUIRE_CONTEXT
.iter()
.any(|domain| current_user.email.0.ends_with(domain))
&& args.context.is_none()
{
return Err(anyhow!(
"As a UiPath user, please provide a context with the `re -c <context>` option"
));
};

Ok(())
}

fn find_configuration(args: &Args) -> Result<PathBuf> {
Expand Down

0 comments on commit 069f72f

Please sign in to comment.