Skip to content

Commit ce7924b

Browse files
committed
fix(cli): resolve errcheck violations and standardize config command flags
BREAKING CHANGE: config template generate and export commands now use --file instead of --output flag ## Error Handling Fixes - Fix unchecked MarkHidden() error returns in database command (cmd/database/database.go) - Fix unchecked MongoDB client.Disconnect() error returns in roles command (cmd/database/roles/roles.go) - Fix unchecked MongoDB client.Disconnect() error returns in users command (cmd/database/users/users.go) - Add proper error handling with panic for flag configuration failures - Add warning messages for MongoDB connection cleanup failures ## CLI Improvements - Standardize config command flags: rename --output/-o to --file for consistency - Update template generate command examples and help text - Update export command examples and help text - Improve flag naming consistency across config subcommands ## Testing Infrastructure - Add comprehensive config command test suite (scripts/test/config-test.sh) - Test template generation, validation, experimental commands, error handling - Add config tests to main test runner (scripts/test.sh) - Include config tests in comprehensive test suite ## Documentation - Update config.md to reflect --file flag usage - Update command help text and examples ## Project Tracking - Document all bug fixes and improvements in tracking/bugfixes.md - Update test script tracking documentation Resolves GoLinting errcheck violations and improves CLI consistency. Refs: #errcheck-fixes #config-standardization
1 parent ee1d452 commit ce7924b

9 files changed

Lines changed: 1000 additions & 288 deletions

File tree

cmd/config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,17 @@ Resource Configuration Templates:
117117
matlas config template generate basic
118118
119119
# Generate Atlas configuration template to file
120-
matlas config template generate atlas --output atlas-config.yaml
120+
matlas config template generate atlas --file atlas-config.yaml
121121
122122
# Generate in JSON format
123-
matlas config template generate complete --format json --output config.json`,
123+
matlas config template generate complete --format json --file config.json`,
124124
RunE: func(cmd *cobra.Command, args []string) error {
125125
templateType = args[0]
126126
return runGenerateTemplate(cmd, templateType, outputFile, format)
127127
},
128128
}
129129

130-
cmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file path (default: stdout)")
130+
cmd.Flags().StringVar(&outputFile, "file", "", "Output file path (default: stdout)")
131131
cmd.Flags().StringVarP(&format, "format", "f", "yaml", "Output format (yaml, json)")
132132

133133
return cmd
@@ -210,10 +210,10 @@ This command can export configurations to:
210210
- YAML configuration files
211211
- Shell export statements`,
212212
Example: ` # Export to environment file
213-
matlas config export --format env --output config.env
213+
matlas config export --format env --file config.env
214214
215215
# Export to JSON
216-
matlas config export --format json --output config.json
216+
matlas config export --format json --file config.json
217217
218218
# Export including sensitive values
219219
matlas config export --include-secrets --format yaml
@@ -225,7 +225,7 @@ This command can export configurations to:
225225
},
226226
}
227227

228-
cmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file path (default: stdout)")
228+
cmd.Flags().StringVar(&outputFile, "file", "", "Output file path (default: stdout)")
229229
cmd.Flags().StringVarP(&format, "format", "f", "yaml", "Export format (yaml, json, env, shell)")
230230
cmd.Flags().BoolVar(&includeSecrets, "include-secrets", false, "Include sensitive values in export")
231231

cmd/database/database.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ available databases, including their size, whether they're empty, and collection
9292

9393
// Hidden flag for advanced users to specify custom roles for temporary users
9494
cmd.Flags().String("temp-user-roles", "", "Advanced: Multiple custom roles for temporary user (format: 'role1@db1,role2@db2'). Default: readWriteAnyDatabase@admin")
95-
cmd.Flags().MarkHidden("temp-user-roles")
95+
if err := cmd.Flags().MarkHidden("temp-user-roles"); err != nil {
96+
// This should not fail as the flag was just added
97+
panic(fmt.Errorf("failed to mark temp-user-roles flag as hidden: %w", err))
98+
}
9699

