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

[FEATURE] - Adding Convenience Aliases for Delete & Apply #868

Merged
merged 1 commit into from
Jun 24, 2023
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
81 changes: 81 additions & 0 deletions pkg/cmd/tnctl/apply/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2023 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package apply

import (
"fmt"
"os/exec"

"github.com/spf13/cobra"

"github.com/appvia/terranetes-controller/pkg/cmd"
)

// Command are the options for the command
type Command struct {
cmd.Factory
// File is the path to the file to apply
File []string
// Namespace is the namespace to apply the resources to
Namespace string
// DryRun is the dry run strategy
DryRun string
// Force is the force flag
Force bool
}

// NewCommand creates and returns a new command
func NewCommand(factory cmd.Factory) *cobra.Command {
o := &Command{Factory: factory}

c := &cobra.Command{
Use: "apply [OPTIONS] [-f PATH...]",
Short: "Used to apply from a file or stdin",
RunE: func(cmd *cobra.Command, args []string) error {
options := []string{"apply"}

for _, file := range o.File {
options = append(options, "-f", file)
}
if o.Force {
options = append(options, "--force")
}
if o.Namespace != "" {
options = append(options, fmt.Sprintf("--namespace=%s", o.Namespace))
}
if o.DryRun != "" {
options = append(options, "--dry-run "+o.DryRun)
}

exe := exec.CommandContext(cmd.Context(), "kubectl", options...)
exe.Stdout = factory.GetStreams().Out
exe.Stderr = factory.GetStreams().ErrOut
exe.Stdin = factory.GetStreams().In

return exe.Run()
},
}

flags := c.Flags()
flags.StringSliceVarP(&o.File, "file", "f", []string{}, "Path to file to apply")
flags.StringVarP(&o.Namespace, "namespace", "n", "", "Namespace the resources will applied to")
flags.StringVar(&o.DryRun, "dry-run", "", `Must be "none", "server", or "client". If client strategy, only print the object that would be sent, without sending it`)
flags.BoolVarP(&o.Force, "force", "", false, "If true, immediately remove resources from API and bypass graceful deletion")

return c
}
83 changes: 83 additions & 0 deletions pkg/cmd/tnctl/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2023 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package delete

import (
"fmt"
"os/exec"

"github.com/spf13/cobra"

"github.com/appvia/terranetes-controller/pkg/cmd"
)

// Command are the options for the command
type Command struct {
cmd.Factory
// File is the file to apply
File []string
// Namespace is the namespace to apply to
Namespace string
// Force, immediately remove resources from API and bypass graceful deletion
Force bool
// Wait, wait for resources to be gone before returning. This waits for finalizers.
Wait bool
}

// NewCommand creates and returns a new command
func NewCommand(factory cmd.Factory) *cobra.Command {
o := &Command{Factory: factory}

c := &cobra.Command{
Use: "delete [OPTIONS] [-f PATH|NAME...]",
Short: "Used to delete resource by file or resource name",
RunE: func(cmd *cobra.Command, args []string) error {
options := []string{"delete"}

for _, file := range o.File {
options = append(options, "-f", file)
}
if o.Namespace != "" {
options = append(options, fmt.Sprintf("--namespace=%s", o.Namespace))
}
if o.Force {
options = append(options, "--force")
}
if o.Wait {
options = append(options, "--wait")
}

options = append(options, args...)

exe := exec.CommandContext(cmd.Context(), "kubectl", options...)
exe.Stdout = factory.GetStreams().Out
exe.Stderr = factory.GetStreams().ErrOut
exe.Stdin = factory.GetStreams().In

return exe.Run()
},
}

flags := c.Flags()
flags.BoolVarP(&o.Force, "force", "", false, "If true, immediately remove resources from API and bypass graceful deletion")
flags.BoolVarP(&o.Wait, "wait", "", true, "If true, wait for resources to be gone before returning. This waits for finalizers.")
flags.StringSliceVarP(&o.File, "file", "f", []string{}, "Path to file to apply")
flags.StringVarP(&o.Namespace, "namespace", "n", "", "Namespace to apply to")

return c
}
5 changes: 0 additions & 5 deletions pkg/cmd/tnctl/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ import (
"github.com/appvia/terranetes-controller/pkg/cmd"
)

// Command are the options for the command
type Command struct {
cmd.Factory
}

// NewCommand creates and returns a new command
func NewCommand(factory cmd.Factory) *cobra.Command {
c := &cobra.Command{
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/tnctl/tnctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import (
"github.com/spf13/cobra"

"github.com/appvia/terranetes-controller/pkg/cmd"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/apply"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/approve"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/config"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/convert"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/create"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/delete"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/describe"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/generate"
"github.com/appvia/terranetes-controller/pkg/cmd/tnctl/get"
Expand Down Expand Up @@ -76,7 +78,9 @@ func New(factory cmd.Factory) *cobra.Command {
command.AddCommand(
config.NewCommand(factory),
approve.NewCommand(factory),
apply.NewCommand(factory),
get.NewCommand(factory),
delete.NewCommand(factory),
kubectl.NewCommand(factory),
search.NewCommand(factory),
convert.NewCommand(factory),
Expand Down