Skip to content

Commit

Permalink
refactor(init): improve/simplify CLI experience (#858)
Browse files Browse the repository at this point in the history
Co-authored-by: neo773 <62795688+neo773@users.noreply.github.com>
  • Loading branch information
ologbonowiwi and neo773 committed Jan 6, 2024
1 parent dbe5e64 commit 4e15382
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 48 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ moka = { version = "0.12.2", default-features = false, features = ["future"] }
cache_control = "0.2.0"
nom = "7.1.3"
exitcode = "1.1.2"
resource = "0.5.0"
inquire = "0.6.2"
log = "0.4.20"
env_logger = "0.10.1"
Expand Down
6 changes: 5 additions & 1 deletion src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ pub enum Command {
},

/// Initialize a new project
Init { file_path: String },
Init {
// default is current directory
#[arg(default_value = ".")]
folder_path: String,
},
}
104 changes: 65 additions & 39 deletions src/cli/tc.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::path::Path;
use std::{env, fs};

use anyhow::Result;
use clap::Parser;
use env_logger::Env;
use inquire::Confirm;
use resource::resource_str;
use stripmargin::StripMargin;
use tokio::runtime::Builder;

Expand All @@ -16,6 +16,9 @@ use crate::config::Config;
use crate::http::Server;
use crate::print_schema;

const FILE_NAME: &str = ".tailcallrc.graphql";
const YML_FILE_NAME: &str = ".graphqlrc.yml";

pub fn run() -> Result<()> {
let cli = Cli::parse();

Expand Down Expand Up @@ -50,6 +53,7 @@ pub fn run() -> Result<()> {
Err(e) => Err(e.into()),
}
}
Command::Init { folder_path } => Ok(tokio::runtime::Runtime::new()?.block_on(async { init(&folder_path).await })?),
Command::Compose { file_path, format } => {
let config =
tokio::runtime::Runtime::new()?.block_on(async { Config::read_from_files(file_path.iter()).await })?;
Expand All @@ -58,56 +62,78 @@ pub fn run() -> Result<()> {

Ok(())
}
Command::Init { file_path } => Ok(tokio::runtime::Runtime::new()?.block_on(async { init(&file_path).await })?),
}
}

pub async fn init(file_path: &str) -> Result<()> {
let tailcallrc: resource::Resource<str> = resource_str!("examples/.tailcallrc.graphql");
pub async fn init(folder_path: &str) -> Result<()> {
let folder_exists = fs::metadata(folder_path).is_ok();

if !folder_exists {
let confirm = Confirm::new(&format!("Do you want to create the folder {}?", folder_path))
.with_default(false)
.prompt()?;

if confirm {
fs::create_dir_all(folder_path)?;
};
}

let tailcallrc = include_str!("../../examples/.tailcallrc.graphql");

let file_path = Path::new(folder_path).join(FILE_NAME);
let yml_file_path = Path::new(folder_path).join(YML_FILE_NAME);

let tailcall_exists = fs::metadata(&file_path).is_ok();

if tailcall_exists {
// confirm overwrite
let confirm = Confirm::new(&format!("Do you want to overwrite the file {}?", FILE_NAME))
.with_default(false)
.prompt()?;

if confirm {
fs::write(&file_path, tailcallrc.as_bytes())?;
}
} else {
fs::write(&file_path, tailcallrc.as_bytes())?;
}

let yml_exists = fs::metadata(&yml_file_path).is_ok();

let answer = Confirm::new("Do you want to add a file to the project?")
.with_default(false)
.prompt();
if !yml_exists {
fs::write(&yml_file_path, "")?;

match answer {
Ok(true) => {
let file_name = inquire::Text::new("Enter the file name:")
.with_default(".graphql")
.prompt()
.unwrap_or_else(|_| String::from(".graphql"));
let graphqlrc = r"|schema:
|- './.tailcallrc.graphql'
"
.strip_margin();

fs::write(&yml_file_path, graphqlrc)?;
}

let file_name = format!("{}.graphql", file_name.strip_suffix(".graphql").unwrap_or(&file_name));
let graphqlrc = fs::read_to_string(&yml_file_path)?;

let confirm = Confirm::new(&format!("Do you want to create the file {}?", file_name))
let file_path = file_path.to_str().unwrap();

let mut yaml: serde_yaml::Value = serde_yaml::from_str(&graphqlrc)?;

if let Some(schema) = yaml.get_mut("schema").and_then(|v| v.as_sequence_mut()) {
if !schema
.iter()
.any(|v| v == &serde_yaml::Value::from("./.tailcallrc.graphql"))
{
let confirm = Confirm::new(&format!("Do you want to add {} to the schema?", file_path))
.with_default(false)
.prompt();

match confirm {
Ok(true) => {
fs::write(format!("{}/{}", file_path, &file_name), "")?;

let graphqlrc = format!(
r#"|schema:
|- './{}'
|- './.tailcallrc.graphql'
"#,
&file_name
)
.strip_margin();
fs::write(format!("{}/.graphqlrc.yml", file_path), graphqlrc)?;
}
Ok(false) => (),
Err(e) => return Err(e.into()),
.prompt()?;

if confirm {
schema.push(serde_yaml::Value::from("./.tailcallrc.graphql"));
let updated = serde_yaml::to_string(&yaml)?;
fs::write(yml_file_path, updated)?;
}
}
Ok(false) => (),
Err(e) => return Err(e.into()),
}

fs::write(
format!("{}/.tailcallrc.graphql", file_path),
tailcallrc.as_ref().as_bytes(),
)?;
Ok(())
}

Expand Down

1 comment on commit 4e15382

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 6.35ms 2.67ms 31.66ms 68.74%
Req/Sec 3.97k 183.51 4.32k 92.00%

473726 requests in 30.00s, 2.37GB read

Requests/sec: 15788.67

Transfer/sec: 81.04MB

Please sign in to comment.