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

     1  package common
     2  
     3  import (
     4  	"github.com/urfave/cli/v2"
     5  )
     6  
     7  type basicFunction func() Basic
     8  
     9  // Used to attach subCommands to their relative base commands
    10  // Ex: `tau new project`  project is attached to the new command
    11  func Attach(app *cli.App, commands ...basicFunction) {
    12  	for _, cmdFunc := range commands {
    13  		attachCommand(cmdFunc())
    14  	}
    15  
    16  	for _, cmd := range []*cli.Command{
    17  		_new,
    18  		_edit,
    19  		_delete,
    20  		_query,
    21  		_list,
    22  		_select,
    23  		_clone,
    24  		_push,
    25  		_pull,
    26  		_checkout,
    27  		_import,
    28  	} {
    29  		if len(cmd.Subcommands) > 0 {
    30  			app.Commands = append(app.Commands, cmd)
    31  		}
    32  	}
    33  }
    34  
    35  func attachCommand(cmd Basic) {
    36  	baseCmd, baseOps := cmd.Base()
    37  
    38  	for _cmd, method := range map[*cli.Command]func() Command{
    39  		_new:      cmd.New,
    40  		_edit:     cmd.Edit,
    41  		_delete:   cmd.Delete,
    42  		_query:    cmd.Query,
    43  		_list:     cmd.List,
    44  		_select:   cmd.Select,
    45  		_import:   cmd.Import,
    46  		_clone:    cmd.Clone,
    47  		_push:     cmd.Push,
    48  		_pull:     cmd.Pull,
    49  		_checkout: cmd.Checkout,
    50  	} {
    51  		_method := method()
    52  		if _method != NotImplemented {
    53  			cliCmd := _method.Initialize(_cmd, baseCmd, baseOps)
    54  			if _cmd == _list {
    55  				pluralAlias(cliCmd)
    56  			}
    57  
    58  			_cmd.Subcommands = append(_cmd.Subcommands, cliCmd)
    59  		}
    60  	}
    61  }
    62  
    63  func pluralAlias(command *cli.Command) {
    64  	if command.Aliases == nil {
    65  		command.Aliases = make([]string, 0)
    66  	}
    67  
    68  	switch command.Name {
    69  	case "messaging", "smartops":
    70  		return
    71  	case "library":
    72  		command.Aliases = append(command.Aliases, "libraries")
    73  	case "application":
    74  		command.Aliases = append(command.Aliases, "apps", "applications")
    75  	default:
    76  		command.Aliases = append(command.Aliases, command.Name+"s")
    77  	}
    78  }