Compare commits

...

12 Commits

34
Cargo.lock generated

@ -332,6 +332,23 @@ dependencies = [
"phf_codegen", "phf_codegen",
] ]
[[package]]
name = "chrono_track"
version = "0.1.0"
dependencies = [
"chrono",
"chrono-tz",
"clap",
"csv",
"lettre",
"log",
"reqwest",
"serde",
"serde_json",
"simplelog",
"toml",
]
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.4.2" version = "4.4.2"
@ -1555,23 +1572,6 @@ dependencies = [
"time-core", "time-core",
] ]
[[package]]
name = "time_tracker"
version = "0.1.0"
dependencies = [
"chrono",
"chrono-tz",
"clap",
"csv",
"lettre",
"log",
"reqwest",
"serde",
"serde_json",
"simplelog",
"toml",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"

@ -1,5 +1,5 @@
[workspace] [workspace]
members = [ members = [
"status_cloud", "status_cloud",
"time_tracker" "chrono_track"
] ]

@ -1,5 +1,5 @@
[package] [package]
name = "time_tracker" name = "chrono_track"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

@ -2,7 +2,7 @@ use chrono::{DateTime, Datelike, Utc};
use chrono_tz::US::Central; use chrono_tz::US::Central;
use clap::Parser; use clap::Parser;
use lettre::{ use lettre::{
message::{Message, header::ContentType, Attachment, MultiPart, SinglePart}, message::{header::ContentType, Attachment, Message, MultiPart, SinglePart},
SendmailTransport, Transport, SendmailTransport, Transport,
}; };
use log::{debug, error}; use log::{debug, error};
@ -196,7 +196,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if do_email_summary { if do_email_summary {
debug!("Send email selected"); debug!("Send email selected");
reset_email_checkbox(&cfg); reset_email_checkbox(&cfg);
send_email_summary(&cfg, body_content); send_email_summary(&cfg, body_content, &args);
} }
Ok(()) Ok(())
@ -236,6 +236,9 @@ struct CliArgs {
#[arg(short, long)] #[arg(short, long)]
config_file: PathBuf, config_file: PathBuf,
#[arg(short, long)]
from_addr: String,
#[arg(short, long)] #[arg(short, long)]
debug: bool, debug: bool,
} }
@ -299,14 +302,14 @@ struct SummaryRow {
// All calls after depend on the success of the one before, and cannot run // All calls after depend on the success of the one before, and cannot run
// without. So it would be too much work to make an error type to handle an error // without. So it would be too much work to make an error type to handle an error
// that would cause a crash anyways // that would cause a crash anyways
fn send_email_summary(config: &Config, body_content: Vec<String>) { fn send_email_summary(config: &Config, body_content: Vec<String>, cliargs: &CliArgs) {
let mut wtr = csv::Writer::from_writer(vec![]); let mut wtr = csv::Writer::from_writer(vec![]);
for line in body_content { for line in body_content {
if line.starts_with("### ") { if line.starts_with("### ") {
let mut split: Vec<&str> = line.split('|').collect(); let mut split: Vec<&str> = line.split('|').collect();
if split.len() != 3 { if split.len() != 3 {
error!("There was an issue with this line: {line}"); error!("There was an issue with this line. 3 splits not found: {line}");
continue; continue;
} }
let date: String = match split.pop() { let date: String = match split.pop() {
@ -355,33 +358,97 @@ fn send_email_summary(config: &Config, body_content: Vec<String>) {
.to_string() .to_string()
.replace("### ", ""); .replace("### ", "");
wtr.serialize(SummaryRow { date: date.trim().to_string(), total_time: time, task_name }).unwrap(); match wtr.serialize(SummaryRow {
date: date.trim().to_string(),
total_time: time,
task_name,
}) {
Ok(_) => continue,
Err(e) => {
error!("There was an error serializing the csv, aborting: {e}");
return;
}
};
} }
} }
let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap(); let data = match String::from_utf8(wtr.into_inner().unwrap()) {
Ok(val) => val,
Err(e) => {
error!("There was an error converting the csv writer to a string, aborting: {e}");
return;
}
};
debug!("{:#?}", data); debug!("{:#?}", data);
let attachment = Attachment::new("TimeSummary.csv".to_string()).body(data, ContentType::parse("text/csv").unwrap()); let attachment = Attachment::new("TimeSummary.csv".to_string())
// The unwrap is on a constant value
.body(data, ContentType::parse("text/csv").unwrap());
const HTML: &str = r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChronoTrack Export</title>
</head>
<body>
<div style="background-color: #33ccff; border-radius: 40px; height: 100px;">
<div style="margin-left: auto; margin-right: auto;width: fit-content">
<img src="https://cdn-icons-png.flaticon.com/512/3938/3938540.png"
style="height:100px; border-radius: 50%; background-color: whitesmoke;">
</div>
</div>
<div>
<h2>Prepared with care, and sent through the horrors of the interwebs, ChronoTrack presents! (see attached)</h2>
</div>
</body>
</html>
"#;
let email = Message::builder() let email = Message::builder()
.from("NoReplay <noreply@nickiel.net>".parse().unwrap()) .from(match cliargs.from_addr.parse() {
.to("Nicholas Young <nicholasyoungsumner@gmail.com>".parse().unwrap()) Ok(val) => val,
.subject("Testing email") Err(e) => {
.header(ContentType::TEXT_PLAIN) error!("The provided from_email address was unparsable:\n{e}");
std::process::exit(1);
}
})
.to(match config.email_addr.parse() {
Ok(val) => val,
Err(e) => {
error!(
"The provided destination email in the configuration file was unparsable:\n{e}"
);
std::process::exit(1);
}
})
.subject("ChronoTrack Export")
.multipart( .multipart(
MultiPart::mixed() MultiPart::mixed()
.singlepart(SinglePart::plain("Hello World".to_string())) .multipart(
.singlepart(attachment) MultiPart::alternative()
).unwrap(); .singlepart(
SinglePart::builder()
.header(ContentType::TEXT_PLAIN)
.body(String::from("This is an automated email from ChronoTrack")),
)
.singlepart(
SinglePart::builder()
.header(ContentType::TEXT_HTML)
.body(String::from(HTML)),
),
)
.singlepart(attachment),
)
.unwrap();
let mailer = SendmailTransport::new(); let mailer = SendmailTransport::new();
match mailer.send(&email) { match mailer.send(&email) {
Ok(val) => debug!("email sent: {:?}", val), Ok(val) => debug!("email sent: {:?}", val),
Err(e) => error!("Couldn't send email {}", e) Err(e) => error!("Couldn't send email {}", e),
}; };
} }

@ -37,7 +37,8 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
rustSettings = with pkgs; { rustSettings = with pkgs; {
src = ./.; src = ./.;
nativeBuildInputs = [ pkg-config ]; nativeBuildInputs = [ pkg-config ];
buildInputs = [ openssl hddtemp msmtp ]; # move makeWrapper to nativeBuildInputs
buildInputs = [ openssl hddtemp msmtp makeWrapper ];
cargoHash = nixpkgs.lib.fakeHash; cargoHash = nixpkgs.lib.fakeHash;
}; };
meta = with nixpkgs.lib; { meta = with nixpkgs.lib; {
@ -61,26 +62,27 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
pname = "status_cloud"; pname = "status_cloud";
version = "0.1.0"; version = "0.1.0";
buildAndTestSubdir = "status_cloud"; buildAndTestSubdir = "status_cloud";
cargoHash = "sha256-XIonh2SQ8EichcZvHErSydt0qtKGodXE0yKz4BsntA8="; cargoHash = "sha256-mXcD/92WJLgN2efX/8EiV3rOqUiNOKGC4puS8DvGFOc=";
preBuild = '' postFixup = ''
sed -i 's/Command::new("hddtemp")/Command::new("${nixpkgs.lib.escape [ "/" ] "${pkgs.hddtemp}"}")/g' status_cloud/src/main.rs wrapProgram $out/bin/status_cloud \
--prefix PATH : "${nixpkgs.lib.makeBinPath [ pkgs.hddtemp ]}"
''; '';
meta = meta // { meta = meta // {
description = "Server status saved to a nextcloud note service."; description = "Server status saved to a nextcloud note service.";
}; };
}); });
time_tracker = pkgs.rustPlatform.buildRustPackage (rustSettings // { chrono_track = pkgs.rustPlatform.buildRustPackage (rustSettings // {
pname = "time_tracker"; pname = "chrono_track";
version = "0.1.0"; version = "0.1.0";
buildAndTestSubdir = "time_tracker"; buildAndTestSubdir = "chrono_track";
cargoHash = "sha256-5Wy2+ef2BriKmvNhllDVh/clAZGV7myDc281ygj05vI="; cargoHash = "sha256-51j5eG2ZBJg2TjbvYSEvvmOM4hhPyNohN+LUmvck88k=";
meta = meta // { meta = meta // {
description = "Using nextcloud notes to track time usage."; description = "Using nextcloud notes to track time usage.";
}; };
postFixup = '' postFixup = ''
wrapProgram $out/bin/time_tracker \ wrapProgram $out/bin/chrono_track \
--prefix PATH : "${lib.makeBinPath [ msmtp ]}" --prefix PATH : "${nixpkgs.lib.makeBinPath [ pkgs.msmtp ]}"
''; '';
}); });
}; };
@ -137,14 +139,14 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
# Time Tracker # Time Tracker
options.services.time_tracker = { options.services.chrono_track = {
enable = lib.mkEnableOption (lib.mdDoc "time tracker service"); enable = lib.mkEnableOption (lib.mdDoc "chrono track, time tracking service");
package = lib.mkOption { package = lib.mkOption {
type = lib.types.package; type = lib.types.package;
default = self.packages.${system}.time_tracker; default = self.packages.${system}.chrono_track;
defaultText = "pkgs.time_tracker"; defaultText = "pkgs.chrono_track";
description = lib.mdDoc '' description = lib.mdDoc ''
The time_tracker package that should be used The chrono_track package that should be used
''; '';
}; };
config_path = lib.mkOption { config_path = lib.mkOption {
@ -153,6 +155,12 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
The file path to the toml that contains user information secrets The file path to the toml that contains user information secrets
''; '';
}; };
from_address = lib.mkOption {
type = lib.types.str;
description = lib.mdDoc ''
The from address for the emails. E.g. noreply@example.com
'';
};
frequency = lib.mkOption { frequency = lib.mkOption {
type = lib.types.int; type = lib.types.int;
description = lib.mdDoc '' description = lib.mdDoc ''
@ -161,25 +169,25 @@ rust-project TODO: write shell script for automatically updating `cargoHash`
}; };
}; };
config.systemd.services.time_tracker = let config.systemd.services.chrono_track = let
cfg = config.services.time_tracker; cfg = config.services.chrono_track;
pkg = self.packages.${system}.time_tracker; pkg = self.packages.${system}.chrono_track;
in lib.mkIf cfg.enable { in lib.mkIf cfg.enable {
description = "Nextcloud Time Tracker"; description = "Chrono Track";
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
ExecStart = '' ExecStart = ''
${cfg.package}/bin/time_tracker --config-file ${builtins.toString cfg.config_path} ${cfg.package}/bin/chrono_track --config-file ${builtins.toString cfg.config_path} --from-addr ${cfg.from_address}
''; '';
}; };
}; };
config.systemd.timers.time_tracker = let config.systemd.timers.chrono_track = let
cfg = config.services.time_tracker; cfg = config.services.chrono_track;
pkg = self.packages.${system}.time_tracker; pkg = self.packages.${system}.chrono_track;
in lib.mkIf cfg.enable { in lib.mkIf cfg.enable {
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
partOf = [ "time_tracker.service" ]; partOf = [ "chrono_track.service" ];
timerConfig.OnCalendar = [ "*:0/${builtins.toString cfg.frequency}" ]; timerConfig.OnCalendar = [ "*:0/${builtins.toString cfg.frequency}" ];
}; };

Loading…
Cancel
Save