Add profile::env module

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier
2022-11-03 13:06:08 +01:00
parent 577b632056
commit 092ccfdcbc
2 changed files with 18 additions and 4 deletions
+3 -1
View File
@@ -2,6 +2,7 @@ use std::{env, net, process, thread};
use anyhow::Context as _;
use radicle::profile;
use radicle_node::crypto::ssh::keystore::MemorySigner;
use radicle_node::logger;
use radicle_node::prelude::Address;
@@ -18,6 +19,7 @@ struct Options {
impl Options {
fn from_env() -> Result<Self, lexopt::Error> {
use lexopt::prelude::*;
let mut parser = lexopt::Parser::from_env();
let mut connect = Vec::new();
let mut listen = Vec::new();
@@ -53,7 +55,7 @@ fn main() -> anyhow::Result<()> {
let signer = match profile.signer() {
Ok(signer) => signer.boxed(),
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(err)?;
MemorySigner::load(&profile.keystore, &passphrase)?.boxed()
+15 -3
View File
@@ -10,8 +10,8 @@
//! node/
//! radicle.sock # Node control socket
//!
use std::io;
use std::path::PathBuf;
use std::{env, io};
use thiserror::Error;
@@ -22,6 +22,18 @@ use crate::node;
use crate::storage::git::transport;
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)]
pub enum Error {
#[error(transparent)]
@@ -107,7 +119,7 @@ impl Profile {
/// Get the path to the radicle node socket.
pub fn node(&self) -> PathBuf {
env::var_os("RAD_SOCKET")
env::var_os(env::RAD_SOCKET)
.map(PathBuf::from)
.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.
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))
} else if let Some(home) = env::var_os("HOME") {
Ok(PathBuf::from(home).join(".radicle"))