twixter

A twtxt command line client in Rust

git clone git://git.shimmy1996.com/twixter.git

tweet.rs (1480B)

    1 use clap::ArgMatches;
    2 
    3 use std::fs::OpenOptions;
    4 use std::io::prelude::*;
    5 use std::path::Path;
    6 use std::process::Command;
    7 
    8 use crate::config::Config;
    9 use crate::entry::Entry;
   10 
   11 /// Helper to run the tweet subcommand.
   12 pub fn tweet(config: &Config, subcommand: &ArgMatches) {
   13     // Parse tweet content.
   14     let content = subcommand
   15         .args
   16         .get("content")
   17         .and_then(|matched_arg| {
   18             Some(
   19                 matched_arg
   20                     .vals
   21                     .iter()
   22                     .map(|os_string| os_string.clone().into_string().unwrap())
   23                     .collect::<Vec<String>>()
   24                     .join(" "),
   25             )
   26         })
   27         .unwrap_or_default();
   28 
   29     if content == "" {
   30         eprintln!("Error: post content must not be empty");
   31     } else {
   32         // Run pre tweet hook.
   33         Command::new("sh")
   34             .args(&["-c", &config.pre_tweet_hook])
   35             .output()
   36             .expect("Failed to run pre tweet hook");
   37 
   38         // Write tweet.
   39         OpenOptions::new()
   40             .append(true)
   41             .create(true)
   42             .open(Path::new(&config.twtfile))
   43             .unwrap()
   44             .write(Entry::new(content, None).to_twtxt().as_bytes())
   45             .expect("Unable to write new post");
   46 
   47         // Run post tweet hook.
   48         Command::new("sh")
   49             .args(&["-c", &config.post_tweet_hook])
   50             .output()
   51             .expect("Failed to run post tweet hook");
   52     }
   53 }