get.porter.sh/porter@v1.3.0/pkg/porter/delete.go (about)

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"get.porter.sh/porter/pkg/cnab"
     9  	"get.porter.sh/porter/pkg/portercontext"
    10  )
    11  
    12  const installationDeleteTmpl = "deleting installation records for %s...\n"
    13  
    14  var (
    15  	// ErrUnsafeInstallationDelete warns the user that deletion of an unsuccessfully uninstalled installation is unsafe
    16  	ErrUnsafeInstallationDelete = errors.New("it is unsafe to delete an installation when the last action wasn't a successful uninstall")
    17  
    18  	// ErrUnsafeInstallationDeleteRetryForce presents the ErrUnsafeInstallationDelete error and provides a retry option of --force
    19  	ErrUnsafeInstallationDeleteRetryForce = fmt.Errorf("%s; if you are sure it should be deleted, retry the last command with the --force flag", ErrUnsafeInstallationDelete)
    20  )
    21  
    22  // DeleteOptions represent options for Porter's installation delete command
    23  type DeleteOptions struct {
    24  	installationOptions
    25  	Force bool
    26  }
    27  
    28  // Validate prepares for an installation delete action and validates the args/options.
    29  func (o *DeleteOptions) Validate(args []string, cxt *portercontext.Context) error {
    30  	// Ensure only one argument exists (installation name) if args length non-zero
    31  	err := o.installationOptions.validateInstallationName(args)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	return o.installationOptions.defaultBundleFiles(cxt)
    37  }
    38  
    39  // DeleteInstallation handles deletion of an installation
    40  func (p *Porter) DeleteInstallation(ctx context.Context, opts DeleteOptions) error {
    41  	err := p.applyDefaultOptions(ctx, &opts.installationOptions)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	installation, err := p.Installations.GetInstallation(ctx, opts.Namespace, opts.Name)
    47  	if err != nil {
    48  		return fmt.Errorf("unable to read status for installation %s: %w", opts.Name, err)
    49  	}
    50  
    51  	if (installation.Status.Action != cnab.ActionUninstall || installation.Status.ResultStatus != cnab.StatusSucceeded) && !opts.Force {
    52  		return ErrUnsafeInstallationDeleteRetryForce
    53  	}
    54  
    55  	fmt.Fprintf(p.Out, installationDeleteTmpl, opts.Name)
    56  	return p.Installations.RemoveInstallation(ctx, opts.Namespace, opts.Name)
    57  }