micro/cmd/cmd.go

87 lines
1.7 KiB
Go
Raw Normal View History

2020-07-19 17:20:05 +01:00
// Package cmd is an interface for building a command line binary
package cmd
import (
"context"
2020-07-19 19:48:11 +01:00
"github.com/micro/cli/v2"
)
// 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)
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
}
// Command line Name
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
2016-01-01 01:16:21 +00:00
}
// Command line Description
func Description(d string) Option {
return func(o *Options) {
o.Description = d
}
}
// 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
}
// Commands to add
func Commands(c ...*cli.Command) Option {
return func(o *Options) {
o.Commands = c
}
2016-01-01 01:16:21 +00:00
}
// Flags to add
func Flags(f ...cli.Flag) Option {
return func(o *Options) {
o.Flags = f
}
}
2020-07-19 18:43:33 +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
}