Skip to content

Commit

Permalink
Loads application configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Apr 13, 2024
1 parent 5d83841 commit 98b5015
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ edition = "2021"

[dependencies]
clap = "4.4"
dirs = "5.0.1"
erdp = "0.1.0"
serde = { version = "1.0.197", features = ["derive"] }
serde_yaml = "0.9.34"
ureq = "2.9.6"
url = { version = "2.5.0", features = ["serde"] }
23 changes: 23 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use serde::Deserialize;
use url::Url;

/// Application configurations.
#[derive(Deserialize)]
pub struct AppConfig {
#[serde(default = "AppConfig::default_default_server")]
pub default_server: Url,
}

impl AppConfig {
fn default_default_server() -> Url {
Url::parse("https://api.warpgate.sh").unwrap()
}
}

impl Default for AppConfig {
fn default() -> Self {
Self {
default_server: Self::default_default_server(),
}
}
}
47 changes: 45 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
use crate::config::AppConfig;
use clap::{value_parser, Arg, ArgMatches, Command};
use dirs::home_dir;
use erdp::ErrorDisplay;
use std::fs::File;
use std::io::BufReader;
use std::{path::PathBuf, process::ExitCode};

mod config;

fn main() -> ExitCode {
// Get our home directory.
let mut home = match home_dir() {
Some(v) => v,
None => {
eprintln!("Failed to locate home directory.");
return ExitCode::FAILURE;
}
};

home.push(".warp");

// Create our home if not exists.
if let Err(e) = std::fs::create_dir(&home) {
if e.kind() != std::io::ErrorKind::AlreadyExists {
eprintln!("Failed to create {}: {}.", home.display(), e.display());
return ExitCode::FAILURE;
}
}

// Load application configurations.
let path = home.join("config.yml");
let config = match File::open(&path) {
Ok(v) => match serde_yaml::from_reader::<_, AppConfig>(BufReader::new(v)) {
Ok(v) => v,
Err(e) => {
eprintln!("Failed to load {}: {}.", path.display(), e.display());
return ExitCode::FAILURE;
}
},
Err(e) if e.kind() == std::io::ErrorKind::NotFound => AppConfig::default(),
Err(e) => {
eprintln!("Failed to open {}: {}.", path.display(), e.display());
return ExitCode::FAILURE;
}
};

// Parse arguments.
let args = Command::new("warp")
.subcommand(
Command::new("init")
.about("Setup an existing directory to be resuming on another computer")
.about("Setup an existing directory to be resume on another computer")
.arg(Arg::new("name").help(
"Unique name of this directory on the server (default to directory name)",
).long("name").value_name("NAME"))
.arg(
Arg::new("server")
.help("URL of the server to use (default to https://api.warpgate.sh)").long("server").value_name("URL"),
.help(format!("URL of the server to use (default to {})", config.default_server)).long("server").value_name("URL"),
).arg(Arg::new("directory").help("The directory to setup (default to current directory)").value_name("DIRECTORY").value_parser(value_parser!(PathBuf))),
)
.get_matches();
Expand Down

0 comments on commit 98b5015

Please sign in to comment.