github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/internal/command/api.go (about)

     1  package command
     2  
     3  type CommandInfo interface {
     4  	Name() string
     5  
     6  	Type() string
     7  
     8  	Category() string
     9  
    10  	Group() string
    11  
    12  	ArgsUsage() string
    13  
    14  	Examples() []ExampleEntry
    15  
    16  	ShortDescription() string
    17  
    18  	LongDescription() string
    19  
    20  	DocFile() string
    21  
    22  	DocLink() string
    23  
    24  	RequestedResources() []string
    25  }
    26  
    27  type CommandManifest interface {
    28  	CommandInfo
    29  
    30  	Executable() string
    31  
    32  	Arguments() []string
    33  
    34  	ValidArgs() []string
    35  
    36  	ValidArgsCmd() []string
    37  
    38  	// deprecated in 1.9.0, replaced by Flags()
    39  	RequiredFlags() []string
    40  
    41  	Flags() []Flag
    42  
    43  	ExclusiveFlags() [][]string
    44  
    45  	GroupFlags() [][]string
    46  
    47  	FlagValuesCmd() []string
    48  
    49  	CheckFlags() bool
    50  }
    51  
    52  type Command interface {
    53  	CommandManifest
    54  
    55  	// the id of the reigstry that the command belongs to
    56  	RepositoryID() string
    57  	// the package name that the command belongs to
    58  	PackageName() string
    59  	// the full ID of the command: registry:package:group:name
    60  	ID() string
    61  	// the full group name: registry:package:group
    62  	FullGroup() string
    63  	// the full command name: registry:package:group:name
    64  	FullName() string
    65  	// the runtime group of the command
    66  	RuntimeGroup() string
    67  	// the runtime name of the command
    68  	RuntimeName() string
    69  	// the package directory
    70  	PackageDir() string
    71  
    72  	Execute(envVars []string, args ...string) (int, error)
    73  
    74  	ExecuteWithOutput(envVars []string, args ...string) (int, string, error)
    75  
    76  	ExecuteValidArgsCmd(envVars []string, args ...string) (int, string, error)
    77  
    78  	ExecuteFlagValuesCmd(envVars []string, flagCmd []string, args ...string) (int, string, error)
    79  
    80  	// namespace speficies the package and the registry/repository of the command
    81  	// there could be two commands with the same group and name in different namespace
    82  	// when resolving the group and name conflict, namespace is used to identify the
    83  	// command
    84  	SetNamespace(regId string, pkgName string)
    85  
    86  	SetPackageDir(pkgDir string)
    87  
    88  	SetRuntimeGroup(alias string)
    89  
    90  	SetRuntimeName(alias string)
    91  }
    92  
    93  type PackageManifest interface {
    94  	Name() string
    95  
    96  	Version() string
    97  
    98  	Commands() []Command
    99  }
   100  
   101  type Package interface {
   102  	PackageManifest
   103  
   104  	// repository ID: dropin, default, repo1, repo2, ...
   105  	RepositoryID() string
   106  
   107  	// verify the sha256 checksum
   108  	VerifyChecksum(checksum string) (bool, error)
   109  
   110  	// verify the package signature
   111  	VerifySignature(signature string) (bool, error)
   112  
   113  	// install package to a local repository
   114  	InstallTo(pathname string) (PackageManifest, error)
   115  
   116  	// run setup process of the package
   117  	RunSetup(pkgDir string) error
   118  }
   119  
   120  type ExampleEntry struct {
   121  	Scenario string `json:"scenario" yaml:"scenario"`
   122  	Command  string `json:"cmd" yaml:"cmd"`
   123  }
   124  
   125  func (example ExampleEntry) Clone() ExampleEntry {
   126  	return ExampleEntry{
   127  		Scenario: example.Scenario,
   128  		Command:  example.Command,
   129  	}
   130  }
   131  
   132  type Flag struct {
   133  	FlagName        string   `json:"name" yaml:"name"`
   134  	FlagType        string   `json:"type" yaml:"type"`
   135  	FlagShortName   string   `json:"short" yaml:"short"`
   136  	FlagDescription string   `json:"desc" yaml:"desc"`
   137  	FlagDefault     string   `json:"default" yaml:"default"`
   138  	FlagRequired    bool     `json:"required" yaml:"required"`
   139  	FlagValues      []string `json:"values" yaml:"values"`
   140  	FlagValuesCmd   []string `json:"valuesCmd" yaml:"valuesCmd"`
   141  }
   142  
   143  func (f Flag) Name() string {
   144  	return f.FlagName
   145  }
   146  
   147  func (f Flag) Type() string {
   148  	if f.FlagType != "string" && f.FlagType != "bool" {
   149  		return "string"
   150  	}
   151  	return f.FlagType
   152  }
   153  
   154  func (f Flag) ShortName() string {
   155  	return f.FlagShortName
   156  }
   157  
   158  func (f Flag) Description() string {
   159  	return f.FlagDescription
   160  }
   161  
   162  func (f Flag) Required() bool {
   163  	return f.FlagRequired
   164  }
   165  
   166  func (f Flag) Default() string {
   167  	if f.FlagType == "bool" {
   168  		return "false"
   169  	}
   170  	return f.FlagDefault
   171  }
   172  
   173  func (f Flag) Values() []string {
   174  	if f.FlagValues != nil && len(f.FlagValues) > 0 {
   175  		return f.FlagValues
   176  	}
   177  	return []string{}
   178  }
   179  
   180  func (f Flag) ValuesCmd() []string {
   181  	if f.FlagValuesCmd != nil && len(f.FlagValuesCmd) > 0 {
   182  		return f.FlagValuesCmd
   183  	}
   184  	return []string{}
   185  }