commit 19cdd92b7978b60864c5d353050560053cb74371
parent f44c34551a230381922103955e8fca5e3871afe8
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Sat, 14 Sep 2019 23:44:24 -0400
Move tweet to separate module
Diffstat:
M | src/main.rs | | | 63 | ++------------------------------------------------------------- |
A | src/tweet.rs | | | 64 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 66 insertions(+), 61 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -1,11 +1,9 @@
use clap::{crate_version, App, Arg, ArgMatches, SubCommand};
-use std::fs::OpenOptions;
-use std::io::prelude::*;
use std::path::Path;
-use std::process::Command;
mod config;
+mod tweet;
use crate::config::Config;
fn main() {
@@ -68,64 +66,7 @@ fn main() {
// Parse subcommands.
match command.subcommand() {
- ("tweet", Some(subcommand)) => tweet(&config, &subcommand),
+ ("tweet", Some(subcommand)) => tweet::tweet(&config, &subcommand),
_ => {}
}
}
-
-fn tweet(config: &Config, subcommand: &ArgMatches) {
- // Helper to run the tweet subcommand.
-
- // Parse tweet content.
- let content = subcommand
- .args
- .get("content")
- .and_then(|matched_arg| {
- Some(
- matched_arg
- .vals
- .iter()
- .map(|os_string| os_string.clone().into_string().unwrap())
- .collect::<Vec<String>>()
- .join(" "),
- )
- })
- .unwrap_or_default();
-
- if content == "" {
- eprintln!("Error: post content must not be empty");
- } else {
- // Run pre tweet hook.
- Command::new("sh")
- .args(&["-c", &config.pre_tweet_hook])
- .output()
- .expect("Failed to run pre tweet hook");
-
- // Write tweet.
- OpenOptions::new()
- .append(true)
- .create(true)
- .open(Path::new(&config.twtfile))
- .unwrap()
- .write(compose(content).as_bytes())
- .expect("Unable to write new post");
-
- // Run post tweet hook.
- Command::new("sh")
- .args(&["-c", &config.post_tweet_hook])
- .output()
- .expect("Failed to run post tweet hook");
- }
-}
-
-fn compose(content: String) -> String {
- // Formats given content into twtxt format by adding datetime.
- use chrono::{Local, SecondsFormat};
- let timestamp = Local::now().to_rfc3339_opts(SecondsFormat::Secs, true);
- let mut post = String::new();
- post.push_str(×tamp);
- post.push('\t');
- post.push_str(&content);
- post.push('\n');
- post
-}
diff --git a/src/tweet.rs b/src/tweet.rs
@@ -0,0 +1,64 @@
+use clap::ArgMatches;
+
+use std::fs::OpenOptions;
+use std::io::prelude::*;
+use std::path::Path;
+use std::process::Command;
+
+use crate::config::Config;
+
+/// Helper to run the tweet subcommand.
+pub fn tweet(config: &Config, subcommand: &ArgMatches) {
+ // Parse tweet content.
+ let content = subcommand
+ .args
+ .get("content")
+ .and_then(|matched_arg| {
+ Some(
+ matched_arg
+ .vals
+ .iter()
+ .map(|os_string| os_string.clone().into_string().unwrap())
+ .collect::<Vec<String>>()
+ .join(" "),
+ )
+ })
+ .unwrap_or_default();
+
+ if content == "" {
+ eprintln!("Error: post content must not be empty");
+ } else {
+ // Run pre tweet hook.
+ Command::new("sh")
+ .args(&["-c", &config.pre_tweet_hook])
+ .output()
+ .expect("Failed to run pre tweet hook");
+
+ // Write tweet.
+ OpenOptions::new()
+ .append(true)
+ .create(true)
+ .open(Path::new(&config.twtfile))
+ .unwrap()
+ .write(compose(content).as_bytes())
+ .expect("Unable to write new post");
+
+ // Run post tweet hook.
+ Command::new("sh")
+ .args(&["-c", &config.post_tweet_hook])
+ .output()
+ .expect("Failed to run post tweet hook");
+ }
+}
+
+/// Formats given content into twtxt format by adding datetime.
+fn compose(content: String) -> String {
+ use chrono::{Local, SecondsFormat};
+ let timestamp = Local::now().to_rfc3339_opts(SecondsFormat::Secs, true);
+ let mut post = String::new();
+ post.push_str(×tamp);
+ post.push('\t');
+ post.push_str(&content);
+ post.push('\n');
+ post
+}