tangled
alpha
login
or
join now
vielle.dev
/
atcities.dev
8
fork
atom
[WIP] A (somewhat barebones) atproto app for creating custom sites without hosting!
8
fork
atom
overview
issues
pulls
pipelines
upload: load cli config
vielle.dev
5 months ago
ed30ee2d
65101683
+114
-2
2 changed files
expand all
collapse all
unified
split
upload
src
config.rs
main.rs
+99
upload/src/config.rs
···
1
1
+
pub enum Id {
2
2
+
Handle(String),
3
3
+
Did(String),
4
4
+
}
5
5
+
6
6
+
pub struct Config {
7
7
+
pub user: Id,
8
8
+
pub pword: String,
9
9
+
pub dir: std::path::PathBuf,
10
10
+
}
11
11
+
12
12
+
pub enum ConfigErr {
13
13
+
TooManyDirectories,
14
14
+
NotEnoughArgs(String),
15
15
+
UnknownArg(String),
16
16
+
NotADirectory,
17
17
+
}
18
18
+
19
19
+
pub fn extract() -> Result<Config, ConfigErr> {
20
20
+
let mut user: Option<String> = None;
21
21
+
let mut pword: Option<String> = None;
22
22
+
let mut dir: Option<String> = None;
23
23
+
24
24
+
let mut args = std::env::args().into_iter();
25
25
+
// skip first arg
26
26
+
args.next();
27
27
+
while let Some(arg) = args.next() {
28
28
+
match arg.as_str() {
29
29
+
"--user" => user = args.next(),
30
30
+
"--password" => pword = args.next(),
31
31
+
_ => {
32
32
+
if arg.starts_with("--") {
33
33
+
return Err(ConfigErr::UnknownArg(arg));
34
34
+
}
35
35
+
if dir.is_some() {
36
36
+
return Err(ConfigErr::TooManyDirectories);
37
37
+
}
38
38
+
dir = Some(arg);
39
39
+
}
40
40
+
}
41
41
+
}
42
42
+
43
43
+
if let Some(user) = user.clone()
44
44
+
&& let Some(pword) = pword.clone()
45
45
+
&& let Some(dir) = dir.clone()
46
46
+
{
47
47
+
// we do not validate that the did or handle is valid rn
48
48
+
// but if it contains a : then its definitely a did
49
49
+
let user = if user.contains(":") {
50
50
+
Id::Did(user)
51
51
+
} else {
52
52
+
Id::Handle(user)
53
53
+
};
54
54
+
55
55
+
let dir = std::path::Path::new(&dir);
56
56
+
if !dir.is_dir() {
57
57
+
return Err(ConfigErr::NotADirectory);
58
58
+
};
59
59
+
60
60
+
return Ok(Config {
61
61
+
user,
62
62
+
pword,
63
63
+
dir: dir.to_owned(),
64
64
+
});
65
65
+
}
66
66
+
67
67
+
return Err(ConfigErr::NotEnoughArgs(String::from(
68
68
+
// xxx
69
69
+
if user.is_none() && pword.is_none() && dir.is_none() {
70
70
+
"--user, --password, and <dir>"
71
71
+
}
72
72
+
// yxx
73
73
+
else if user.is_some() && pword.is_none() && dir.is_none() {
74
74
+
"--password and <dir>"
75
75
+
}
76
76
+
// xyx
77
77
+
else if user.is_none() && pword.is_some() && dir.is_none() {
78
78
+
"--user and <dir>"
79
79
+
}
80
80
+
// yyx
81
81
+
else if user.is_some() && pword.is_some() && dir.is_none() {
82
82
+
"<dir>"
83
83
+
}
84
84
+
// xxy
85
85
+
else if user.is_none() && pword.is_none() && dir.is_some() {
86
86
+
"--user and --password"
87
87
+
}
88
88
+
// yxy
89
89
+
else if user.is_some() && pword.is_none() && dir.is_some() {
90
90
+
"--password"
91
91
+
}
92
92
+
// xyy
93
93
+
else if user.is_none() && pword.is_some() && dir.is_some() {
94
94
+
"--user"
95
95
+
} else {
96
96
+
"Invalid State, as all values are provided here"
97
97
+
},
98
98
+
)));
99
99
+
}
+15
-2
upload/src/main.rs
···
1
1
-
fn main() {
2
2
-
println!("Hello, World!")
1
1
+
mod config;
2
2
+
3
3
+
fn main() -> Result<(), ()> {
4
4
+
let config = config::extract();
5
5
+
if let Err(err) = config {
6
6
+
match err {
7
7
+
config::ConfigErr::TooManyDirectories => println!("Too many directories"),
8
8
+
config::ConfigErr::NotEnoughArgs(args) => println!("Missing {}", args),
9
9
+
config::ConfigErr::UnknownArg(arg) => println!("Unknown arg {}", arg),
10
10
+
config::ConfigErr::NotADirectory => println!("Not a directory"),
11
11
+
}
12
12
+
return Err(());
13
13
+
}
14
14
+
15
15
+
Ok(())
3
16
}