github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/commands/resources/common/edit.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 Edit[T structureSpec.Structure] struct {
    12  	PromptsGetOrSelect func(ctx *cli.Context) (T, error)
    13  	PromptsEdit        func(ctx *cli.Context, prev T) error
    14  	TableConfirm       func(ctx *cli.Context, resource T, prompt string) bool
    15  	PromptsEditThis    string
    16  	LibSet             func(resource T) error
    17  	I18nEdited         func(name string)
    18  
    19  	UniqueFlags []cli.Flag
    20  }
    21  
    22  func (h *Edit[T]) Default() common.Command {
    23  	return common.Create(
    24  		&cli.Command{
    25  			Flags:  h.BasicFlags(),
    26  			Action: h.Action(),
    27  		},
    28  	)
    29  }
    30  
    31  func (h *Edit[T]) BasicFlags() []cli.Flag {
    32  	return append(append([]cli.Flag{
    33  		flags.Description,
    34  		flags.Tags,
    35  	},
    36  		// Insert unique flags between basic and Yes
    37  		h.UniqueFlags...),
    38  
    39  		flags.Yes,
    40  	)
    41  }
    42  
    43  func (h *Edit[T]) Action() func(ctx *cli.Context) error {
    44  	PanicIfMissingValue(h)
    45  
    46  	return func(ctx *cli.Context) error {
    47  		err := projectLib.ConfirmSelectedProject()
    48  		if err != nil {
    49  			return err
    50  		}
    51  
    52  		// Prompts.GetOrSelect will get the struct from --name or offer a selection menu
    53  		resource, err := h.PromptsGetOrSelect(ctx)
    54  		if err != nil {
    55  			return err
    56  		}
    57  
    58  		// Prompts.Edit will get the relative items from flags
    59  		// and edit the real values in the passed in struct pointer
    60  		// resource Edit does not return an error
    61  		err = h.PromptsEdit(ctx, resource)
    62  		if err != nil {
    63  			return err
    64  		}
    65  
    66  		// Table.Confirm will display a table and wait for a confirmation based on the
    67  		// flag provided to `tau edit -y` or offer a y\n selection
    68  		confirm := h.TableConfirm(ctx, resource, h.PromptsEditThis)
    69  		if confirm {
    70  
    71  			// Lib.Set handles the seer set based on selected project/application
    72  			err = h.LibSet(resource)
    73  			if err != nil {
    74  				return err
    75  			}
    76  			// I18n.Edited will display a message that the resource of name has been edited
    77  			h.I18nEdited(resource.GetName())
    78  
    79  			return nil
    80  		}
    81  
    82  		return nil
    83  	}
    84  }