tracing_modality/common/
options.rs1use auxon_sdk::api::AttrVal;
2use std::net::SocketAddr;
3
4#[derive(Clone)]
6pub struct Options {
7 pub(crate) auth: Option<Vec<u8>>,
8 pub(crate) metadata: Vec<(String, AttrVal)>,
9 pub(crate) server_addr: SocketAddr,
10}
11
12impl Options {
13 pub fn new() -> Options {
14 let auth = Self::resolve_auth_token();
15 let server_addr = ([127, 0, 0, 1], 14182).into();
16 Options {
17 auth,
18 metadata: Vec::new(),
19 server_addr,
20 }
21 }
22
23 fn resolve_auth_token() -> Option<Vec<u8>> {
24 if let Some(from_env) = std::env::var("MODALITY_AUTH_TOKEN")
25 .ok()
26 .and_then(|t| hex::decode(t).ok())
27 {
28 return Some(from_env);
29 }
30
31 dirs::config_dir()
32 .and_then(|config| {
33 let file_path = config.join("modality_cli").join(".user_auth_token");
34 std::fs::read_to_string(file_path).ok()
35 })
36 .and_then(|t| hex::decode(t.trim()).ok())
37 }
38
39 pub fn set_auth<S: AsRef<[u8]>>(&mut self, auth: S) {
41 self.auth = hex::decode(auth).ok();
42 }
43 pub fn with_auth<S: AsRef<[u8]>>(mut self, auth: S) -> Self {
45 self.auth = hex::decode(auth).ok();
46 self
47 }
48
49 pub fn set_name<S: AsRef<str>>(&mut self, name: S) {
52 self.metadata.push((
53 "timeline.name".to_string(),
54 AttrVal::String(name.as_ref().to_string().into()),
55 ));
56 }
57 pub fn with_name<S: AsRef<str>>(mut self, name: S) -> Self {
59 self.metadata.push((
60 "timeline.name".to_string(),
61 AttrVal::String(name.as_ref().to_string().into()),
62 ));
63 self
64 }
65
66 pub fn add_metadata<K: AsRef<str>, V: Into<AttrVal>>(&mut self, key: K, value: V) {
70 let key = key.as_ref();
71 let key = if key.starts_with("timeline.") {
72 key.to_string()
73 } else {
74 format!("timeline.{}", key)
75 };
76
77 self.metadata.push((key, value.into()));
78 }
79 pub fn with_metadata<K: AsRef<str>, V: Into<AttrVal>>(mut self, key: K, value: V) -> Self {
81 let key = key.as_ref();
82 let key = if key.starts_with("timeline.") {
83 key.to_string()
84 } else {
85 format!("timeline.{}", key)
86 };
87
88 self.metadata.push((key, value.into()));
89 self
90 }
91
92 pub fn set_server_address(&mut self, addr: SocketAddr) {
96 self.server_addr = addr;
97 }
98 pub fn with_server_address(mut self, addr: SocketAddr) -> Self {
100 self.server_addr = addr;
101 self
102 }
103}
104
105impl Default for Options {
106 fn default() -> Options {
107 Options::new()
108 }
109}