v0.3.2 - Implement -p/--port CLI option for first-time startup port override
- Add cli_options_t structure for extensible command line options - Implement port override in create_default_config_event() - Update main() with robust CLI parsing and validation - Add comprehensive help text documenting first-time only behavior - Ensure CLI options only affect initial configuration event creation - Maintain event-based configuration architecture for ongoing operation - Include comprehensive error handling and input validation - Add documentation in CLI_PORT_OVERRIDE_IMPLEMENTATION.md Tested: First-time startup uses CLI port, subsequent startups use database config
This commit is contained in:
28
src/config.c
28
src/config.c
@@ -443,7 +443,8 @@ int generate_random_private_key_bytes(unsigned char* privkey_bytes) {
|
||||
|
||||
cJSON* create_default_config_event(const unsigned char* admin_privkey_bytes,
|
||||
const char* relay_privkey_hex,
|
||||
const char* relay_pubkey_hex) {
|
||||
const char* relay_pubkey_hex,
|
||||
const cli_options_t* cli_options) {
|
||||
if (!admin_privkey_bytes || !relay_privkey_hex || !relay_pubkey_hex) {
|
||||
log_error("Invalid parameters for creating default config event");
|
||||
return NULL;
|
||||
@@ -475,11 +476,28 @@ cJSON* create_default_config_event(const unsigned char* admin_privkey_bytes,
|
||||
cJSON_AddItemToArray(relay_privkey_tag, cJSON_CreateString(relay_privkey_hex));
|
||||
cJSON_AddItemToArray(tags, relay_privkey_tag);
|
||||
|
||||
// Add all default configuration values
|
||||
// Add all default configuration values with command line overrides
|
||||
for (size_t i = 0; i < DEFAULT_CONFIG_COUNT; i++) {
|
||||
cJSON* tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(tag, cJSON_CreateString(DEFAULT_CONFIG_VALUES[i].key));
|
||||
cJSON_AddItemToArray(tag, cJSON_CreateString(DEFAULT_CONFIG_VALUES[i].value));
|
||||
|
||||
// Check for command line overrides
|
||||
const char* value = DEFAULT_CONFIG_VALUES[i].value;
|
||||
if (cli_options) {
|
||||
// Override relay_port if specified on command line
|
||||
if (cli_options->port_override > 0 && strcmp(DEFAULT_CONFIG_VALUES[i].key, "relay_port") == 0) {
|
||||
char port_str[16];
|
||||
snprintf(port_str, sizeof(port_str), "%d", cli_options->port_override);
|
||||
cJSON_AddItemToArray(tag, cJSON_CreateString(port_str));
|
||||
log_info("Using command line port override in configuration event");
|
||||
printf(" Port: %d (overriding default %s)\n", cli_options->port_override, DEFAULT_CONFIG_VALUES[i].value);
|
||||
} else {
|
||||
cJSON_AddItemToArray(tag, cJSON_CreateString(value));
|
||||
}
|
||||
} else {
|
||||
cJSON_AddItemToArray(tag, cJSON_CreateString(value));
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(tags, tag);
|
||||
}
|
||||
|
||||
@@ -516,7 +534,7 @@ cJSON* create_default_config_event(const unsigned char* admin_privkey_bytes,
|
||||
// IMPLEMENTED FUNCTIONS
|
||||
// ================================
|
||||
|
||||
int first_time_startup_sequence(void) {
|
||||
int first_time_startup_sequence(const cli_options_t* cli_options) {
|
||||
log_info("Starting first-time startup sequence...");
|
||||
|
||||
// 1. Generate admin keypair using /dev/urandom + nostr_core_lib
|
||||
@@ -566,7 +584,7 @@ int first_time_startup_sequence(void) {
|
||||
}
|
||||
|
||||
// 5. Create initial configuration event using defaults
|
||||
cJSON* config_event = create_default_config_event(admin_privkey_bytes, relay_privkey, relay_pubkey);
|
||||
cJSON* config_event = create_default_config_event(admin_privkey_bytes, relay_privkey, relay_pubkey, cli_options);
|
||||
if (!config_event) {
|
||||
log_error("Failed to create default configuration event");
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user