Skip to content

fix(cmd): return error when config add flags are partially provided#3826

Open
Ankitsinghsisodya wants to merge 2 commits into
knative:mainfrom
Ankitsinghsisodya:fix/cli-config-add-partial-flags
Open

fix(cmd): return error when config add flags are partially provided#3826
Ankitsinghsisodya wants to merge 2 commits into
knative:mainfrom
Ankitsinghsisodya:fix/cli-config-add-partial-flags

Conversation

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

@Ankitsinghsisodya Ankitsinghsisodya commented May 21, 2026

Note: Re-submission of #3762, accidentally closed when the head repository was deleted.

Problem

config envs add --name=KEY (without --value) silently writes fn.Env{Name: &"KEY", Value: nil} to func.yaml. This entry is invisible in config envs list, renders as KEY= (empty string) at deploy time, and fails ValidateEnvs at build time with "env entry #N is missing value field". The OR condition in the non-interactive guard allowed the partial set to bypass the interactive prompt and go straight to Save.

config labels add --name=Foo (without --value) silently drops the flag and launches the interactive prompt with no explanation, because the AND condition means the non-interactive block is never entered.

Changes

cmd/config_envs.go

  • Added guard: if --name is provided but --value is not, return --value is required when --name is provided before calling Save.
  • Bulk-import usage (--value='{{ secret:S }}' without --name) is unaffected — the guard only triggers when name is set and value is nil.

cmd/config_labels.go

  • Added guards before the interactive fallback: if only --name or only --value is provided, return a clear error rather than silently entering interactive mode.

Tests

Added to cmd/config_test.go:

  • TestEnvsAddNameWithoutValue — verifies error is returned and Save is not called.
  • TestLabelsAddNameWithoutValue — verifies error when --name is given without --value.
  • TestLabelsAddValueWithoutName — verifies error when --value is given without --name.

Added validation to ensure that the --name flag cannot be provided without the --value flag, and vice versa, in both environment and label commands. This prevents the creation of invalid configurations. Added corresponding unit tests to verify the expected error behavior when these flags are misused.
Updated error handling in the label command to use errors.New instead of fmt.Errorf for consistency in flag validation messages. This change improves clarity and maintains uniformity in error reporting when required flags are missing.
Copilot AI review requested due to automatic review settings May 21, 2026 11:20
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 21, 2026 11:20
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 21, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ankitsinghsisodya
Once this PR has been reviewed and has the lgtm label, please assign jrangelramos for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added size/M 🤖 PR changes 30-99 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 21, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 21, 2026

Hi @Ankitsinghsisodya. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds validation and tests to prevent config envs/labels add from accepting incomplete flag pairs (e.g., --name without --value) which would otherwise write broken config entries.

Changes:

  • Add a regression test ensuring config envs add --name without --value fails and does not save.
  • Add regression tests for config labels add when --name/--value are provided without the other.
  • Add CLI-side validation for config labels add when flags are incomplete.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cmd/config_test.go Adds tests for missing --name/--value combinations and a helper to build the labels command.
cmd/config_labels.go Adds validation intended to require --name and --value together.
cmd/config_envs.go Adds validation to require --value when --name is provided for envs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/config_labels.go
Comment on lines +96 to +99
if np != nil {
return errors.New("--value is required when --name is provided")
}
if vp != nil {
Comment thread cmd/config_test.go
Comment on lines +150 to +159
// TestLabelsAddNameWithoutValue ensures that providing --name without --value returns an error.
func TestLabelsAddNameWithoutValue(t *testing.T) {
mock := common.NewMockLoaderSaver()
mock.LoadFn = func(path string) (fn.Function, error) {
return fn.Function{}, nil
}
mock.SaveFn = func(f fn.Function) error {
t.Error("Save must not be called when validation fails")
return nil
}
@lkingland lkingland added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/M 🤖 PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants