make server_addr configurable for client
This commit is contained in:
parent
5cf848f7fe
commit
cab5ec40cc
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "creddy",
|
"name": "creddy",
|
||||||
"version": "0.5.2",
|
"version": "0.5.3",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
|
2
src-tauri/Cargo.lock
generated
2
src-tauri/Cargo.lock
generated
@ -1196,7 +1196,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "creddy"
|
name = "creddy"
|
||||||
version = "0.5.2"
|
version = "0.5.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argon2",
|
"argon2",
|
||||||
"auto-launch",
|
"auto-launch",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "creddy"
|
name = "creddy"
|
||||||
version = "0.5.2"
|
version = "0.5.3"
|
||||||
description = "A friendly AWS credentials manager"
|
description = "A friendly AWS credentials manager"
|
||||||
authors = ["Joseph Montanaro"]
|
authors = ["Joseph Montanaro"]
|
||||||
license = ""
|
license = ""
|
||||||
|
@ -11,17 +11,12 @@ use std::{
|
|||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = cli::parser().get_matches();
|
let global_matches = cli::parser().get_matches();
|
||||||
if let Some(true) = args.get_one::<bool>("help") {
|
let res = match global_matches.subcommand() {
|
||||||
cli::parser().print_help().unwrap(); // if we can't print help we can't print an error
|
|
||||||
process::exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
let res = match args.subcommand() {
|
|
||||||
None | Some(("run", _)) => launch_gui(),
|
None | Some(("run", _)) => launch_gui(),
|
||||||
Some(("get", m)) => cli::get(m),
|
Some(("get", m)) => cli::get(m, &global_matches),
|
||||||
Some(("exec", m)) => cli::exec(m),
|
Some(("exec", m)) => cli::exec(m, &global_matches),
|
||||||
Some(("shortcut", m)) => cli::invoke_shortcut(m),
|
Some(("shortcut", m)) => cli::invoke_shortcut(m, &global_matches),
|
||||||
_ => unreachable!("Unknown subcommand"),
|
_ => unreachable!("Unknown subcommand"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::process::Command as ChildCommand;
|
use std::process::Command as ChildCommand;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@ -9,6 +10,7 @@ use clap::{
|
|||||||
ArgMatches,
|
ArgMatches,
|
||||||
ArgAction,
|
ArgAction,
|
||||||
builder::PossibleValuesParser,
|
builder::PossibleValuesParser,
|
||||||
|
value_parser,
|
||||||
};
|
};
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
|
||||||
@ -37,6 +39,14 @@ pub fn parser() -> Command<'static> {
|
|||||||
Command::new("creddy")
|
Command::new("creddy")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.about("A friendly AWS credentials manager")
|
.about("A friendly AWS credentials manager")
|
||||||
|
.arg(
|
||||||
|
Arg::new("server_addr")
|
||||||
|
.short('a')
|
||||||
|
.long("server-addr")
|
||||||
|
.takes_value(true)
|
||||||
|
.value_parser(value_parser!(PathBuf))
|
||||||
|
.help("Connect to the main Creddy process at this address")
|
||||||
|
)
|
||||||
.subcommand(
|
.subcommand(
|
||||||
Command::new("run")
|
Command::new("run")
|
||||||
.about("Launch Creddy")
|
.about("Launch Creddy")
|
||||||
@ -71,6 +81,7 @@ pub fn parser() -> Command<'static> {
|
|||||||
Arg::new("name")
|
Arg::new("name")
|
||||||
.short('n')
|
.short('n')
|
||||||
.long("name")
|
.long("name")
|
||||||
|
.takes_value(true)
|
||||||
.help("If unspecified, use default credentials")
|
.help("If unspecified, use default credentials")
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
@ -91,11 +102,12 @@ pub fn parser() -> Command<'static> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn get(args: &ArgMatches) -> Result<(), CliError> {
|
pub fn get(args: &ArgMatches, global_args: &ArgMatches) -> Result<(), CliError> {
|
||||||
let name = args.get_one("name").cloned();
|
let name = args.get_one("name").cloned();
|
||||||
let base = *args.get_one("base").unwrap_or(&false);
|
let base = *args.get_one("base").unwrap_or(&false);
|
||||||
|
let addr = global_args.get_one("server_addr").cloned();
|
||||||
|
|
||||||
let output = match make_request(&Request::GetAwsCredentials { name, base })? {
|
let output = match make_request(addr, &Request::GetAwsCredentials { name, base })? {
|
||||||
Response::AwsBase(creds) => serde_json::to_string(&creds).unwrap(),
|
Response::AwsBase(creds) => serde_json::to_string(&creds).unwrap(),
|
||||||
Response::AwsSession(creds) => serde_json::to_string(&creds).unwrap(),
|
Response::AwsSession(creds) => serde_json::to_string(&creds).unwrap(),
|
||||||
r => return Err(RequestError::Unexpected(r).into()),
|
r => return Err(RequestError::Unexpected(r).into()),
|
||||||
@ -105,9 +117,10 @@ pub fn get(args: &ArgMatches) -> Result<(), CliError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn exec(args: &ArgMatches) -> Result<(), CliError> {
|
pub fn exec(args: &ArgMatches, global_args: &ArgMatches) -> Result<(), CliError> {
|
||||||
let name = args.get_one("name").cloned();
|
let name = args.get_one("name").cloned();
|
||||||
let base = *args.get_one("base").unwrap_or(&false);
|
let base = *args.get_one("base").unwrap_or(&false);
|
||||||
|
let addr = global_args.get_one("server_addr").cloned();
|
||||||
let mut cmd_line = args.get_many("command")
|
let mut cmd_line = args.get_many("command")
|
||||||
.ok_or(ExecError::NoCommand)?;
|
.ok_or(ExecError::NoCommand)?;
|
||||||
|
|
||||||
@ -115,7 +128,7 @@ pub fn exec(args: &ArgMatches) -> Result<(), CliError> {
|
|||||||
let mut cmd = ChildCommand::new(cmd_name);
|
let mut cmd = ChildCommand::new(cmd_name);
|
||||||
cmd.args(cmd_line);
|
cmd.args(cmd_line);
|
||||||
|
|
||||||
match make_request(&Request::GetAwsCredentials { name, base })? {
|
match make_request(addr, &Request::GetAwsCredentials { name, base })? {
|
||||||
Response::AwsBase(creds) => {
|
Response::AwsBase(creds) => {
|
||||||
cmd.env("AWS_ACCESS_KEY_ID", creds.access_key_id);
|
cmd.env("AWS_ACCESS_KEY_ID", creds.access_key_id);
|
||||||
cmd.env("AWS_SECRET_ACCESS_KEY", creds.secret_access_key);
|
cmd.env("AWS_SECRET_ACCESS_KEY", creds.secret_access_key);
|
||||||
@ -159,7 +172,8 @@ pub fn exec(args: &ArgMatches) -> Result<(), CliError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn invoke_shortcut(args: &ArgMatches) -> Result<(), CliError> {
|
pub fn invoke_shortcut(args: &ArgMatches, global_args: &ArgMatches) -> Result<(), CliError> {
|
||||||
|
let addr = global_args.get_one("server_addr").cloned();
|
||||||
let action = match args.get_one::<String>("action").map(|s| s.as_str()) {
|
let action = match args.get_one::<String>("action").map(|s| s.as_str()) {
|
||||||
Some("show_window") => ShortcutAction::ShowWindow,
|
Some("show_window") => ShortcutAction::ShowWindow,
|
||||||
Some("launch_terminal") => ShortcutAction::LaunchTerminal,
|
Some("launch_terminal") => ShortcutAction::LaunchTerminal,
|
||||||
@ -167,7 +181,7 @@ pub fn invoke_shortcut(args: &ArgMatches) -> Result<(), CliError> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let req = Request::InvokeShortcut(action);
|
let req = Request::InvokeShortcut(action);
|
||||||
match make_request(&req) {
|
match make_request(addr, &req) {
|
||||||
Ok(Response::Empty) => Ok(()),
|
Ok(Response::Empty) => Ok(()),
|
||||||
Ok(r) => Err(RequestError::Unexpected(r).into()),
|
Ok(r) => Err(RequestError::Unexpected(r).into()),
|
||||||
Err(e) => Err(e.into()),
|
Err(e) => Err(e.into()),
|
||||||
@ -176,12 +190,12 @@ pub fn invoke_shortcut(args: &ArgMatches) -> Result<(), CliError> {
|
|||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn make_request(req: &Request) -> Result<Response, RequestError> {
|
async fn make_request(addr: Option<PathBuf>, req: &Request) -> Result<Response, RequestError> {
|
||||||
let mut data = serde_json::to_string(req).unwrap();
|
let mut data = serde_json::to_string(req).unwrap();
|
||||||
// server expects newline marking end of request
|
// server expects newline marking end of request
|
||||||
data.push('\n');
|
data.push('\n');
|
||||||
|
|
||||||
let mut stream = connect().await?;
|
let mut stream = connect(addr).await?;
|
||||||
stream.write_all(&data.as_bytes()).await?;
|
stream.write_all(&data.as_bytes()).await?;
|
||||||
|
|
||||||
let mut buf = Vec::with_capacity(1024);
|
let mut buf = Vec::with_capacity(1024);
|
||||||
@ -192,10 +206,10 @@ async fn make_request(req: &Request) -> Result<Response, RequestError> {
|
|||||||
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
async fn connect() -> Result<NamedPipeClient, std::io::Error> {
|
async fn connect(addr: Option<PathBuf>) -> Result<NamedPipeClient, std::io::Error> {
|
||||||
// apparently attempting to connect can fail if there's already a client connected
|
// apparently attempting to connect can fail if there's already a client connected
|
||||||
loop {
|
loop {
|
||||||
let addr = srv::addr("creddy-server");
|
let addr = addr.unwrap_or_else(|| srv::addr("creddy-server"));
|
||||||
match ClientOptions::new().open(&addr) {
|
match ClientOptions::new().open(&addr) {
|
||||||
Ok(stream) => return Ok(stream),
|
Ok(stream) => return Ok(stream),
|
||||||
Err(e) if e.raw_os_error() == Some(ERROR_PIPE_BUSY.0 as i32) => (),
|
Err(e) if e.raw_os_error() == Some(ERROR_PIPE_BUSY.0 as i32) => (),
|
||||||
@ -207,7 +221,7 @@ async fn connect() -> Result<NamedPipeClient, std::io::Error> {
|
|||||||
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
async fn connect() -> Result<UnixStream, std::io::Error> {
|
async fn connect(addr: Option<PathBuf>) -> Result<UnixStream, std::io::Error> {
|
||||||
let path = srv::addr("creddy-server");
|
let path = addr.unwrap_or_else(|| srv::addr("creddy-server"));
|
||||||
UnixStream::connect(&path).await
|
UnixStream::connect(&path).await
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"productName": "creddy",
|
"productName": "creddy",
|
||||||
"version": "0.5.2",
|
"version": "0.5.3",
|
||||||
"identifier": "creddy",
|
"identifier": "creddy",
|
||||||
"plugins": {},
|
"plugins": {},
|
||||||
"app": {
|
"app": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user