Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch name flag to args for delete template command #2346

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/cmd/alpha/templates/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package templates
import (
"fmt"

"github.com/kyma-project/cli.v3/internal/cmd/alpha/templates/parameters"
"github.com/spf13/cobra"
)

Expand All @@ -18,3 +19,13 @@ func AssignOptionalNameArg(name *string) func(*cobra.Command, []string) error {
return nil
}
}

func AssignRequiredNameArg(name parameters.Value) func(*cobra.Command, []string) error {
return func(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires exactly one resource name as an argument, received %d", len(args))
}

return name.Set(args[0])
}
}
39 changes: 29 additions & 10 deletions internal/cmd/alpha/templates/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/kyma-project/cli.v3/internal/clierror"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/templates/parameters"
Expand All @@ -17,6 +18,7 @@ import (
type DeleteOptions struct {
types.DeleteCommand
ResourceInfo types.ResourceInfo
RootCommand types.RootCommand
}

func BuildDeleteCommand(clientGetter KubeClientGetter, options *DeleteOptions) *cobra.Command {
Expand All @@ -26,9 +28,10 @@ func BuildDeleteCommand(clientGetter KubeClientGetter, options *DeleteOptions) *
func buildDeleteCommand(out io.Writer, clientGetter KubeClientGetter, options *DeleteOptions) *cobra.Command {
extraValues := []parameters.Value{}
cmd := &cobra.Command{
Use: "delete",
Short: options.Description,
Long: options.DescriptionLong,
Use: "delete",
Short: options.Description,
Long: options.DescriptionLong,
Example: buildDeleteExample(options),
Run: func(cmd *cobra.Command, args []string) {
clierror.Check(deleteResource(&deleteArgs{
out: out,
Expand All @@ -40,18 +43,34 @@ func buildDeleteCommand(out io.Writer, clientGetter KubeClientGetter, options *D
},
}

for _, flag := range commonResourceFlags(options.ResourceInfo.Scope) {
value := parameters.NewTyped(flag.Type, flag.Path, flag.DefaultValue)
cmd.Flags().VarP(value, flag.Name, flag.Shorthand, flag.Description)
if flag.Required {
_ = cmd.MarkFlagRequired(flag.Name)
}
extraValues = append(extraValues, value)
// define resource name as required args[0]
nameArgValue := parameters.NewTyped(resourceNameFlag.Type, resourceNameFlag.Path, resourceNameFlag.DefaultValue)
cmd.Args = AssignRequiredNameArg(nameArgValue)
extraValues = append(extraValues, nameArgValue)

// define --namespace/-n flag only is resource is namespace scoped
if options.ResourceInfo.Scope == types.NamespaceScope {
namespaceFlagValue := parameters.NewTyped(resourceNamespaceFlag.Type, resourceNamespaceFlag.Path, resourceNamespaceFlag.DefaultValue)
cmd.Flags().VarP(namespaceFlagValue, resourceNamespaceFlag.Name, resourceNamespaceFlag.Shorthand, resourceNamespaceFlag.Description)
extraValues = append(extraValues, namespaceFlagValue)
}

return cmd
}

func buildDeleteExample(options *DeleteOptions) string {
template := " # delete resource\n" +
" kyma alpha ROOT_COMMAND delete <resource_name>"

if options.ResourceInfo.Scope == types.NamespaceScope {
template += "\n\n" +
" # delete resource from specific namespace\n" +
" kyma alpha ROOT_COMMAND delete <resource_name> --namespace <resource_namespace> "
}

return strings.ReplaceAll(template, "ROOT_COMMAND", options.RootCommand.Name)
}

type deleteArgs struct {
out io.Writer
ctx context.Context
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/alpha/templates/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_remove(t *testing.T) {
require.Equal(t, "delete test deploy", cmd.Short)
require.Equal(t, "use this to delete test deploy", cmd.Long)

require.NotNil(t, cmd.Flag("name"))
require.NoError(t, cmd.ValidateArgs([]string{"resource_name"}))
require.NotNil(t, cmd.Flag("namespace"))
})

Expand All @@ -38,7 +38,7 @@ func Test_remove(t *testing.T) {

cmd := fixDeleteCommand(buf, &mock)

cmd.SetArgs([]string{"--name", "test-deploy", "--namespace", "test-namespace"})
cmd.SetArgs([]string{"test-deploy", "--namespace", "test-namespace"})

err := cmd.Execute()
require.NoError(t, err)
Expand Down
19 changes: 19 additions & 0 deletions internal/cmd/alpha/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var (
resourceNameFlag = types.CustomFlag{
Name: "name",
Type: types.StringCustomFlagType,
Description: "name of the resource",
Path: ".metadata.name",
Required: true,
}

resourceNamespaceFlag = types.CustomFlag{
Name: "namespace",
Type: types.StringCustomFlagType,
Description: "resource namespace",
Path: ".metadata.namespace",
DefaultValue: "default",
}
)

// TODO: remove this func and use vars above
func commonResourceFlags(resourceScope types.Scope) []types.CustomFlag {
params := []types.CustomFlag{
{
Expand Down
1 change: 1 addition & 0 deletions internal/cmdcommon/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func addGenericCommands(cmd *cobra.Command, config *KymaConfig, extension *Exten
if extension.Resource != nil && commands.DeleteCommand != nil {
cmd.AddCommand(availableTemplateCommands.Delete(config, &templates.DeleteOptions{
DeleteCommand: *commands.DeleteCommand,
RootCommand: extension.RootCommand,
ResourceInfo: *extension.Resource,
}))
}
Expand Down
Loading