tools: remove panics in favor of errors in `v bump` (#19066)

This commit is contained in:
Rodrigo Villablanca 2023-08-06 15:24:43 -04:00 committed by GitHub
parent c3f7fe39ec
commit 23a7c40c44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 12 deletions

View File

@ -73,10 +73,10 @@ fn get_replacement_function(options Options) ReplacementFunction {
return replace_with_increased_patch_version
}
fn process_file(input_file string, options Options) {
lines := os.read_lines(input_file) or { panic('Failed to read file: ${input_file}') }
fn process_file(input_file string, options Options) ! {
lines := os.read_lines(input_file) or { return error('Failed to read file: ${input_file}') }
mut re := regex.regex_opt(semver_query) or { panic('Could not create a RegEx parser.') }
mut re := regex.regex_opt(semver_query) or { return error('Could not create a RegEx parser.') }
repl_fn := get_replacement_function(options)
@ -110,11 +110,11 @@ fn process_file(input_file string, options Options) {
os.rm(backup_file) or {}
// Rename the original to the backup.
os.mv(input_file, backup_file) or { panic('Failed to copy file: ${input_file}') }
os.mv(input_file, backup_file) or { return error('Failed to copy file: ${input_file}') }
// Process the old file and write it back to the original.
os.write_file(input_file, new_lines.join_lines()) or {
panic('Failed to write file: ${input_file}')
return error('Failed to write file: ${input_file}')
}
// Remove the backup file.
@ -129,7 +129,7 @@ fn process_file(input_file string, options Options) {
fn main() {
if os.args.len < 2 {
println('Usage: ${tool_name} [options] [file1 file2 ...]
eprintln('Usage: ${tool_name} [options] [file1 file2 ...]
${tool_description}
Try ${tool_name} -h for more help...')
exit(1)
@ -152,7 +152,7 @@ Try ${tool_name} -h for more help...')
}
remaining := fp.finalize() or {
println(fp.usage())
eprintln(fp.usage())
exit(1)
}
@ -161,24 +161,33 @@ Try ${tool_name} -h for more help...')
exit(0)
}
validate_options(options) or { panic(err) }
validate_options(options) or {
eprintln(fp.usage())
exit(1)
}
files := remaining[1..]
if files.len == 0 {
if !os.exists('v.mod') {
println('v.mod does not exist. You can create one using "v init".')
eprintln('v.mod does not exist. You can create one using "v init".')
exit(1)
}
process_file('v.mod', options) or {
eprintln('Failed to process v.mod: ${err}')
exit(1)
}
process_file('v.mod', options)
}
for input_file in files {
if !os.exists(input_file) {
println('File not found: ${input_file}')
eprintln('File not found: ${input_file}')
exit(1)
}
process_file(input_file, options) or {
eprintln('Failed to process ${input_file}: ${err}')
exit(1)
}
process_file(input_file, options)
}
}