aboutsummaryrefslogtreecommitdiffstats
path: root/rs
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2024-01-24 16:23:51 +0100
committerLibravatar Rutger Broekhoff2024-01-24 16:23:51 +0100
commitc16ca29d44bf42dd0a08bae4ebb82c8e93738f67 (patch)
tree0c0505167ae3e956fc3969c7fe498f58366a1999 /rs
parent2bba6b5341fd341de2b282e43f9b62f281ccf40e (diff)
downloadgitolfs3-c16ca29d44bf42dd0a08bae4ebb82c8e93738f67.tar.gz
gitolfs3-c16ca29d44bf42dd0a08bae4ebb82c8e93738f67.zip
Handle unknown commands
Diffstat (limited to 'rs')
-rw-r--r--rs/shell/src/main.rs21
1 files changed, 13 insertions, 8 deletions
diff --git a/rs/shell/src/main.rs b/rs/shell/src/main.rs
index cbe01e7..02da053 100644
--- a/rs/shell/src/main.rs
+++ b/rs/shell/src/main.rs
@@ -80,33 +80,35 @@ fn is_posix_space(c: char) -> bool {
80} 80}
81 81
82fn main() -> ExitCode { 82fn main() -> ExitCode {
83 let bad_usage = ExitCode::from(2);
84
83 let mut args = std::env::args().skip(1); 85 let mut args = std::env::args().skip(1);
84 if args.next() != Some("-c".to_string()) { 86 if args.next() != Some("-c".to_string()) {
85 eprintln!("Expected usage: shell -c <argument>"); 87 eprintln!("Expected usage: shell -c <argument>");
86 return ExitCode::FAILURE; 88 return bad_usage;
87 } 89 }
88 let Some(cmd) = args.next() else { 90 let Some(cmd) = args.next() else {
89 eprintln!("Missing argument for argument '-c'"); 91 eprintln!("Missing argument for argument '-c'");
90 return ExitCode::FAILURE; 92 return bad_usage;
91 }; 93 };
92 if args.next() != None { 94 if args.next() != None {
93 eprintln!("Too many arguments passed"); 95 eprintln!("Too many arguments passed");
94 return ExitCode::FAILURE; 96 return bad_usage;
95 } 97 }
96 98
97 let Some(mut cmd) = parse_cmd(&cmd) else { 99 let Some(mut cmd) = parse_cmd(&cmd) else {
98 eprintln!("Bad command"); 100 eprintln!("Bad command");
99 return ExitCode::FAILURE; 101 return bad_usage;
100 }; 102 };
101 103
102 let Some(mut program) = cmd.drain(0..1).next() else { 104 let Some(mut program) = cmd.drain(0..1).next() else {
103 eprintln!("Bad command"); 105 eprintln!("Bad command");
104 return ExitCode::FAILURE; 106 return bad_usage;
105 }; 107 };
106 if program == "git" { 108 if program == "git" {
107 let Some(subcommand) = cmd.drain(0..1).next() else { 109 let Some(subcommand) = cmd.drain(0..1).next() else {
108 eprintln!("Bad command"); 110 eprintln!("Bad command");
109 return ExitCode::FAILURE; 111 return bad_usage;
110 }; 112 };
111 program.push('-'); 113 program.push('-');
112 program.push_str(&subcommand); 114 program.push_str(&subcommand);
@@ -118,18 +120,21 @@ fn main() -> ExitCode {
118 if git_cmds.contains(&program.as_str()) { 120 if git_cmds.contains(&program.as_str()) {
119 if cmd.len() != 1 { 121 if cmd.len() != 1 {
120 eprintln!("Bad command"); 122 eprintln!("Bad command");
121 return ExitCode::FAILURE; 123 return bad_usage;
122 } 124 }
123 let repository = cmd[0].trim_start_matches('/'); 125 let repository = cmd[0].trim_start_matches('/');
124 args.push(repository); 126 args.push(repository);
125 } else if program == "git-lfs-authenticate" { 127 } else if program == "git-lfs-authenticate" {
126 if cmd.len() != 2 { 128 if cmd.len() != 2 {
127 eprintln!("Bad command"); 129 eprintln!("Bad command");
128 return ExitCode::FAILURE; 130 return bad_usage;
129 } 131 }
130 let repository = cmd[0].trim_start_matches('/'); 132 let repository = cmd[0].trim_start_matches('/');
131 args.push(repository); 133 args.push(repository);
132 args.push(&cmd[1]); 134 args.push(&cmd[1]);
135 } else {
136 eprintln!("Unknown command");
137 return bad_usage;
133 } 138 }
134 139
135 let e = std::process::Command::new(program).args(args).exec(); 140 let e = std::process::Command::new(program).args(args).exec();