-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
202 lines (174 loc) · 5.92 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
mod chart;
mod config;
mod import;
mod set_start;
mod start;
mod summary;
mod tag;
mod untag;
mod tags;
mod time_input;
use augr_core::{
repository::{timesheet::Error as Conflict, Error as RepositoryError, Repository},
store::{SyncFolderStore, SyncFolderStoreError},
};
use snafu::{ErrorCompat, ResultExt, Snafu};
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "augr", about, author)]
struct Opt {
/// Use the config file at the specified path. Defaults to `$XDG_CONFIG_HOME/augr/config.toml`.
#[structopt(long = "config")]
config: Option<PathBuf>,
#[structopt(subcommand)]
cmd: Option<Command>,
}
#[derive(StructOpt, Debug)]
enum Command {
/// Add an event to the timesheet; start defaults to the current time
#[structopt(no_version, name = "start")]
Start(start::StartCmd),
/// Show a table tracked time; defaults to only showing time tracked today
#[structopt(no_version, name = "summary")]
Summary(summary::SummaryCmd),
/// Show an ascii art chart of tracked time
#[structopt(no_version, name = "chart")]
Chart(chart::Cmd),
/// Get a list of all the different tags that have been used.
#[structopt(no_version, name = "tags")]
Tags(tags::TagsCmd),
/// Add tags to an existing event
#[structopt(no_version, name = "tag")]
Tag(tag::Cmd),
/// Remove tags from an existing event
#[structopt(no_version, name = "untag")]
Untag(untag::Cmd),
/// Change when an event started
#[structopt(no_version, name = "set-start")]
SetStart(set_start::Cmd),
/// Import data from version 0.1 of augr
#[structopt(no_version, name = "import")]
Import(import::ImportCmd),
}
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Error getting config: {}", source))]
GetConfig { source: config::Error },
#[snafu(display("Errors reading repository: {:?}", errors))]
ReadRepository {
errors: Vec<RepositoryError<SyncFolderStoreError>>,
},
#[snafu(display("Conflicts while merging patches: {:?}", conflicts))]
MergeConflicts { conflicts: Vec<Conflict> },
#[snafu(display("Error importing data: {}", source))]
ImportError { source: Box<dyn std::error::Error> },
#[snafu(display("Errors synchronizing data: {:?}", errors))]
SyncError {
errors: Vec<RepositoryError<SyncFolderStoreError>>,
},
#[snafu(display("Error: {}", source))]
GeneralError { source: Box<dyn std::error::Error> },
}
fn main() {
match run() {
Ok(()) => {}
Err(e) => {
eprintln!("An error occured: {}", e);
if let Some(backtrace) = ErrorCompat::backtrace(&e) {
eprintln!("{}", backtrace);
}
}
}
}
fn run() -> Result<(), Error> {
let opt = Opt::from_args();
// Load config
let conf_file = match opt.config {
Some(config_path) => config_path,
None => {
let proj_dirs = directories::ProjectDirs::from("xyz", "geemili", "augr").unwrap();
proj_dirs.config_dir().join("config.toml")
}
};
let conf = config::load_config(&conf_file).context(GetConfig {})?;
// Load store for own data
let store = SyncFolderStore::new(conf.sync_folder, conf.device_id).should_init(true);
let mut repo = Repository::from_store(store).unwrap();
// Synchronize data
repo.try_sync_data()
.map_err(|errors| Error::SyncError { errors })?;
repo.save_meta().unwrap();
// Convert abstract patch data structure into a more conventional format
let eventgraph = repo.timesheet();
let timesheet = eventgraph
.flatten()
.map_err(|conflicts| Error::MergeConflicts { conflicts })?;
// Run command
match opt.cmd.unwrap_or_default() {
Command::Start(subcmd) => {
let patches = subcmd.exec(×heet);
for patch in patches {
println!("{}", patch.patch_ref());
repo.add_patch(patch).unwrap();
}
}
Command::Import(subcmd) => {
let patches = subcmd.exec(×heet).context(ImportError {})?;
for patch in patches {
println!("{}", patch.patch_ref());
repo.add_patch(patch).unwrap();
}
}
Command::Summary(subcmd) => subcmd.exec(×heet),
Command::Chart(subcmd) => subcmd.exec(×heet),
Command::Tags(subcmd) => subcmd.exec(×heet),
Command::Tag(subcmd) => {
let patches = subcmd
.exec(×heet)
.map_err(|e| Box::new(e).into())
.context(GeneralError {})?;
for patch in patches {
println!("{}", patch.patch_ref());
repo.add_patch(patch).unwrap();
}
}
Command::Untag(subcmd) => {
let patches = subcmd
.exec(×heet)
.map_err(|e| Box::new(e).into())
.context(GeneralError {})?;
for patch in patches {
println!("{}", patch.patch_ref());
repo.add_patch(patch).unwrap();
}
}
Command::SetStart(subcmd) => {
let patches = subcmd
.exec(×heet)
.map_err(|e| Box::new(e).into())
.context(GeneralError {})?;
for patch in patches {
println!("{}", patch.patch_ref());
repo.add_patch(patch).unwrap();
}
}
};
// Save which patches this device uses to disk
repo.save_meta().unwrap();
Ok(())
}
fn format_duration(duration: chrono::Duration) -> String {
let hours = duration.num_hours();
let mins = duration.num_minutes() - (hours * 60);
if hours < 1 {
format!("{}m", mins)
} else {
format!("{}h {}m", hours, mins)
}
}
impl Default for Command {
fn default() -> Self {
Command::Summary(summary::SummaryCmd::default())
}
}