97100
// At least one connection method is required
98101
cmd.MarkFlagsOneRequired("connection-string", "cluster")
@@ -164,7 +167,10 @@ Authentication options:
164167

165168
// Hidden flag for advanced users to specify custom roles for temporary users
166169
cmd.Flags().String("temp-user-roles", "", "Advanced: Multiple custom roles for temporary user (format: 'role1@db1,role2@db2'). Default: readWrite@<database>")
167-
cmd.Flags().MarkHidden("temp-user-roles")
170+
if err := cmd.Flags().MarkHidden("temp-user-roles"); err != nil {
171+
// This should not fail as the flag was just added
172+
panic(fmt.Errorf("failed to mark temp-user-roles flag as hidden: %w", err))
173+
}
168174

169175
// At least one connection method is required
170176
cmd.MarkFlagsOneRequired("connection-string", "cluster")
@@ -228,7 +234,10 @@ Use with caution in production environments.`,
228234

229235
// Hidden flag for advanced users to specify custom roles for temporary users
230236
cmd.Flags().String("temp-user-roles", "", "Advanced: Multiple custom roles for temporary user (format: 'role1@db1,role2@db2'). Default: readWriteAnyDatabase@admin")
231-
cmd.Flags().MarkHidden("temp-user-roles")
237+
if err := cmd.Flags().MarkHidden("temp-user-roles"); err != nil {
238+
// This should not fail as the flag was just added
239+
panic(fmt.Errorf("failed to mark temp-user-roles flag as hidden: %w", err))
240+
}
232241

233242
// At least one connection method is required
234243
cmd.MarkFlagsOneRequired("connection-string", "cluster")

cmd/database/roles/roles.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ func runListRoles(cmd *cobra.Command, connectionString, clusterName, projectID,
242242
progress.StopSpinnerWithError("Failed to connect to MongoDB")
243243
return fmt.Errorf("failed to connect to MongoDB: %w", err)
244244
}
245-
defer client.Disconnect(ctx)
245+
defer func() {
246+
if err := client.Disconnect(ctx); err != nil {
247+
fmt.Printf("Warning: Failed to disconnect from MongoDB: %v\n", err)
248+
}
249+
}()
246250

247251
// Get the database
248252
db := client.Database(databaseName)
@@ -336,7 +340,11 @@ func runCreateRole(cmd *cobra.Command, connectionString, clusterName, projectID,
336340
progress.StopSpinnerWithError("Failed to connect to MongoDB")
337341
return fmt.Errorf("failed to connect to MongoDB: %w", err)
338342
}
339-
defer client.Disconnect(ctx)
343+
defer func() {
344+
if err := client.Disconnect(ctx); err != nil {
345+
fmt.Printf("Warning: Failed to disconnect from MongoDB: %v\n", err)
346+
}
347+
}()
340348

341349
// Get the database
342350
db := client.Database(databaseName)
@@ -444,7 +452,11 @@ func runDeleteRole(cmd *cobra.Command, connectionString, clusterName, projectID,
444452
progress.StopSpinnerWithError("Failed to connect to MongoDB")
445453
return fmt.Errorf("failed to connect to MongoDB: %w", err)
446454
}
447-
defer client.Disconnect(ctx)
455+
defer func() {
456+
if err := client.Disconnect(ctx); err != nil {
457+
fmt.Printf("Warning: Failed to disconnect from MongoDB: %v\n", err)
458+
}
459+
}()
448460

449461
// Get the database
450462
db := client.Database(databaseName)
@@ -484,7 +496,11 @@ func runGetRole(cmd *cobra.Command, connectionString, clusterName, projectID, da
484496
progress.StopSpinnerWithError("Failed to connect to MongoDB")
485497
return fmt.Errorf("failed to connect to MongoDB: %w", err)
486498
}
487-
defer client.Disconnect(ctx)
499+
defer func() {
500+
if err := client.Disconnect(ctx); err != nil {
501+
fmt.Printf("Warning: Failed to disconnect from MongoDB: %v\n", err)
502+
}
503+
}()
488504

489505
// Get the database
490506
db := client.Database(databaseName)

cmd/database/users/users.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,11 @@ func runListDatabaseUsers(cmd *cobra.Command, connectionString, clusterName, pro
312312
progress.StopSpinnerWithError("Failed to connect to MongoDB")
313313
return fmt.Errorf("failed to connect to MongoDB: %w", err)
314314
}
315-
defer client.Disconnect(ctx)
315+
defer func() {
316+
if err := client.Disconnect(ctx); err != nil {
317+
fmt.Printf("Warning: Failed to disconnect from MongoDB: %v\n", err)
318+
}
319+
}()
316320

317321
// Get the admin database for user operations
318322
// Note: All user management operations should be performed on admin database

docs/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ List and generate config templates.
2525

2626
```bash
2727
matlas config template list
28-
matlas config template generate <basic|atlas|database|apply|complete> [-o file] [-f yaml|json]
28+
matlas config template generate <basic|atlas|database|apply|complete> [--file output-file] [--format yaml|json]
2929
```
3030

3131
Notes:

scripts/test.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ COMMANDS:
4747
discovery Run discovery lifecycle tests (comprehensive discovery feature testing)
4848
Use --cluster-lifecycle flag to include cluster creation/deletion tests (costs money!)
4949
applydoc Run ApplyDocument format tests (comprehensive coverage)
50+
config Run configuration command tests (validate, template generation, experimental commands)
5051
all Run all tests (unit + integration + e2e)
5152
comprehensive Run all tests including cluster and applydoc tests
5253
clean Clean test cache and artifacts
@@ -67,6 +68,8 @@ EXAMPLES:
6768
$0 discovery # Run discovery lifecycle tests
6869
$0 discovery --cluster-lifecycle # Run discovery tests with cluster creation (costs money!)
6970
$0 applydoc # Run ApplyDocument format tests
71+
$0 config # Run configuration command tests
72+
$0 config --verbose # Run config tests with verbose output
7073
$0 e2e # Run e2e tests (users/network only)
7174
$0 e2e --include-clusters # Run e2e tests with real clusters
7275
$0 users # Run live users lifecycle tests
@@ -237,17 +240,29 @@ main() {
237240
return 1
238241
fi
239242
;;
243+
config)
244+
print_info "Running configuration command tests..."
245+
print_info "Testing: validate, template generation, experimental commands, error handling"
246+
if "$SCRIPT_DIR/test/config-test.sh" "${args[@]}"; then
247+
print_success "Configuration command tests passed"
248+
else
249+
print_error "Configuration command tests failed"
250+
return 1
251+
fi
252+
;;
240253
comprehensive)
241254
local failed=0
242255
print_info "Running comprehensive test suite (all test types)..."
243256
run_test_type "unit" "${args[@]}" || ((failed++))
244257
run_test_type "integration" "${args[@]}" || ((failed++))
245258
run_test_type "e2e" "${args[@]}" || ((failed++))
246259

247-
print_warning "⚠️ Including ApplyDocument, discovery, and cluster tests - CLUSTER TESTS CREATE/DELETE REAL CLUSTERS!"
260+
print_warning "⚠️ Including ApplyDocument, discovery, config, and cluster tests - CLUSTER TESTS CREATE/DELETE REAL CLUSTERS!"
248261
print_success "✓ SAFE MODE: Cluster tests use --preserve-existing to protect existing clusters"
249262
print_info "ℹ️ Database and discovery tests require existing cluster but do NOT create/delete clusters"
263+
print_info "ℹ️ Config tests are safe and do not require external resources"
250264
"$SCRIPT_DIR/test/applydocument-test.sh" "${args[@]}" || ((failed++))
265+
"$SCRIPT_DIR/test/config-test.sh" "${args[@]}" || ((failed++))
251266
"$SCRIPT_DIR/test/discovery-lifecycle.sh" "${args[@]}" || ((failed++))
252267
"$SCRIPT_DIR/test/database-operations.sh" "${args[@]}" || ((failed++))
253268
"$SCRIPT_DIR/test/cluster-lifecycle.sh" "${args[@]}" || ((failed++))

0 commit comments

Comments
 (0)