github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/delete_isolation_segment_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/v3action"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  	"code.cloudfoundry.org/cli/command/v6/shared"
    10  )
    11  
    12  //go:generate counterfeiter . DeleteIsolationSegmentActor
    13  
    14  type DeleteIsolationSegmentActor interface {
    15  	DeleteIsolationSegmentByName(name string) (v3action.Warnings, error)
    16  }
    17  
    18  type DeleteIsolationSegmentCommand struct {
    19  	RequiredArgs    flag.IsolationSegmentName `positional-args:"yes"`
    20  	Force           bool                      `short:"f" description:"Force deletion without confirmation"`
    21  	usage           interface{}               `usage:"CF_NAME delete-isolation-segment SEGMENT_NAME"`
    22  	relatedCommands interface{}               `related_commands:"disable-org-isolation, isolation-segments"`
    23  
    24  	UI          command.UI
    25  	Config      command.Config
    26  	SharedActor command.SharedActor
    27  	Actor       DeleteIsolationSegmentActor
    28  }
    29  
    30  func (cmd *DeleteIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error {
    31  	cmd.UI = ui
    32  	cmd.Config = config
    33  	cmd.SharedActor = sharedaction.NewActor(config)
    34  
    35  	client, _, err := shared.NewV3BasedClients(config, ui, true)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	cmd.Actor = v3action.NewActor(client, config, nil, nil)
    40  
    41  	return nil
    42  }
    43  
    44  func (cmd DeleteIsolationSegmentCommand) Execute(args []string) error {
    45  	err := cmd.SharedActor.CheckTarget(false, false)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	if !cmd.Force {
    51  		deleteSegment, promptErr := cmd.UI.DisplayBoolPrompt(false, "Really delete the isolation segment {{.IsolationSegmentName}}?", map[string]interface{}{
    52  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    53  		})
    54  
    55  		if promptErr != nil {
    56  			return promptErr
    57  		}
    58  
    59  		if !deleteSegment {
    60  			cmd.UI.DisplayText("Delete cancelled")
    61  			return nil
    62  		}
    63  	}
    64  
    65  	user, err := cmd.Config.CurrentUser()
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	cmd.UI.DisplayTextWithFlavor("Deleting isolation segment {{.SegmentName}} as {{.CurrentUser}}...", map[string]interface{}{
    71  		"SegmentName": cmd.RequiredArgs.IsolationSegmentName,
    72  		"CurrentUser": user.Name,
    73  	})
    74  
    75  	warnings, err := cmd.Actor.DeleteIsolationSegmentByName(cmd.RequiredArgs.IsolationSegmentName)
    76  	cmd.UI.DisplayWarnings(warnings)
    77  	if _, ok := err.(actionerror.IsolationSegmentNotFoundError); ok {
    78  		cmd.UI.DisplayWarning("Isolation segment {{.IsolationSegmentName}} does not exist.", map[string]interface{}{
    79  			"IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName,
    80  		})
    81  	} else if err != nil {
    82  		return err
    83  	}
    84  
    85  	cmd.UI.DisplayOK()
    86  
    87  	return nil
    88  }