fix: adds the missing source-name flag in app unset command (#26712)

Signed-off-by: Patroklos Papapetrou <ppapapetrou76@gmail.com>
This commit is contained in:
Papapetrou Patroklos 2026-03-08 18:11:16 +02:00 committed by GitHub
parent ffeacae414
commit 08602c27c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 5 deletions

View file

@ -995,6 +995,9 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
c.HelpFunc()(c, args)
os.Exit(1)
}
if sourceName != "" && sourcePosition != -1 {
errors.Fatal(errors.ErrorGeneric, "Only one of source-position and source-name can be specified.")
}
appName, appNs := argo.ParseFromQualifiedName(args[0], appNamespace)
conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie()
@ -1002,11 +1005,6 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
app, err := appIf.Get(ctx, &application.ApplicationQuery{Name: &appName, AppNamespace: &appNs})
errors.CheckError(err)
sourceName = appOpts.SourceName
if sourceName != "" && sourcePosition != -1 {
errors.Fatal(errors.ErrorGeneric, "Only one of source-position and source-name can be specified.")
}
if sourceName != "" {
sourceNameToPosition := getSourceNameToPositionMap(app)
pos, ok := sourceNameToPosition[sourceName]
@ -1069,6 +1067,7 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
command.Flags().BoolVar(&opts.passCredentials, "pass-credentials", false, "Unset passCredentials")
command.Flags().BoolVar(&opts.ref, "ref", false, "Unset ref on the source")
command.Flags().IntVar(&sourcePosition, "source-position", -1, "Position of the source from the list of sources of the app. Counting starts at 1.")
command.Flags().StringVar(&sourceName, "source-name", "", "Name of the source from the list of sources of the app.")
return command
}

View file

@ -1,12 +1,15 @@
package commands
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"slices"
"strings"
"testing"
@ -1047,6 +1050,39 @@ func TestPrintApplicationNames(t *testing.T) {
require.Equalf(t, output, expectation, "Incorrect print params output %q, should be %q", output, expectation)
}
func TestNewApplicationUnsetCommand_Validation(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
cmd := NewApplicationUnsetCommand(nil)
cmd.SetArgs([]string{"my-app", "--source-position", "1", "--source-name", "test"})
_ = cmd.Execute()
}
cmd := exec.CommandContext(context.Background(), os.Args[0], "-test.run=TestNewApplicationUnsetCommand_Validation")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if e, ok := errors.AsType[*exec.ExitError](err); ok && !e.Success() {
assert.Contains(t, stderr.String(), "Only one of source-position and source-name can be specified.")
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}
func TestNewApplicationUnsetCommand_Flags(t *testing.T) {
cmd := NewApplicationUnsetCommand(nil)
assert.NotNil(t, cmd)
flag := cmd.Flags().Lookup("source-name")
assert.NotNil(t, flag)
assert.Equal(t, "source-name", flag.Name)
flag = cmd.Flags().Lookup("source-position")
assert.NotNil(t, flag)
assert.Equal(t, "source-position", flag.Name)
}
func Test_unset(t *testing.T) {
kustomizeSource := &v1alpha1.ApplicationSource{
Kustomize: &v1alpha1.ApplicationSourceKustomize{

View file

@ -44,6 +44,7 @@ argocd app unset APPNAME parameters [flags]
--pass-credentials Unset passCredentials
--plugin-env stringArray Unset plugin env variables (e.g --plugin-env name)
--ref Unset ref on the source
--source-name string Name of the source from the list of sources of the app.
--source-position int Position of the source from the list of sources of the app. Counting starts at 1. (default -1)
--values stringArray Unset one or more Helm values files
--values-literal Unset literal Helm values block