cli: add --sec argument to quickly add an account
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
133
src/app.rs
133
src/app.rs
@@ -638,44 +638,74 @@ fn render_damus(damus: &mut Damus, ctx: &Context) {
|
|||||||
puffin_egui::profiler_window(ctx);
|
puffin_egui::profiler_window(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Damus {
|
struct Args {
|
||||||
/// Called once before the first frame.
|
timelines: Vec<Timeline>,
|
||||||
pub fn new<P: AsRef<Path>>(
|
is_mobile: Option<bool>,
|
||||||
cc: &eframe::CreationContext<'_>,
|
secret_key: Option<SecretKey>,
|
||||||
data_path: P,
|
light: bool,
|
||||||
args: Vec<String>,
|
}
|
||||||
) -> Self {
|
|
||||||
let mut timelines: Vec<Timeline> = vec![];
|
|
||||||
let mut is_mobile: Option<bool> = None;
|
|
||||||
let mut i = 0;
|
|
||||||
let mut light: bool = false;
|
|
||||||
|
|
||||||
if args.len() > 1 {
|
fn parse_args(args: &[String]) -> Args {
|
||||||
for arg in &args[1..] {
|
let mut res = Args {
|
||||||
if arg == "--mobile" {
|
timelines: vec![],
|
||||||
is_mobile = Some(true);
|
is_mobile: None,
|
||||||
} else if arg == "--light" {
|
secret_key: None,
|
||||||
light = true;
|
light: false,
|
||||||
} else if arg == "--dark" {
|
};
|
||||||
light = false;
|
|
||||||
} else if arg == "--filter" {
|
if args.len() <= 1 {
|
||||||
let next_args = &args[1 + i + 1..];
|
let filter = serde_json::from_str(include_str!("../queries/timeline.json")).unwrap();
|
||||||
if next_args.is_empty() {
|
res.timelines.push(Timeline::new(filter));
|
||||||
continue;
|
return res;
|
||||||
}
|
}
|
||||||
let filter = &next_args[0];
|
|
||||||
|
let mut i = 0;
|
||||||
|
let len = args.len();
|
||||||
|
while i < len {
|
||||||
|
let arg = &args[i];
|
||||||
|
|
||||||
|
if arg == "--mobile" {
|
||||||
|
res.is_mobile = Some(true);
|
||||||
|
} else if arg == "--light" {
|
||||||
|
res.light = true;
|
||||||
|
} else if arg == "--dark" {
|
||||||
|
res.light = false;
|
||||||
|
} else if arg == "--sec" {
|
||||||
|
i += 1;
|
||||||
|
let secstr = if let Some(next_arg) = args.get(i) {
|
||||||
|
next_arg
|
||||||
|
} else {
|
||||||
|
error!("sec argument missing?");
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
res.secret_key = SecretKey::parse(secstr).ok();
|
||||||
|
|
||||||
|
if res.secret_key.is_none() {
|
||||||
|
error!("failed to parse --sec argument. Make sure to use hex or nsec.");
|
||||||
|
}
|
||||||
|
} else if arg == "--filter" {
|
||||||
|
i += 1;
|
||||||
|
let filter = if let Some(next_arg) = args.get(i) {
|
||||||
|
next_arg
|
||||||
|
} else {
|
||||||
|
error!("filter argument missing?");
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
if let Ok(filter) = serde_json::from_str(filter) {
|
if let Ok(filter) = serde_json::from_str(filter) {
|
||||||
timelines.push(Timeline::new(filter));
|
res.timelines.push(Timeline::new(filter));
|
||||||
} else {
|
} else {
|
||||||
error!("failed to parse filter '{}'", filter);
|
error!("failed to parse filter '{}'", filter);
|
||||||
}
|
}
|
||||||
} else if arg == "--filter-file" || arg == "-f" {
|
} else if arg == "--filter-file" || arg == "-f" {
|
||||||
let next_args = &args[1 + i + 1..];
|
i += 1;
|
||||||
if next_args.is_empty() {
|
let filter_file = if let Some(next_arg) = args.get(i) {
|
||||||
|
next_arg
|
||||||
|
} else {
|
||||||
|
error!("filter file argument missing?");
|
||||||
continue;
|
continue;
|
||||||
}
|
};
|
||||||
let filter_file = &next_args[0];
|
|
||||||
|
|
||||||
let data = if let Ok(data) = std::fs::read(filter_file) {
|
let data = if let Ok(data) = std::fs::read(filter_file) {
|
||||||
data
|
data
|
||||||
@@ -685,7 +715,7 @@ impl Damus {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(filter) = serde_json::from_slice(&data) {
|
if let Ok(filter) = serde_json::from_slice(&data) {
|
||||||
timelines.push(Timeline::new(filter));
|
res.timelines.push(Timeline::new(filter));
|
||||||
} else {
|
} else {
|
||||||
error!("failed to parse filter in '{}'", filter_file);
|
error!("failed to parse filter in '{}'", filter_file);
|
||||||
}
|
}
|
||||||
@@ -693,14 +723,23 @@ impl Damus {
|
|||||||
|
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let filter = serde_json::from_str(include_str!("../queries/timeline.json")).unwrap();
|
|
||||||
timelines.push(Timeline::new(filter));
|
|
||||||
};
|
|
||||||
|
|
||||||
let is_mobile = is_mobile.unwrap_or(ui::is_compiled_as_mobile());
|
res
|
||||||
|
}
|
||||||
|
|
||||||
setup_cc(cc, is_mobile, light);
|
impl Damus {
|
||||||
|
/// Called once before the first frame.
|
||||||
|
pub fn new<P: AsRef<Path>>(
|
||||||
|
cc: &eframe::CreationContext<'_>,
|
||||||
|
data_path: P,
|
||||||
|
args: Vec<String>,
|
||||||
|
) -> Self {
|
||||||
|
// arg parsing
|
||||||
|
let parsed_args = parse_args(&args);
|
||||||
|
|
||||||
|
let is_mobile = parsed_args.is_mobile.unwrap_or(ui::is_compiled_as_mobile());
|
||||||
|
|
||||||
|
setup_cc(cc, is_mobile, parsed_args.light);
|
||||||
|
|
||||||
let imgcache_dir = data_path.as_ref().join(ImageCache::rel_datadir());
|
let imgcache_dir = data_path.as_ref().join(ImageCache::rel_datadir());
|
||||||
let _ = std::fs::create_dir_all(imgcache_dir.clone());
|
let _ = std::fs::create_dir_all(imgcache_dir.clone());
|
||||||
@@ -708,6 +747,19 @@ impl Damus {
|
|||||||
let mut config = Config::new();
|
let mut config = Config::new();
|
||||||
config.set_ingester_threads(2);
|
config.set_ingester_threads(2);
|
||||||
|
|
||||||
|
let mut account_manager = AccountManager::new(
|
||||||
|
// TODO: should pull this from settings
|
||||||
|
None,
|
||||||
|
// TODO: use correct KeyStorage mechanism for current OS arch
|
||||||
|
crate::key_storage::KeyStorage::None,
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some(secret) = parsed_args.secret_key {
|
||||||
|
let keypair = enostr::Keypair::from_secret(secret);
|
||||||
|
info!("adding account: {}", keypair.pubkey);
|
||||||
|
account_manager.add_account(keypair);
|
||||||
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
is_mobile,
|
is_mobile,
|
||||||
drafts: Drafts::default(),
|
drafts: Drafts::default(),
|
||||||
@@ -716,15 +768,10 @@ impl Damus {
|
|||||||
img_cache: ImageCache::new(imgcache_dir),
|
img_cache: ImageCache::new(imgcache_dir),
|
||||||
note_cache: NoteCache::default(),
|
note_cache: NoteCache::default(),
|
||||||
selected_timeline: 0,
|
selected_timeline: 0,
|
||||||
timelines,
|
timelines: parsed_args.timelines,
|
||||||
textmode: false,
|
textmode: false,
|
||||||
ndb: Ndb::new(data_path.as_ref().to_str().expect("db path ok"), &config).expect("ndb"),
|
ndb: Ndb::new(data_path.as_ref().to_str().expect("db path ok"), &config).expect("ndb"),
|
||||||
account_manager: AccountManager::new(
|
account_manager,
|
||||||
// TODO: should pull this from settings
|
|
||||||
None,
|
|
||||||
// TODO: use correct KeyStorage mechanism for current OS arch
|
|
||||||
crate::key_storage::KeyStorage::None,
|
|
||||||
),
|
|
||||||
//compose: "".to_string(),
|
//compose: "".to_string(),
|
||||||
frame_history: FrameHistory::default(),
|
frame_history: FrameHistory::default(),
|
||||||
show_account_switcher: false,
|
show_account_switcher: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user