Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Sep 26, 2025

This PR addresses issue #XXX by replacing hardcoded flag strings with constants throughout all viper.Get* calls to prevent flags from getting out of sync.

Problem

Previously, flag definitions and viper usage relied on hardcoded strings that could easily get out of sync if flag names were changed:

cmd.Flags().String("host", "localhost", "host to listen on")
// ... later in code
host := viper.GetString("host")  // Could become out of sync

Solution

Introduced flag constants in each command file to ensure consistency:

const hostFlag = "host"
cmd.Flags().String(hostFlag, "localhost", "host to listen on")
// ... later in code
host := viper.GetString(hostFlag)  // Now guaranteed to stay in sync

Changes Made

  • Server (cmd/root.go): Added constants for all server flags (host, port, brokers, topic, db, tls-cert, tls-key, config, json-logging, debug)
  • CLI Root (cli/cmd/root.go): Added constants for common flags and removed duplicate viper.AutomaticEnv() call
  • All CLI Commands: Updated event, receiver, group, and status commands with localized flag constants

Benefits

  • Type safety: Flag name typos are caught at compile time
  • Consistency: Impossible for flag definitions and usage to get out of sync
  • Maintainability: Flag name changes only require updating the constant definition
  • Clean code: Eliminates magic strings throughout the codebase

Testing

  • All existing unit tests pass
  • Both server and CLI binaries build successfully
  • Manual testing confirms all flags work correctly with help output and functionality verified

The implementation keeps flag constants localized to their respective command files as requested, avoiding cross-project dependencies while ensuring consistency between flag definitions and viper usage.

Original prompt

This section details on the original issue you should resolve

<issue_title>Viper flags should use constants for flag values</issue_title>
<issue_description>To avoid getting flags out of sync if we ever change them, the viper names should use constants. So instead of:

cmd.Flags().String("foo", "", "it does stuff")
...
foo := viper.GetString("foo")

do this:

const fooFlag := "foo"
cmd.Flags().String(fooFlag , "", "it does stuff")
...
foo := viper.GetString(fooFlag )

Keep the flags related to whatever commands they reference. No need to reference variables from way across the project.</issue_description>

<agent_instructions>Review all the commands in cmd and cli/cmd and update all values passed to viper.Get commands to be constants
</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@caproven Cobra/viper related & could possibly be looked at when handling this issue:

I've noticed cli/cmd/root.go has 2 calls of viper.AutomaticEnv(). I haven't looked any further to see if one could be safely deleted, but imagine this is unintentional.</comment_new>

Fixes #81

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Viper flags should use constants for flag values Replace hardcoded flag strings with constants in viper calls Sep 26, 2025
@Copilot Copilot AI requested a review from xbcsmith September 26, 2025 20:55
Copilot finished work on behalf of xbcsmith September 26, 2025 20:55
@xbcsmith xbcsmith marked this pull request as ready for review October 4, 2025 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Viper flags should use constants for flag values

2 participants