Skip to content

Commit

Permalink
Merge pull request #11 from opeolluwa/store
Browse files Browse the repository at this point in the history
Store
  • Loading branch information
opeolluwa authored Aug 26, 2023
2 parents b0ba24c + 3be08e7 commit 8d067d5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 26 deletions.
40 changes: 37 additions & 3 deletions src/commands/email.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::{thread, time::Duration};
use std::time::Duration;

use clap::{Args, Subcommand};
use dialoguer::Confirm;
use indicatif::{ProgressBar, ProgressStyle};
use serde::{Deserialize, Serialize};

use crate::database::Database;
use crate::style::PrintColoredText;
use lettre::message::header::ContentType;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};

#[derive(clap::Args, Debug, Serialize)]
pub struct EmailCommands {
Expand Down Expand Up @@ -69,6 +73,8 @@ impl EmailCommands {
}

fn list(&self) {
let conn = Database::conn();

println!("email listed");
}

Expand All @@ -82,6 +88,11 @@ impl EmailCommands {
}

fn send(&self, data: &SendOptions) {
//TODO get the email credentials from the database

//TODO throw error if not found

// if found use the template to send email
let prompt = format!("Proceed to send email to {email}?", email = data.email);
if Confirm::new()
.with_prompt(prompt)
Expand All @@ -98,8 +109,31 @@ impl EmailCommands {
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏", "✓"]),
);
pb.set_message("Please wait...");
thread::sleep(Duration::from_secs(5));
pb.finish_with_message("Email successfully sent");
// send the email
let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!"))
.unwrap();

let creds = Credentials::new("smtp_username".to_owned(), "smtp_password".to_owned());

// Open a remote connection to gmail
let mailer = SmtpTransport::relay("smtp.gmail.com")
.unwrap()
.credentials(creds)
.build();

// Send the email
match mailer.send(&email) {
//TODO Save the email if it saves successfully
Ok(_) => pb.finish_with_message("Email successfully sent"),
Err(e) => panic!("Could not send email: {e:?}"),
}
// thread::sleep(Duration::from_secs(5));
} else {
PrintColoredText::warning("termination...")
}
Expand Down
57 changes: 42 additions & 15 deletions src/commands/store.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};

use crate::{database::Database, style::PrintColoredText};

// utils store k v
#[derive(Args, Debug, Serialize)]
pub struct StoreCommands {
/// a unique key
#[clap(short, long, value_parser)]
pub key: String,
///' value
#[clap(short, long, value_parser)]
pub value: String,
/// sub commands
#[command(subcommand)]
pub subcommands: Option<StoreSubCommand>,
pub subcommands: StoreSubCommand,
}

#[derive(Debug, Subcommand, Serialize, Deserialize, Clone)]
pub enum StoreSubCommand {
Set,
Add,
Replace,
Remove,
/// update value
Set { key: String, value: String },
/// save new value
Add {
/// a unique key
#[clap(short, long, value_parser)]
key: String,
///' value
#[clap(short, long, value_parser)]
value: String,
},
/// remove value
Remove { key: String },
}

impl StoreCommands {
pub fn parse(&self) {
match &self.subcommands {
Some(StoreSubCommand::Set) => println!("set"),
_ => println!("default"),
// Some()
StoreSubCommand::Set { key, value } => println!("set {} {}", key, value),
StoreSubCommand::Remove { key } => println!("remove"),
StoreSubCommand::Add { key, value } => println!("add"),
_ => PrintColoredText::warning("invalid input"),
}
println!("{:?}", self)
; }
}

/*store the key value pair in the database after checking that the key does not exist, if the key exist prompt use to overwrite */
fn add(key: &String, value: &String) {
let conn = Database::conn();
let query = format!("SELECT * FROM store WHERE key = '{}'", key);
let mut stmt = conn.prepare(&query).unwrap();
// let mut rows = stmt.query([]).unwrap();
}
/* accept a key and update the value of the key */
fn set(key: &String, value: &String) {
let conn = Database::conn();
let query = format!("SELECT * FROM store WHERE key = '{}'", key);
let mut stmt = conn.prepare(&query).unwrap();
}

fn remove(key: &String) {
let conn = Database::conn();
let query = format!("SELECT * FROM store WHERE key = '{}'", key);
let mut stmt = conn.prepare(&query).unwrap();
}
}
1 change: 1 addition & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sqlite::Connection;
// }

pub struct Database;
#[allow(unused)]

impl Database {
pub fn init() -> Connection {
Expand Down
15 changes: 8 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ impl Utils {

match utils.command {
Commands::GitIgnore(git_ignore) => git_ignore.parse(),
Commands::Email(email) => email.parse(),
Commands::Mailto(email) => email.parse(),
Commands::Readme(readme) => readme.parse(),
_ => panic!(),
Commands::Store(store) => store.parse(),
_ => println!("not implemented"),
}
}
}
Expand All @@ -32,13 +33,13 @@ pub enum Commands {
/// download files, videos, etc
Download(DownloadCommands),
/// send email from the command line
Email(EmailCommands),
/// generate project readmes
Mailto(EmailCommands),
/// add readme to a git software project
Readme(ReadmeCommands),
///send SMS
///send SMS to people from the command line
Sms(SmsCommands),
/// include .gitignore
/// include .gitignore in a git repo
GitIgnore(GitIgnoreCommands),
/// store values
/// store data in the database
Store(StoreCommands),
}
2 changes: 1 addition & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use console::Style;

pub struct PrintColoredText;
#[allow(unused)]
/// return colored text to stdout or std err
impl PrintColoredText {
pub fn error(message: &str) {
let error_style = Style::new().for_stderr().red();
println!("{}", error_style.apply_to(message));
}

pub fn success(message: &str) {
let error_style = Style::new().for_stderr().green();
println!("{}", error_style.apply_to(message));
Expand Down

0 comments on commit 8d067d5

Please sign in to comment.