Skip to content

Conversation

@water-sucks
Copy link
Contributor

Community Contribution License Agreement

By creating this pull request, I grant the project maintainers an unlimited, perpetual license to use, modify, and redistribute these contributions under any terms they choose, including both the AGPLv3 and the Fossorial Commercial license terms. I represent that I have the right to grant this license for all contributed content.

Description

This does a few refactors to flatten the structure of the Cobra commands to avoid excessive nesting and globals. It:

  • Moves all global flag variables into named option structs that are not globals
  • Moves flag validation to before running command main functions, if at all
    possible
  • Passes defaults to Cobra flag sets so that it can handle default fallbacks,
    rather than using custom closures to mimic this builtin functionality
  • Moves subcommand Run functions into named functions
  • Consolidates os.Exit() behavior to the Run() functions when possible, and
    uses error handling to determine whether or not to exit with a failure code
  • Replaces the clunky way of manually traversing the Cobra command tree in order
    to run a default command by just copying the command Run() function

How to test?

This is purely a refactoring PR, All functionality should be the same other than the fact that more states (i.e. unsuccessful login, selection, etc.) should now result in unsuccessful exit codes.

Instead of using global variables for Cobra flag values, and separate
defaults and clunky getter functions everywhere, set the defaults inside
Cobra's flag set itself.

This makes handling of values radically simpler.

Additionally, this also adds more stringent flag validation before even
running the program, and errors are handled more gracefully at the end
with a single os.Exit() invocation.
This normalizes error handling to always propagate up until the Run()
function, where this will exit with the 1 status code if an error
is detected.

Before this commit, some unsuccessful attempts and error states resulted
in an exit code of 0, which can throw off shell scripts.
@miloschwartz miloschwartz merged commit aec8830 into fosrl:dev Dec 20, 2025
@water-sucks water-sucks deleted the refactor-cobra-struture branch December 20, 2025 03:26
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.

2 participants