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

     1  package common
     2  
     3  import "github.com/urfave/cli/v2"
     4  
     5  type beforeHandler struct {
     6  	linker *linker
     7  }
     8  
     9  func (l *linker) Before() BeforeHandler {
    10  	return beforeHandler{l}
    11  }
    12  
    13  // Shift will add a before function to the start of the before chain.
    14  func (h beforeHandler) Shift(method cli.BeforeFunc) {
    15  	prev := h.linker.Raw().Before
    16  
    17  	if prev != nil {
    18  		method = func(ctx *cli.Context) error {
    19  			err := prev(ctx)
    20  			if err != nil {
    21  				return err
    22  			}
    23  
    24  			return method(ctx)
    25  		}
    26  	}
    27  
    28  	h.linker.Raw().Before = method
    29  }
    30  
    31  // Push will add a before function to the end of the before chain.
    32  func (h beforeHandler) Push(method cli.BeforeFunc) {
    33  	prev := h.linker.Raw().Before
    34  
    35  	if prev != nil {
    36  		method = func(ctx *cli.Context) error {
    37  			err := method(ctx)
    38  			if err != nil {
    39  				return err
    40  			}
    41  
    42  			return prev(ctx)
    43  		}
    44  	}
    45  
    46  	h.linker.Raw().Before = method
    47  }