Add `profile::env` module

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-11-03 13:05:28 +01:00
parent 577b632056
commit 092ccfdcbc
No known key found for this signature in database
2 changed files with 18 additions and 4 deletions

View File

@ -2,6 +2,7 @@ use std::{env, net, process, thread};
use anyhow::Context as _; use anyhow::Context as _;
use radicle::profile;
use radicle_node::crypto::ssh::keystore::MemorySigner; use radicle_node::crypto::ssh::keystore::MemorySigner;
use radicle_node::logger; use radicle_node::logger;
use radicle_node::prelude::Address; use radicle_node::prelude::Address;
@ -18,6 +19,7 @@ struct Options {
impl Options { impl Options {
fn from_env() -> Result<Self, lexopt::Error> { fn from_env() -> Result<Self, lexopt::Error> {
use lexopt::prelude::*; use lexopt::prelude::*;
let mut parser = lexopt::Parser::from_env(); let mut parser = lexopt::Parser::from_env();
let mut connect = Vec::new(); let mut connect = Vec::new();
let mut listen = Vec::new(); let mut listen = Vec::new();
@ -53,7 +55,7 @@ fn main() -> anyhow::Result<()> {
let signer = match profile.signer() { let signer = match profile.signer() {
Ok(signer) => signer.boxed(), Ok(signer) => signer.boxed(),
Err(err) => { Err(err) => {
let passphrase = env::var("RAD_PASSPHRASE") let passphrase = env::var(profile::env::RAD_PASSPHRASE)
.context("Either ssh-agent must be initialized, or `RAD_PASSPHRASE` must be set") .context("Either ssh-agent must be initialized, or `RAD_PASSPHRASE` must be set")
.context(err)?; .context(err)?;
MemorySigner::load(&profile.keystore, &passphrase)?.boxed() MemorySigner::load(&profile.keystore, &passphrase)?.boxed()

View File

@ -10,8 +10,8 @@
//! node/ //! node/
//! radicle.sock # Node control socket //! radicle.sock # Node control socket
//! //!
use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::{env, io};
use thiserror::Error; use thiserror::Error;
@ -22,6 +22,18 @@ use crate::node;
use crate::storage::git::transport; use crate::storage::git::transport;
use crate::storage::git::Storage; use crate::storage::git::Storage;
/// Environment variables used by radicle.
pub mod env {
pub use std::env::*;
/// Path to the radicle home folder.
pub const RAD_HOME: &str = "RAD_HOME";
/// Path to the radicle node socket file.
pub const RAD_SOCKET: &str = "RAD_SOCKET";
/// Passphrase for the encrypted radicle secret key.
pub const RAD_PASSPHRASE: &str = "RAD_PASSPHRASE";
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error(transparent)] #[error(transparent)]
@ -107,7 +119,7 @@ impl Profile {
/// Get the path to the radicle node socket. /// Get the path to the radicle node socket.
pub fn node(&self) -> PathBuf { pub fn node(&self) -> PathBuf {
env::var_os("RAD_SOCKET") env::var_os(env::RAD_SOCKET)
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(|| self.home.join("node").join(node::DEFAULT_SOCKET_NAME)) .unwrap_or_else(|| self.home.join("node").join(node::DEFAULT_SOCKET_NAME))
} }
@ -115,7 +127,7 @@ impl Profile {
/// Get the path to the radicle home folder. /// Get the path to the radicle home folder.
pub fn home() -> Result<PathBuf, io::Error> { pub fn home() -> Result<PathBuf, io::Error> {
if let Some(home) = env::var_os("RAD_HOME") { if let Some(home) = env::var_os(env::RAD_HOME) {
Ok(PathBuf::from(home)) Ok(PathBuf::from(home))
} else if let Some(home) = env::var_os("HOME") { } else if let Some(home) = env::var_os("HOME") {
Ok(PathBuf::from(home).join(".radicle")) Ok(PathBuf::from(home).join(".radicle"))