Skip to content

Commit

Permalink
Changes based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed May 7, 2024
1 parent 67bdac1 commit af48057
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 58 deletions.
5 changes: 2 additions & 3 deletions rust/agama-cli/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::{arg, Args, Subcommand};

use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use rpassword::{prompt_password, read_password};
use std::fs;
use std::fs::File;
use std::io;
Expand Down Expand Up @@ -124,7 +123,7 @@ fn jwt_file() -> Option<PathBuf> {
}
/// Path to agama-live token file.
fn agama_token_file() -> Option<PathBuf> {
Some(home::home_dir()?.join(DEFAULT_AGAMA_TOKEN_FILE))
home::home_dir().map(|p| p.join(DEFAULT_AGAMA_TOKEN_FILE))
}

/// Reads first line from given file
Expand Down Expand Up @@ -155,7 +154,7 @@ fn read_line_from_file(path: &Path) -> io::Result<String> {
/// Asks user to provide a line of input. Displays a prompt.
fn read_credential(caption: String) -> io::Result<String> {
let caption = format!("{}: ", caption);
let cred = prompt_password(caption.clone()).unwrap();
let cred = rpassword::prompt_password(caption.clone()).unwrap();
if cred.is_empty() {
return Err(io::Error::new(
io::ErrorKind::Other,
Expand Down
101 changes: 49 additions & 52 deletions rust/agama-cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use crate::auth;
use crate::error::CliError;
use crate::printers::{print, Format};
use agama_lib::connection;
use agama_lib::install_settings::{InstallSettings, Scope};
use agama_lib::Store as SettingsStore;
use crate::{
auth,
error::CliError,
printers::{print, Format},
};
use agama_lib::{
connection,
install_settings::{InstallSettings, Scope},
Store as SettingsStore,
};
use agama_settings::{settings::Settings, SettingObject, SettingValue};
use clap::Subcommand;
use convert_case::{Case, Casing};
use std::str::FromStr;
use std::{collections::HashMap, error::Error, io};
use std::{collections::HashMap, error::Error, io, str::FromStr};

#[derive(Subcommand, Debug)]
pub enum ConfigCommands {
Expand All @@ -33,56 +36,50 @@ pub enum ConfigAction {
}

fn token() -> Option<String> {
match auth::jwt() {
Ok(token) => return Some(token),
Err(_) => match auth::agama_token() {
Ok(token) => return Some(token),
Err(_) => return None,
},
}
auth::jwt().or_else(|_| auth::agama_token()).ok()
}

pub async fn run(subcommand: ConfigCommands, format: Format) -> anyhow::Result<()> {
if let Some(token) = token() {
let client = agama_lib::http_client(token)?;
let store = SettingsStore::new(connection().await?, client).await?;
let Some(token) = token() else {
println!("You need to login for generating a valid token");
return Ok(());
};

let command = parse_config_command(subcommand)?;
match command {
ConfigAction::Set(changes) => {
let scopes = changes
.keys()
.filter_map(|k| key_to_scope(k).ok())
.collect();
let mut model = store.load(Some(scopes)).await?;
for (key, value) in changes {
model.set(&key.to_case(Case::Snake), SettingValue(value))?;
}
Ok(store.store(&model).await?)
}
ConfigAction::Show => {
let model = store.load(None).await?;
print(model, io::stdout(), format)?;
Ok(())
}
ConfigAction::Add(key, values) => {
let scope = key_to_scope(&key).unwrap();
let mut model = store.load(Some(vec![scope])).await?;
model.add(&key.to_case(Case::Snake), SettingObject::from(values))?;
Ok(store.store(&model).await?)
}
ConfigAction::Load(path) => {
let contents = std::fs::read_to_string(path)?;
let result: InstallSettings = serde_json::from_str(&contents)?;
let scopes = result.defined_scopes();
let mut model = store.load(Some(scopes)).await?;
model.merge(&result);
Ok(store.store(&model).await?)
let client = agama_lib::http_client(token)?;
let store = SettingsStore::new(connection().await?, client).await?;

let command = parse_config_command(subcommand)?;
match command {
ConfigAction::Set(changes) => {
let scopes = changes
.keys()
.filter_map(|k| key_to_scope(k).ok())
.collect();
let mut model = store.load(Some(scopes)).await?;
for (key, value) in changes {
model.set(&key.to_case(Case::Snake), SettingValue(value))?;
}
Ok(store.store(&model).await?)
}
ConfigAction::Show => {
let model = store.load(None).await?;
print(model, io::stdout(), format)?;
Ok(())
}
ConfigAction::Add(key, values) => {
let scope = key_to_scope(&key).unwrap();
let mut model = store.load(Some(vec![scope])).await?;
model.add(&key.to_case(Case::Snake), SettingObject::from(values))?;
Ok(store.store(&model).await?)
}
ConfigAction::Load(path) => {
let contents = std::fs::read_to_string(path)?;
let result: InstallSettings = serde_json::from_str(&contents)?;
let scopes = result.defined_scopes();
let mut model = store.load(Some(scopes)).await?;
model.merge(&result);
Ok(store.store(&model).await?)
}
} else {
println!("You need to login for generating a valid token");
Ok(())
}
}

Expand Down
6 changes: 3 additions & 3 deletions rust/agama-lib/src/network/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl NetworkClient {
}

async fn text_for(&self, response: Response) -> Result<String, ServiceError> {
let status = response.status().clone();
let status = response.status();
let text = response
.text()
.await
Expand All @@ -42,7 +42,7 @@ impl NetworkClient {
.await
.map_err(|e| ServiceError::NetworkClientError(e.to_string()))?;

Ok(self.text_for(response).await?)
self.text_for(response).await
}

/// Returns an array of network devices
Expand Down Expand Up @@ -82,7 +82,7 @@ impl NetworkClient {
let id = connection.id.clone();
let response = self.connection(id.as_str()).await;

if let Ok(_) = response {
if response.is_ok() {
let path = format!("{API_URL}/connections/{id}");
self.client
.put(path)
Expand Down

0 comments on commit af48057

Please sign in to comment.