github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/commands/resources/common/delete.go (about)

     1  package resources
     2  
     3  import (
     4  	structureSpec "github.com/taubyte/go-specs/structure"
     5  	"github.com/taubyte/tau-cli/cli/common"
     6  	"github.com/taubyte/tau-cli/flags"
     7  	projectLib "github.com/taubyte/tau-cli/lib/project"
     8  	"github.com/urfave/cli/v2"
     9  )
    10  
    11  type Delete[T structureSpec.Structure] struct {
    12  	PromptsGetOrSelect func(ctx *cli.Context) (T, error)
    13  	TableConfirm       func(ctx *cli.Context, resource T, prompt string) bool
    14  	PromptsDeleteThis  string
    15  	LibDelete          func(name string) error
    16  	I18nDeleted        func(name string)
    17  }
    18  
    19  func (h *Delete[T]) Default() common.Command {
    20  	return common.Create(
    21  		&cli.Command{
    22  			Flags:  h.BasicFlags(),
    23  			Action: h.Action(),
    24  		},
    25  	)
    26  }
    27  
    28  func (h *Delete[T]) BasicFlags() []cli.Flag {
    29  	return []cli.Flag{
    30  		flags.Yes,
    31  	}
    32  }
    33  
    34  func (h *Delete[T]) Action() func(ctx *cli.Context) error {
    35  	PanicIfMissingValue(h)
    36  
    37  	return func(ctx *cli.Context) error {
    38  		err := projectLib.ConfirmSelectedProject()
    39  		if err != nil {
    40  			return err
    41  		}
    42  
    43  		// Prompts.GetOrSelect will get the struct from --name or offer a selection menu
    44  		resource, err := h.PromptsGetOrSelect(ctx)
    45  		if err != nil {
    46  			return err
    47  		}
    48  
    49  		// Table.Confirm will display a table and wait for a confirmation based on the
    50  		// flag provided to `tau delete -y` or offer a y\n selection
    51  		confirm := h.TableConfirm(ctx, resource, h.PromptsDeleteThis)
    52  		if confirm {
    53  
    54  			// Lib.Delete handles the seer deletion based on selected project/application
    55  			err = h.LibDelete(resource.GetName())
    56  			if err != nil {
    57  				return err
    58  			}
    59  
    60  			// I18n.Deleted will display a message that the resource of name has been deleted
    61  			h.I18nDeleted(resource.GetName())
    62  
    63  			return nil
    64  		}
    65  
    66  		return nil
    67  	}
    68  }