-
Notifications
You must be signed in to change notification settings - Fork 2
S3UTILS-227: replace util.parseArgs with manual argv parsing #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,6 @@ | |
| const fs = require('fs'); | ||
| const http = require('http'); | ||
| const https = require('https'); | ||
| const { parseArgs } = require('util'); | ||
| const { Client: VaultClient } = require('vaultclient'); | ||
| const AWS = require('aws-sdk'); | ||
|
|
||
|
|
@@ -49,17 +48,30 @@ function log(message) { | |
| } | ||
|
|
||
| // =========================================================================== | ||
| // Argument parsing (Node.js 22+ built-in util.parseArgs) | ||
| // Argument parsing (manual process.argv for Node 16+ compatibility) | ||
| // =========================================================================== | ||
| function getConfig() { | ||
| const { values, positionals } = parseArgs({ | ||
| allowPositionals: true, | ||
| options: { | ||
| 'iam-port': { type: 'string', default: '8600' }, | ||
| 'https': { type: 'boolean', default: false }, | ||
| 'dry-run': { type: 'boolean', default: false }, | ||
| }, | ||
| }); | ||
| const args = process.argv.slice(2); | ||
|
|
||
| const positionals = []; | ||
| let iamPort = 8600; | ||
| let useHttps = false; | ||
| let dryRun = false; | ||
|
|
||
| for (let i = 0; i < args.length; i++) { | ||
| if (args[i] === '--iam-port') { | ||
| if (i + 1 >= args.length) { | ||
| throw new Error('Missing value for --iam-port'); | ||
| } | ||
| iamPort = parseInt(args[++i], 10); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| } else if (args[i] === '--https') { | ||
| useHttps = true; | ||
| } else if (args[i] === '--dry-run') { | ||
| dryRun = true; | ||
| } else { | ||
| positionals.push(args[i]); | ||
| } | ||
| } | ||
|
|
||
| if (positionals.length < 3) { | ||
| log('Usage: node fix-missing-replication-permissions.js' | ||
|
|
@@ -72,11 +84,11 @@ function getConfig() { | |
| return { | ||
| inputFile, | ||
| vaultHost, | ||
| iamPort: parseInt(values['iam-port'], 10), | ||
| iamPort, | ||
| adminConfig, | ||
| useHttps: values.https, | ||
| useHttps, | ||
| outputFile: outputFile || 'replication-fix-results.json', | ||
| dryRun: values['dry-run'], | ||
| dryRun, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thoughts but consolidated using claude LLM:
The current implementation works, but the parsing logic is tightly coupled, order-dependent, and will become harder to maintain as flags grow.
A few specific concerns:
Parsing, validation, and state mutation are all interleaved, making this difficult to test in isolation.
If we want to solve an already solved problem(see last paragraph), I’d recommend moving toward a table-driven approach where each flag maps to a handler. That separates concerns and makes the system extensible without modifying the core loop. At a minimum, a cas-switch-based structure with explicit validation would improve readability and safety.
More broadly, this is reimplementing CLI parsing logic that libraries like minimist or commander already handle robustly (including defaults, type coercion, aliases, and help text). If this script is expected to evolve beyond a couple of flags, introducing a lightweight dependency would reduce long-term complexity and edge cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ephemeral script (will be deleted o nce we update replication permissions for all clients using CRR cf PR description) is meant to be copied into older vault containers where only a minimal set of dependencies exist.
Using
minimistwould introduce an additional dependency, and we would need to ensure it behaves consistently across different Vault versions.for 3 flags, the manual approach is arguably fine. The commit message on HEAD (ec54481) even says "replace util.parseArgs with manual argv parsing" -> so this was a deliberate choice for compatibility.