quartz_cli/action/
cookie.rs

1use crate::{cookie::Cookie, Ctx};
2
3#[derive(clap::Args, Debug)]
4pub struct PrintArgs {
5    key: Option<String>,
6
7    /// Filter cookies that match this domain
8    #[arg(long, short = 'd')]
9    domain: Option<String>,
10}
11
12pub fn print(ctx: &Ctx, args: PrintArgs) {
13    let jar = ctx.require_env().cookie_jar(ctx);
14
15    let iter = jar.iter().filter(|c| {
16        if let Some(domain) = &args.domain {
17            c.domain().matches(domain.as_str())
18        } else {
19            true
20        }
21    });
22
23    if let Some(key) = args.key {
24        let cookies = iter.filter(|c| c.name() == key).collect::<Vec<&Cookie>>();
25
26        match cookies.len() {
27            0 => panic!("{key}: No such cookie"),
28            1 => println!("{}", cookies[0].value()),
29            _ => {
30                for c in cookies {
31                    println!("{}: {}", **c.domain(), c.value());
32                }
33            }
34        }
35    } else {
36        for cookie in iter {
37            println!("{}={}", cookie.name(), cookie.value());
38        }
39    }
40}