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

     1  package common
     2  
     3  import (
     4  	"github.com/urfave/cli/v2"
     5  )
     6  
     7  type linker struct {
     8  	parent *cli.Command
     9  	raw    *cli.Command
    10  	ops    []Option
    11  }
    12  
    13  func Create(cmd *cli.Command, ops ...Option) Command {
    14  	return &linker{nil, cmd, ops}
    15  }
    16  
    17  func (l *linker) Raw() *cli.Command {
    18  	return l.raw
    19  }
    20  
    21  func (l *linker) Parent() *cli.Command {
    22  	return l.parent
    23  }
    24  
    25  func (l *linker) Options() []Option {
    26  	return l.ops
    27  }
    28  
    29  func setBaseCmdFields(base *cli.Command, cmd *cli.Command) {
    30  	if cmd.Name == "" {
    31  		cmd.Name = base.Name
    32  	}
    33  
    34  	if cmd.ArgsUsage == "" {
    35  		cmd.ArgsUsage = base.ArgsUsage
    36  	}
    37  
    38  	if len(cmd.Aliases) == 0 {
    39  		cmd.Aliases = base.Aliases
    40  	}
    41  }
    42  
    43  /*
    44  Initialize will run the options of a command and return a command using the base command
    45  
    46  Example: tau new application
    47    - parentCmd: new
    48    - baseCmd: application
    49  */
    50  func (l *linker) Initialize(parentCmd *cli.Command, baseCmd *cli.Command, baseOps []Option) *cli.Command {
    51  	l.parent = parentCmd
    52  
    53  	setBaseCmdFields(baseCmd, l.raw)
    54  
    55  	for _, op := range append(baseOps, l.ops...) {
    56  		op(l)
    57  	}
    58  
    59  	return l.raw
    60  }
    61  
    62  func (l *linker) Linker() Linker {
    63  	return l
    64  }