Skip to content

Allow CLI flags to override config file settings with empty values#2195

Open
toller892 wants to merge 1 commit into
getsops:mainfrom
toller892:fix/cli-override-config-precedence
Open

Allow CLI flags to override config file settings with empty values#2195
toller892 wants to merge 1 commit into
getsops:mainfrom
toller892:fix/cli-override-config-precedence

Conversation

@toller892
Copy link
Copy Markdown

Problem

When a user passes --encrypted-regex='' on the command line, the intent is to unset the config file's encrypted_regex setting. Previously, the code checked if the CLI value was empty string and fell back to the config file value, making it impossible to override config settings with empty strings.

This affects all six suffix/regex flags: --encrypted-suffix, --unencrypted-suffix, --encrypted-regex, --unencrypted-regex, --encrypted-comment-regex, --unencrypted-comment-regex.

Fixes #617

Root Cause

In getEncryptConfig() (cmd/sops/main.go), the precedence logic was:

if encryptedRegex == "" {
    encryptedRegex = optionalConfig.EncryptedRegex
}

This treats "flag not provided" and "flag explicitly set to empty" identically.

Fix

Use cli.Context.IsSet() to distinguish between "flag not provided" (use config) and "flag explicitly set to empty string" (use CLI value):

if !c.IsSet("encrypted-regex") && encryptedRegex == "" {
    encryptedRegex = optionalConfig.EncryptedRegex
}

This pattern is already used elsewhere in the codebase (c.GlobalIsSet("indent") at line 2391).

Testing

  • go build ./cmd/sops/ passes
  • go test ./config/ passes
  • go test ./... -short passes (only pre-existing PGP keyring test fails, unrelated to this change)

When a user passes --encrypted-regex='' on the command line, the intent
is to unset the config file's encrypted_regex setting. Previously, the
code checked if the CLI value was empty and fell back to the config,
making it impossible to override config settings with empty strings.

Use cli.Context.IsSet() to distinguish 'flag not provided' from 'flag
explicitly set to empty string'. This applies to all six suffix/regex
flags: encrypted-suffix, unencrypted-suffix, encrypted-regex,
unencrypted-regex, encrypted-comment-regex, unencrypted-comment-regex.

Fixes getsops#617
@felixfontein felixfontein changed the title fix: allow CLI flags to override config file settings with empty values Allow CLI flags to override config file settings with empty values Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot override configuration settings

2 participants