tangled
alpha
login
or
join now
vielle.dev
/
tangled-on-commit
5
fork
atom
Listen to git commits for a specific repo and run a shell command
5
fork
atom
overview
issues
pulls
pipelines
Extract cli arguments
vielle.dev
6 months ago
145bae1c
81487e13
verified
This commit was signed with the committer's
known signature
.
vielle.dev
SSH Key Fingerprint:
SHA256:/4bvxqoEh9iMdjAPgcgAgXKZZQTROL3ULiPt6nH9RSs=
+68
-4
2 changed files
expand all
collapse all
unified
split
Cargo.toml
src
main.rs
+1
Cargo.toml
···
3
3
version = "0.1.0"
4
4
edition = "2024"
5
5
license = "GPL-3.0-or-later"
6
6
+
rust-version = "1.88"
6
7
7
8
[dependencies]
+67
-4
src/main.rs
···
23
23
}
24
24
}
25
25
26
26
+
// if 0 args are passed, skip
27
27
+
// if 1 arg is passed, its the shell command
28
28
+
// if 2 args are passed:
29
29
+
// if arg 1 starts in @ its a handle
30
30
+
// if it contains a / the contents after the slash is the reponame
31
31
+
// else its the reponame
32
32
+
// arg 2 is always the shell command
33
33
+
// if 3 args are passed its handle repo shell
34
34
+
// if more args are passed its an error
35
35
+
36
36
+
let mut handle: Option<String> = None;
37
37
+
let mut repo_name: Option<String> = None;
38
38
+
let mut shell: Option<String> = None;
39
39
+
match std::env::args().collect::<Vec<_>>().len() {
40
40
+
// 0 args (std env args includes this script)
41
41
+
1 => {}
42
42
+
2 => {
43
43
+
shell = Some(std::env::args().last().expect("Invalid state: 2 `Some` std::env::args() but found no Some values"))
44
44
+
}
45
45
+
3 => {
46
46
+
// load args and consume first
47
47
+
let mut args = std::env::args();
48
48
+
args.next();
49
49
+
50
50
+
if let Some(val) = args.next() {
51
51
+
if val.starts_with("@") {
52
52
+
if val.contains("/") {
53
53
+
let entries: Vec<_> = val.split("/").collect();
54
54
+
if entries.len() != 2 {
55
55
+
return Err(());
56
56
+
}
57
57
+
handle = Some(entries[0][1..].to_string());
58
58
+
repo_name = Some(entries[1].to_string());
59
59
+
} else {
60
60
+
handle = Some(val[1..].to_string());
61
61
+
}
62
62
+
} else {
63
63
+
repo_name = Some(val)
64
64
+
};
65
65
+
}
66
66
+
shell = Some(args.next().expect("Invalid state: 3 `Some` std::env::args() but only found 2"));
67
67
+
}
68
68
+
4 => {
69
69
+
// load args and consume first
70
70
+
let mut args = std::env::args();
71
71
+
args.next();
72
72
+
73
73
+
handle = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 1"));
74
74
+
repo_name = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 2"));
75
75
+
shell = Some(args.next().expect("Invalid state: 4 `Some` std::env::args() but only found 3"));
76
76
+
}
77
77
+
_ => {
78
78
+
// err
79
79
+
}
80
80
+
}
81
81
+
82
82
+
if let Some(ref handle) = handle && let Some(ref repo_name) = repo_name && let Some(ref shell) = shell {
83
83
+
return Ok(Config {
84
84
+
handle: handle.to_string(),
85
85
+
repo_name: repo_name.to_string(),
86
86
+
shell: shell.to_string(),
87
87
+
});
88
88
+
}
89
89
+
26
90
return Ok(Config {
27
27
-
handle: String::from(""),
28
28
-
repo_name: String::from(""),
29
29
-
shell: String::from("")
91
91
+
handle: handle.unwrap_or(String::new()),
92
92
+
repo_name: repo_name.unwrap_or(String::new()),
93
93
+
shell: shell.unwrap_or(String::new()),
30
94
});
31
95
}
32
96
33
97
fn main() -> Result<(), ()> {
34
34
-
35
98
// load configuration
36
99
let config = match load_config() {
37
100
Ok(res) => res,