Skip to content

Commit

Permalink
feat(tui): single connector from cli arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Sep 23, 2024
1 parent b6f4655 commit cbe5ab7
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 15 deletions.
112 changes: 110 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions edc-connector-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
crossterm = "0.28.1"
ratatui = "0.28.1"
tui-textarea = "0.6"
clap = { version = "4.5.18", features = ["derive"] }
anyhow = "1.0.89"
dirs-next = "2.0.0"
toml = "0.8.12"
Expand Down
17 changes: 11 additions & 6 deletions edc-connector-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ impl App {
Connector::new(cfg, client, status)
}

pub fn init(cfg: Config) -> App {
let connectors = cfg
.connectors
.into_iter()
.map(App::init_connector)
.collect();
pub fn init_with_connectors(connectors: Vec<Connector>) -> App {
let connectors = ConnectorsComponent::new(connectors);

let sheet = connectors.info_sheet().merge(Self::info_sheet());
Expand All @@ -106,6 +101,16 @@ impl App {
}
}

pub fn init(cfg: Config) -> App {
let connectors = cfg
.connectors
.into_iter()
.map(App::init_connector)
.collect();

Self::init_with_connectors(connectors)
}

pub fn info_sheet() -> InfoSheet {
InfoSheet::default()
.key_binding("<tab>", "Switch menu")
Expand Down
3 changes: 2 additions & 1 deletion edc-connector-tui/src/components/connectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ impl Component for ConnectorsComponent {

impl ConnectorsComponent {
pub fn new(connectors: Vec<Connector>) -> Self {
let selected = connectors.first().cloned();
Self {
table: ConnectorsTable::with_elements(
"Connectors".to_string(),
connectors.into_iter().map(ConnectorEntry).collect(),
true,
)
.on_select(|connector| Box::new(ConnectorsMsg::ConnectorSelected(connector.0.clone()))),
selected: None,
selected,
}
}

Expand Down
10 changes: 9 additions & 1 deletion edc-connector-tui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn get_app_config_path() -> anyhow::Result<std::path::PathBuf> {
}
.ok_or_else(|| anyhow::anyhow!("failed to find os config dir."))?;

path.push("edc-tui");
path.push("edc-connector-tui");
std::fs::create_dir_all(&path)?;
Ok(path)
}
Expand Down Expand Up @@ -72,6 +72,14 @@ impl AuthKind {
}

impl ConnectorConfig {
pub fn new(name: String, address: String, auth: AuthKind) -> Self {
Self {
name,
address,
auth,
}
}

pub fn name(&self) -> &str {
&self.name
}
Expand Down
61 changes: 56 additions & 5 deletions edc-connector-tui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use app::App;
use config::{default_file, Config};
use clap::{Parser, Subcommand};
use config::{default_file, Config, ConnectorConfig};
use edc_connector_client::{Auth, EdcConnectorClient};
use runner::Runner;
use std::time::Duration;

use std::{path::PathBuf, time::Duration};
use types::connector::{Connector, ConnectorStatus};
mod app;
mod components;
mod config;
Expand All @@ -11,10 +13,37 @@ mod types;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = Config::parse(&default_file()?)?;
let cli = Cli::parse();
tui::install_panic_hook();
let terminal = tui::init_terminal()?;
let mut runner = Runner::new(Duration::from_millis(250), App::init(config));

let app = match cli.mode {
Some(Commands::Connector { url, name, token }) => {
let auth = token.map(Auth::api_token).unwrap_or_else(|| Auth::NoAuth);
let client = EdcConnectorClient::builder()
.management_url(url.clone())
.with_auth(auth)
.build()
.unwrap();

let cfg = ConnectorConfig::new(
name.unwrap_or_else(|| url.clone()),
url,
config::AuthKind::Token {
token_alias: "unknown".to_string(),
},
);

let connector = Connector::new(cfg, client, ConnectorStatus::Connected);

App::init_with_connectors(vec![connector])
}
None => {
let config = Config::parse(&cli.config.map(Ok).unwrap_or_else(default_file)?)?;
App::init(config)
}
};
let mut runner = Runner::new(Duration::from_millis(250), app);
runner.run(terminal).await?;
tui::restore_terminal()?;
Ok(())
Expand Down Expand Up @@ -53,3 +82,25 @@ mod tui {
}));
}
}

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Sets a custom config file
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
#[command(subcommand)]
mode: Option<Commands>,
}

#[derive(Subcommand)]
pub enum Commands {
Connector {
#[arg(short, long)]
name: Option<String>,
#[arg(short, long)]
url: String,
#[arg(short, long)]
token: Option<String>,
},
}

0 comments on commit cbe5ab7

Please sign in to comment.