Skip to content

Commit

Permalink
feat(#13): add clap, port argument
Browse files Browse the repository at this point in the history
  • Loading branch information
l3r8yJ committed Jun 22, 2024
1 parent 496125b commit e444f34
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
11 changes: 8 additions & 3 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ version = "0.1.0"
edition = "2021"

[[bin]]
name = "fakehub-cli"
name = "cli"
path = "src/main.rs"

[dev-dependencies]
assert_cmd = "2.0.14"

[dependencies]
clap = "4.5.7"
anyhow = "1.0.86"
clap = { version = "4.5.7", features = ["derive"] }
server = { path = "../server" }
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
log = "0.4.21"
12 changes: 12 additions & 0 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use clap::Parser;

// @todo #13:15min Handle port argument
// We should process the port argument and
// pass it to the server on `start` command.
// Start command should be added also with clap
#[derive(Parser, Debug)]
pub(crate) struct Args {
/// The port to run
#[arg(short, long, default_value_t = 3000)]
pub(crate) port: usize,
}
51 changes: 25 additions & 26 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use server::Server;
use clap::Parser;

#[tokio::main]
async fn main() {
let server = Server::new(3000);
server.start().await.expect("Can't start the server");
use crate::args::Args;

mod args;

fn main() {
let _ = Args::parse();
}

#[cfg(test)]
mod tests {
use std::str;

use anyhow::Result;
use assert_cmd::Command;

#[test]
fn should_output_help() -> Result<()> {
let assertion = Command::cargo_bin("cli")?.arg("--help").assert();
let bytes = assertion.get_output().stdout.as_slice();
let output = str::from_utf8(bytes)?;
assert!(output.contains("The port to run"));
assert!(output.contains("3000"));
Ok(())
}
}
6 changes: 4 additions & 2 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// SOFTWARE.
use std::io;

use anyhow::Result;
use axum::routing::get;
use axum::Router;
use tokio::net::TcpListener;
Expand All @@ -43,7 +44,7 @@ impl Server {
}

impl Server {
pub async fn start(self) -> anyhow::Result<()> {
pub async fn start(self) -> Result<()> {
touch_storage(Some("fakehub.xml"));
let app: Router = Router::new().route("/", get(home::home));
let addr: String = format!("0.0.0.0:{}", self.port);
Expand All @@ -60,9 +61,10 @@ impl Server {

#[cfg(test)]
mod tests {
use anyhow::Result;

#[test]
fn creates_the_server() -> anyhow::Result<()> {
fn creates_the_server() -> Result<()> {
let server = crate::Server::new(1234);
assert_eq!(server.port, 1234);
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions server/src/xml/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ pub fn touch_storage(path: Option<&str>) -> File {

#[cfg(test)]
mod tests {
use anyhow::Result;
use tempdir::TempDir;

use crate::xml::storage::touch_storage;

#[test]
fn creates_xml_storage() -> anyhow::Result<()> {
fn creates_xml_storage() -> Result<()> {
let temp = TempDir::new("temp")?;
let path = temp.path().join("fakehub.xml");
let storage = path.to_str();
Expand All @@ -58,7 +59,7 @@ mod tests {
}

#[test]
fn creates_xml_storage_with_different_name() -> anyhow::Result<()> {
fn creates_xml_storage_with_different_name() -> Result<()> {
let temp = TempDir::new("temp")?;
let path = temp.path().join("test.xml");
let storage = path.to_str();
Expand Down

0 comments on commit e444f34

Please sign in to comment.