Files
odo/pkg/odo/cli/component/delete.go
Mohammed Ahmed 54ff6263e3 Fixing issue #804 Improving delete command to list affected children (#1319)
* Attempting to delete application, now lists affected URLs

Signed-off-by: Mohammed Zeeshan Ahmed <mohammed.zee1000@gmail.com>

* Making component delete list affected objects before deletion.

* Applying missed go gmt

* Altering Application ListInProject to actually list in project.

 - Also includes changes to corresponding references to
use List instead

* Fixing imports in component that got missed

* Applying go fmt

* Improving messaging for application and project.

 - More detailed messages are now displayed about affected
   child components
 - Abortion of deletion in interactive, now logged as error

* Adding more details of affected components for project deletion

* Updating messaging to list linked services during component deletion

* Adding missed printProjectDeleteInfo after rebasae

* Updating component and project to use new format of URLs

* Fixing up changes messed up during rebase.

* Fixing up after rebase

* Applying go fmt

* Force deleting testversioncmp

* Adding missing message for application deletion

* Adding tests to ensure proper messaging during deletion of app and project

* Deletion of application now lists affected service instances.

* Fixing up application and project after rebase to use new structs

* Fixing up component after rebase to use new struct and removing linked services

  - New struct makes it harder to list these, so will figure out a way
    later

* Fixing up missing return of error

* Fixing unnessasary message.

* fixing after rebase

* Fixing up after rebase to use new format of URL

* Moving back to infof
2019-03-12 05:55:56 -07:00

124 lines
3.8 KiB
Go

package component
import (
"fmt"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/pkg/errors"
"github.com/redhat-developer/odo/pkg/log"
appCmd "github.com/redhat-developer/odo/pkg/odo/cli/application"
projectCmd "github.com/redhat-developer/odo/pkg/odo/cli/project"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
"github.com/golang/glog"
"github.com/redhat-developer/odo/pkg/component"
"github.com/spf13/cobra"
)
// DeleteRecommendedCommandName is the recommended delete command name
const DeleteRecommendedCommandName = "delete"
var deleteExample = ktemplates.Examples(` # Delete component named 'frontend'.
%[1]s frontend
`)
// DeleteOptions is a container to attach complete, validate and run pattern
type DeleteOptions struct {
componentForceDeleteFlag bool
*ComponentOptions
}
// NewDeleteOptions returns new instance of DeleteOptions
func NewDeleteOptions() *DeleteOptions {
return &DeleteOptions{false, &ComponentOptions{}}
}
// Complete completes log args
func (do *DeleteOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
err = do.ComponentOptions.Complete(name, cmd, args)
return
}
// Validate validates the list parameters
func (do *DeleteOptions) Validate() (err error) {
isExists, err := component.Exists(do.Client, do.componentName, do.Application)
if err != nil {
return err
}
if !isExists {
return fmt.Errorf("failed to delete component %s as it doesn't exist", do.componentName)
}
return
}
// Run has the logic to perform the required actions as part of command
func (do *DeleteOptions) Run() (err error) {
glog.V(4).Infof("component delete called")
glog.V(4).Infof("args: %#v", do)
err = printDeleteComponentInfo(do.Client, do.componentName, do.Context.Application, do.Context.Project)
if err != nil {
return err
}
if do.componentForceDeleteFlag || ui.Proceed(fmt.Sprintf("Are you sure you want to delete %v from %v?", do.componentName, do.Application)) {
err := component.Delete(do.Client, do.componentName, do.Application)
if err != nil {
return err
}
log.Successf("Component %s from application %s has been deleted", do.componentName, do.Application)
currentComponent, err := component.GetCurrent(do.Application, do.Project)
if err != nil {
return errors.Wrapf(err, "Unable to get current component")
}
if currentComponent == "" {
log.Info("No default component has been set")
} else {
log.Infof("Default component set to: %s", currentComponent)
}
} else {
return fmt.Errorf("Aborting deletion of component: %v", do.componentName)
}
return
}
// NewCmdDelete implements the delete odo command
func NewCmdDelete(name, fullName string) *cobra.Command {
do := NewDeleteOptions()
var componentDeleteCmd = &cobra.Command{
Use: fmt.Sprintf("%s <component_name>", name),
Short: "Delete an existing component",
Long: "Delete an existing component.",
Example: fmt.Sprintf(deleteExample, fullName),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(do, cmd, args)
},
}
componentDeleteCmd.Flags().BoolVarP(&do.componentForceDeleteFlag, "force", "f", false, "Delete component without prompting")
// Add a defined annotation in order to appear in the help menu
componentDeleteCmd.Annotations = map[string]string{"command": "component"}
componentDeleteCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
completion.RegisterCommandHandler(componentDeleteCmd, completion.ComponentNameCompletionHandler)
//Adding `--project` flag
projectCmd.AddProjectFlag(componentDeleteCmd)
//Adding `--application` flag
appCmd.AddApplicationFlag(componentDeleteCmd)
return componentDeleteCmd
}