1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-05-25 21:53:14 +02:00

119 lines
2.4 KiB
Go
Raw Normal View History

2021-11-01 14:34:09 +00:00
package cli
2021-08-30 16:48:57 +02:00
import (
"fmt"
"os"
"github.com/urfave/cli/v2"
mcmd "go-micro.dev/v4/cmd"
2021-08-30 16:48:57 +02:00
)
var (
2021-11-01 14:34:09 +00:00
// DefaultCLI is the default, unmodified root command.
DefaultCLI CLI = NewCLI()
2021-08-30 16:48:57 +02:00
name string = "micro"
2021-08-30 16:48:57 +02:00
description string = "The Go Micro CLI tool"
version string = "latest"
)
2021-11-01 14:34:09 +00:00
// CLI is the interface that wraps the cli app.
2021-08-30 16:48:57 +02:00
//
2021-11-01 14:34:09 +00:00
// CLI embeds the Cmd interface from the go-micro.dev/v4/cmd
2021-08-30 16:48:57 +02:00
// package and adds a Run method.
//
// Run runs the cli app within this command and exits on error.
2021-11-01 14:34:09 +00:00
type CLI interface {
2021-08-30 16:48:57 +02:00
mcmd.Cmd
Run() error
}
type cmd struct {
app *cli.App
opts mcmd.Options
}
// App returns the cli app within this command.
func (c *cmd) App() *cli.App {
return c.app
}
// Options returns the options set within this command.
func (c *cmd) Options() mcmd.Options {
return c.opts
}
// Init adds options, parses flags and exits on error.
func (c *cmd) Init(opts ...mcmd.Option) error {
return mcmd.Init(opts...)
}
// Run runs the cli app within this command and exits on error.
func (c *cmd) Run() error {
return c.app.Run(os.Args)
}
// DefaultOptions returns the options passed to the default command.
func DefaultOptions() mcmd.Options {
2021-11-01 14:34:09 +00:00
return DefaultCLI.Options()
2021-08-30 16:48:57 +02:00
}
// App returns the cli app within the default command.
func App() *cli.App {
2021-11-01 14:34:09 +00:00
return DefaultCLI.App()
2021-08-30 16:48:57 +02:00
}
// Register appends commands to the default app.
func Register(cmds ...*cli.Command) {
2021-11-01 14:34:09 +00:00
app := DefaultCLI.App()
2021-08-30 16:48:57 +02:00
app.Commands = append(app.Commands, cmds...)
}
// Run runs the cli app within the default command. On error, it prints the
// error message and exits.
func Run() {
2021-11-01 14:34:09 +00:00
if err := DefaultCLI.Run(); err != nil {
2021-08-30 16:48:57 +02:00
fmt.Println(err.Error())
os.Exit(1)
}
}
2021-11-01 14:34:09 +00:00
// NewCLI returns a new command.
func NewCLI(opts ...mcmd.Option) CLI {
2021-08-30 16:48:57 +02:00
options := mcmd.DefaultOptions()
// Clear the name, version and description parameters from the default
// options so the options passed may override them.
options.Name = ""
options.Version = ""
options.Description = ""
for _, o := range opts {
o(&options)
}
if len(options.Name) == 0 {
options.Name = name
}
if len(options.Description) == 0 {
options.Description = description
}
if len(options.Version) == 0 {
options.Version = version
}
c := new(cmd)
c.opts = options
c.app = cli.NewApp()
c.app.Name = c.opts.Name
c.app.Usage = c.opts.Description
c.app.Version = c.opts.Version
c.app.Flags = mcmd.DefaultFlags
if len(options.Version) == 0 {
c.app.HideVersion = true
}
return c
}