2020-07-19 17:20:05 +01:00
|
|
|
// Package cmd is an interface for building a command line binary
|
2015-02-14 23:33:21 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-07-27 13:22:00 +01:00
|
|
|
"context"
|
2015-02-14 23:33:21 +00:00
|
|
|
|
2020-07-19 19:48:11 +01:00
|
|
|
"github.com/micro/cli/v2"
|
2015-02-14 23:33:21 +00:00
|
|
|
)
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
// TODO: replace App with RegisterCommand/RegisterFlags
|
2016-01-01 01:16:21 +00:00
|
|
|
type Cmd interface {
|
2020-07-19 18:14:18 +01:00
|
|
|
// Init initialises options
|
|
|
|
// Note: Use Run to parse command line
|
2020-07-19 17:20:05 +01:00
|
|
|
Init(opts ...Option) error
|
2016-01-01 01:16:21 +00:00
|
|
|
// Options set within this command
|
|
|
|
Options() Options
|
2020-07-19 18:14:18 +01:00
|
|
|
// The cli app within this cmd
|
|
|
|
App() *cli.App
|
|
|
|
// Run executes the command
|
|
|
|
Run() error
|
2020-07-19 17:20:05 +01:00
|
|
|
// Implementation
|
|
|
|
String() string
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
type Options struct {
|
|
|
|
// Name of the application
|
|
|
|
Name string
|
|
|
|
// Description of the application
|
|
|
|
Description string
|
|
|
|
// Version of the application
|
|
|
|
Version string
|
|
|
|
// Action to execute when Run is called and there is no subcommand
|
|
|
|
// TODO replace with a build in context
|
|
|
|
Action func(*cli.Context) error
|
|
|
|
// TODO replace with built in command definition
|
|
|
|
Commands []*cli.Command
|
|
|
|
// TODO replace with built in flags definition
|
|
|
|
Flags []cli.Flag
|
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-05 19:25:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
// Command line Name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
2020-01-25 13:41:25 +03:00
|
|
|
}
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
// Command line Description
|
|
|
|
func Description(d string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Description = d
|
2020-03-12 18:13:03 +00:00
|
|
|
}
|
2015-04-27 22:21:56 +01:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
// Command line Version
|
|
|
|
func Version(v string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Version = v
|
2020-01-19 00:55:01 +00:00
|
|
|
}
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
2015-04-27 22:21:56 +01:00
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
// Commands to add
|
|
|
|
func Commands(c ...*cli.Command) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Commands = c
|
|
|
|
}
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
// Flags to add
|
|
|
|
func Flags(f ...cli.Flag) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Flags = f
|
|
|
|
}
|
2015-02-14 23:33:21 +00:00
|
|
|
}
|
2020-07-19 18:43:33 +01:00
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
// Action to execute
|
|
|
|
func Action(a func(*cli.Context) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Action = a
|
|
|
|
}
|
2020-07-19 18:43:33 +01:00
|
|
|
}
|