Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Reduce cognitive complexity in main
Browse files Browse the repository at this point in the history
This replaces our massive clap App with a much simpler Cli struct that
has all the same information in an easier to understand and modify
format (types). Because everything is now typed (and because we've moved
clap::App creation out of main), we can further reduce the cognitive
complexity of main by removing lots of parameter checking and parsing,
removing that pesky clippy lint attribute.
  • Loading branch information
ObsidianMinor committed May 25, 2021
1 parent 76b1e64 commit b54d2c8
Show file tree
Hide file tree
Showing 18 changed files with 953 additions and 1,329 deletions.
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ semver = "0.11.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.60"
serde_with = "1.5.1"
structopt = "0.3.21"
sys-info = "0.9"
tempfile = "3.1.0"
term_size = "0.3"
Expand Down
26 changes: 0 additions & 26 deletions src/commands/build.rs

This file was deleted.

12 changes: 5 additions & 7 deletions src/commands/dev/server_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use protocol::Protocol;
use host::Host;

use anyhow::Result;
use std::net::{SocketAddr, TcpListener};
use std::net::{IpAddr, SocketAddr, TcpListener};

#[derive(Debug, Clone)]
pub struct ServerConfig {
Expand All @@ -16,14 +16,12 @@ pub struct ServerConfig {

impl ServerConfig {
pub fn new(
host: Option<&str>,
ip: Option<&str>,
port: Option<u16>,
host: Option<String>,
ip: IpAddr,
port: u16,
upstream_protocol: Protocol,
) -> Result<Self> {
let ip = ip.unwrap_or("127.0.0.1");
let port = port.unwrap_or(8787);
let addr = format!("{}:{}", ip, port);
let addr = SocketAddr::new(ip, port);
let listening_address = match TcpListener::bind(&addr) {
Ok(socket) => socket.local_addr(),
Err(_) => anyhow::bail!("{} is unavailable, try binding to another address with the --port and --ip flags, or stop other `wrangler dev` processes.", &addr)
Expand Down
18 changes: 14 additions & 4 deletions src/commands/dev/server_config/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::{anyhow, Result};
use std::convert::TryFrom;
use serde::{Deserialize, Serialize};
use std::{convert::TryFrom, str::FromStr};

#[derive(Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Protocol {
Http,
Https,
Expand All @@ -18,10 +20,18 @@ impl Protocol {
}

impl TryFrom<&str> for Protocol {
type Error = anyhow::Error;
type Error = <Self as FromStr>::Err;

fn try_from(p: &str) -> Result<Protocol> {
match p {
p.parse()
}
}

impl FromStr for Protocol {
type Err = anyhow::Error;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"http" => Ok(Protocol::Http),
"https" => Ok(Protocol::Https),
_ => Err(anyhow!("Invalid protocol, must be http or https")),
Expand Down
2 changes: 0 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::process::Command;

pub mod build;
pub mod config;
pub mod dev;
pub mod generate;
Expand All @@ -18,7 +17,6 @@ pub mod whoami;

pub use self::config::global_config;
pub use self::preview::run as preview;
pub use build::build;
pub use dev::dev;
pub use generate::generate;
pub use init::init;
Expand Down
3 changes: 2 additions & 1 deletion src/commands/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use cloudflare::framework::{
endpoint::{Endpoint, Method},
response::ApiFailure,
};
use std::path::Path;

struct ErrorReport(Report);

Expand All @@ -27,7 +28,7 @@ impl Endpoint<(), (), Report> for ErrorReport {
}
}

pub fn run(log: Option<&str>) -> Result<()> {
pub fn run(log: Option<&Path>) -> Result<()> {
let user = settings::global_user::GlobalUser::new()?;
let report = reporter::read_log(log)?;
let client = http::cf_v4_client(&user)?;
Expand Down
9 changes: 4 additions & 5 deletions src/commands/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use cloudflare::framework::apiclient::ApiClient;
use crate::http;
use crate::settings::global_user::GlobalUser;
use crate::terminal::message::{Message, StdOut};
pub fn list(zone_identifier: String, user: &GlobalUser) -> Result<()> {

pub fn list(zone_identifier: &str, user: &GlobalUser) -> Result<()> {
let client = http::cf_v4_client(user)?;

let result = client.request(&ListRoutes {
zone_identifier: &zone_identifier,
});
let result = client.request(&ListRoutes { zone_identifier });

match result {
Ok(success) => {
Expand All @@ -23,7 +22,7 @@ pub fn list(zone_identifier: String, user: &GlobalUser) -> Result<()> {
Ok(())
}

pub fn delete(zone_identifier: String, user: &GlobalUser, route_id: &str) -> Result<()> {
pub fn delete(zone_identifier: &str, user: &GlobalUser, route_id: &str) -> Result<()> {
let client = http::cf_v4_client(user)?;

let result = client.request(&DeleteRoute {
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::settings::global_user::GlobalUser;
use crate::{http, util::TEMP_NOTICE_ES_MODULES_DO_BETA};
use crate::{http, TEMP_NOTICE_ES_MODULES_DO_BETA};

use anyhow::Result;

Expand Down
2 changes: 1 addition & 1 deletion src/deploy/zoneless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::commands::{subdomain::Subdomain, whoami::display_account_id_maybe};
use crate::http;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::RouteConfig;
use crate::util::TEMP_NOTICE_ES_MODULES_DO_BETA;
use crate::TEMP_NOTICE_ES_MODULES_DO_BETA;

use anyhow::Result;

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate text_io;

mod build;
pub mod preview;
pub mod util;
pub use build::build_target;
pub mod commands;
pub mod deploy;
Expand All @@ -25,3 +24,5 @@ pub mod watch;
pub mod wranglerjs;

pub mod fixtures;

const TEMP_NOTICE_ES_MODULES_DO_BETA: &str = "Your account does not have permission to do this! While Durable Objects are in Beta, the modules format is limited to accounts which have opted-in to the Beta. You may do so by following the instructions here: https://developers.cloudflare.com/workers/learning/using-durable-objects";
Loading

0 comments on commit b54d2c8

Please sign in to comment.