github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/cmd/options.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/urfave/cli/v2"
     7  )
     8  
     9  type Option func(o *Options)
    10  
    11  type Options struct {
    12  	// Name of the application
    13  	Name string
    14  	// Description of the application
    15  	Description string
    16  	// Version of the application
    17  	Version string
    18  	// Action to execute when Run is called and there is no subcommand
    19  	Action func(*cli.Context) error
    20  	// TODO replace with built in command definition
    21  	Commands []*cli.Command
    22  	// TODO replace with built in flags definition
    23  	Flags []cli.Flag
    24  	// Other options for implementations of the interface
    25  	// can be stored in a context
    26  	Context context.Context
    27  }
    28  
    29  // Command line Name
    30  func Name(n string) Option {
    31  	return func(o *Options) {
    32  		o.Name = n
    33  	}
    34  }
    35  
    36  // Command line Description
    37  func Description(d string) Option {
    38  	return func(o *Options) {
    39  		o.Description = d
    40  	}
    41  }
    42  
    43  // Command line Version
    44  func Version(v string) Option {
    45  	return func(o *Options) {
    46  		o.Version = v
    47  	}
    48  }
    49  
    50  // Commands to add
    51  func Commands(c ...*cli.Command) Option {
    52  	return func(o *Options) {
    53  		o.Commands = c
    54  	}
    55  }
    56  
    57  // Flags to add
    58  func Flags(f ...cli.Flag) Option {
    59  	return func(o *Options) {
    60  		o.Flags = f
    61  	}
    62  }
    63  
    64  // Action to execute
    65  func Action(a func(*cli.Context) error) Option {
    66  	return func(o *Options) {
    67  		o.Action = a
    68  	}
    69  }
    70  
    71  type beforeKey struct{}
    72  type setupOnlyKey struct{}
    73  
    74  // Before sets a function to be called before micro is setup
    75  func Before(f cli.BeforeFunc) Option {
    76  	return func(o *Options) {
    77  		if o.Context == nil {
    78  			o.Context = context.Background()
    79  		}
    80  		o.Context = context.WithValue(o.Context, beforeKey{}, f)
    81  	}
    82  }
    83  
    84  func beforeFromContext(ctx context.Context, def cli.BeforeFunc) cli.BeforeFunc {
    85  	if ctx == nil {
    86  		return def
    87  	}
    88  
    89  	a, ok := ctx.Value(beforeKey{}).(cli.BeforeFunc)
    90  	if !ok {
    91  		return def
    92  	}
    93  
    94  	// perform the before func passed in the context before the default
    95  	return func(ctx *cli.Context) error {
    96  		if err := a(ctx); err != nil {
    97  			return err
    98  		}
    99  		return def(ctx)
   100  	}
   101  }
   102  
   103  // SetupOnly for to execute
   104  func SetupOnly() Option {
   105  	return func(o *Options) {
   106  		if o.Context == nil {
   107  			o.Context = context.Background()
   108  		}
   109  		o.Context = context.WithValue(o.Context, setupOnlyKey{}, true)
   110  	}
   111  }
   112  
   113  func setupOnlyFromContext(ctx context.Context) bool {
   114  	if ctx == nil {
   115  		return false
   116  	}
   117  
   118  	a, _ := ctx.Value(setupOnlyKey{}).(bool)
   119  	return a
   120  }