github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/prompts/internal/mock/mock.go (about)

     1  //go:build !production
     2  
     3  package mock
     4  
     5  import (
     6  	"os"
     7  
     8  	"github.com/urfave/cli/v2"
     9  )
    10  
    11  type CLI struct {
    12  	Flags  []cli.Flag
    13  	Action cli.ActionFunc
    14  	ToSet  map[string]string
    15  }
    16  
    17  func (m CLI) Run(args ...string) (_ctx *cli.Context, err error) {
    18  	app := &cli.App{
    19  		Flags: m.Flags,
    20  
    21  		// Capture ctx for returning
    22  		Action: func(ctx *cli.Context) error {
    23  			_ctx = ctx
    24  			if m.Action != nil {
    25  				return m.Action(_ctx)
    26  			} else {
    27  				return nil
    28  			}
    29  		},
    30  	}
    31  
    32  	err = app.Run(append([]string{os.Args[0]}, args...))
    33  	if err != nil {
    34  		return
    35  	}
    36  
    37  	if m.ToSet != nil {
    38  		for name, value := range m.ToSet {
    39  			if len(value) > 0 {
    40  				err = _ctx.Set(name, value)
    41  				if err != nil {
    42  					return
    43  				}
    44  			}
    45  		}
    46  	}
    47  
    48  	return
    49  }