Skip to content

feat: bind environment variables to CLI flags#135

Open
Dav-14 wants to merge 1 commit intomainfrom
feat/bind-env-to-flags
Open

feat: bind environment variables to CLI flags#135
Dav-14 wants to merge 1 commit intomainfrom
feat/bind-env-to-flags

Conversation

@Dav-14
Copy link
Copy Markdown
Contributor

@Dav-14 Dav-14 commented Mar 20, 2026

Summary

  • Adds service.BindEnvToCommand(cmd) so all CLI flags can be configured via environment variables
  • Fixes pre-existing go.mod issues (duplicate go-libs/v4 dependency line, missing gopkg.in/go-jose/go-jose.v4 replace directive) via go mod tidy
  • Supersedes feat: bind environment variable to flags #100 (which targeted a stale base branch)

Changes

  • Added go-libs/v3/service import to cmd/root.go
  • Added service.BindEnvToCommand(cmd) call after NewRootCommand() in Execute()
  • Cleaned up go.mod and go.sum via go mod tidy

Test plan

  • Verify that setting env vars like PROFILE=myprofile correctly binds to --profile flag
  • Verify existing CLI behavior is unchanged when no env vars are set

🤖 Generated with Claude Code

Add service.BindEnvToCommand(cmd) to the root command so all CLI flags
can be configured via environment variables. Also fixes go.mod issues
(duplicate dependency, missing go-jose replace directive) via go mod tidy.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Dav-14 Dav-14 requested a review from a team as a code owner March 20, 2026 11:02
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 20, 2026

Walkthrough

Added an import for github.com/formancehq/go-libs/v3/service and inserted a call to service.BindEnvToCommand(cmd) in the Execute() function, immediately after root command creation and before command execution context runs.

Changes

Cohort / File(s) Summary
Environment Command Binding
cmd/root.go
Added import for service library and introduced environment-to-command binding call in the Execute() function's control flow.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A binding hop, a service call,
Environment whispers through the hall,
Commands now listen, fresh and keen,
Where config vars were never seen! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: bind environment variables to CLI flags' directly and clearly describes the main change introduced in the pull request.
Description check ✅ Passed The description is directly related to the changeset, providing clear context about binding environment variables to CLI flags and go.mod cleanup.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/bind-env-to-flags
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cmd/root.go (1)

106-107: Move env binding into NewRootCommand constructor to ensure consistent env resolution across all code paths.

NewRootCommand() is called from two contexts: Execute() in cmd/root.go and directly from cmd/prompt.go (lines 110, 143). Currently, service.BindEnvToCommand() is only applied in Execute(), leaving prompt-initiated commands without env binding. Both paths then fall back to manual os.Getenv() calls in pkg/flags.go, creating inconsistent precedence between bound flags and direct env reads.

Moving BindEnvToCommand() into the NewRootCommand() constructor ensures all command instances receive consistent env binding behavior.

♻️ Proposed refactor
 func NewRootCommand() *cobra.Command {
	homedir, err := os.UserHomeDir()
	if err != nil {
		panic(err)
	}
+	service.BindEnvToCommand(cmd)
	return cmd
 }

 func Execute() {
	cmd := NewRootCommand()
-	service.BindEnvToCommand(cmd)
	if err := cmd.ExecuteContext(ctx); err != nil {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/root.go` around lines 106 - 107, Move the environment binding call into
the root command constructor so all command instances get consistent env
binding: remove the standalone service.BindEnvToCommand(cmd) call from Execute()
and instead invoke service.BindEnvToCommand(...) inside NewRootCommand() (the
constructor that returns *cobra.Command used by Execute() and cmd/prompt.go) so
both Execute() and direct callers (like prompt.go) receive the same env-bound
flags; update NewRootCommand to accept or obtain the service needed to call
BindEnvToCommand and ensure no remaining manual os.Getenv() fallbacks in
pkg/flags.go are relied on for precedence.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@cmd/root.go`:
- Around line 106-107: Move the environment binding call into the root command
constructor so all command instances get consistent env binding: remove the
standalone service.BindEnvToCommand(cmd) call from Execute() and instead invoke
service.BindEnvToCommand(...) inside NewRootCommand() (the constructor that
returns *cobra.Command used by Execute() and cmd/prompt.go) so both Execute()
and direct callers (like prompt.go) receive the same env-bound flags; update
NewRootCommand to accept or obtain the service needed to call BindEnvToCommand
and ensure no remaining manual os.Getenv() fallbacks in pkg/flags.go are relied
on for precedence.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fc49a5e6-8d7a-427d-a730-4a809a52a882

📥 Commits

Reviewing files that changed from the base of the PR and between 061c4d0 and 993638d.

⛔ Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (1)
  • cmd/root.go

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