1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-09-16 08:36:30 +02:00

flatten cli (#2332)

This commit is contained in:
Asim Aslam
2021-11-01 14:34:09 +00:00
committed by GitHub
parent 3c70d23a1d
commit 2b9a6f9aeb
14 changed files with 70 additions and 75 deletions

View File

@@ -9,13 +9,13 @@ import (
"github.com/urfave/cli/v2"
"go-micro.dev/v4"
"go-micro.dev/v4/client"
"go-micro.dev/v4/cmd/micro/cmd"
mcli "go-micro.dev/v4/cmd/micro/cli"
)
func init() {
cmd.Register(&cli.Command{
mcli.Register(&cli.Command{
Name: "call",
Usage: "Call a service, e.g. " + cmd.App().Name + " call helloworld Helloworld.Call '{\"name\": \"John\"}'",
Usage: "Call a service, e.g. " + mcli.App().Name + " call helloworld Helloworld.Call '{\"name\": \"John\"}'",
Action: RunCall,
})
}

View File

@@ -1,4 +1,4 @@
package cmd
package cli
import (
"fmt"
@@ -9,21 +9,21 @@ import (
)
var (
// DefaultCmd is the default, unmodified root command.
DefaultCmd Cmd = NewCmd()
// DefaultCLI is the default, unmodified root command.
DefaultCLI CLI = NewCLI()
name string = "micro"
description string = "The Go Micro CLI tool"
version string = "latest"
)
// Cmd is the interface that wraps the cli app.
// CLI is the interface that wraps the cli app.
//
// Cmd embeds the Cmd interface from the go-micro.dev/v4/cmd
// CLI embeds the Cmd interface from the go-micro.dev/v4/cmd
// package and adds a Run method.
//
// Run runs the cli app within this command and exits on error.
type Cmd interface {
type CLI interface {
mcmd.Cmd
Run() error
}
@@ -55,31 +55,31 @@ func (c *cmd) Run() error {
// DefaultOptions returns the options passed to the default command.
func DefaultOptions() mcmd.Options {
return DefaultCmd.Options()
return DefaultCLI.Options()
}
// App returns the cli app within the default command.
func App() *cli.App {
return DefaultCmd.App()
return DefaultCLI.App()
}
// Register appends commands to the default app.
func Register(cmds ...*cli.Command) {
app := DefaultCmd.App()
app := DefaultCLI.App()
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() {
if err := DefaultCmd.Run(); err != nil {
if err := DefaultCLI.Run(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
// NewCmd returns a new command.
func NewCmd(opts ...mcmd.Option) Cmd {
// NewCLI returns a new command.
func NewCLI(opts ...mcmd.Option) CLI {
options := mcmd.DefaultOptions()
// Clear the name, version and description parameters from the default

View File

@@ -2,7 +2,7 @@ package describe
import (
"github.com/urfave/cli/v2"
"go-micro.dev/v4/cmd/micro/cmd"
mcli "go-micro.dev/v4/cmd/micro/cli"
)
var flags []cli.Flag = []cli.Flag{
@@ -14,14 +14,14 @@ var flags []cli.Flag = []cli.Flag{
}
func init() {
cmd.Register(&cli.Command{
mcli.Register(&cli.Command{
Name: "describe",
Usage: "Describe a resource",
Subcommands: []*cli.Command{
{
Name: "service",
Aliases: []string{"s"},
Usage: "Describe a service resource, e.g. " + cmd.App().Name + " describe service helloworld",
Usage: "Describe a service resource, e.g. " + mcli.App().Name + " describe service helloworld",
Action: Service,
Flags: flags,
},

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/urfave/cli/v2"
"go-micro.dev/v4/cmd/micro/cmd"
mcli "go-micro.dev/v4/cmd/micro/cli"
"gopkg.in/yaml.v2"
)
@@ -20,7 +20,7 @@ func Service(ctx *cli.Context) error {
return cli.ShowSubcommandHelp(ctx)
}
r := *cmd.DefaultOptions().Registry
r := *mcli.DefaultOptions().Registry
srvs, err := r.GetService(args[0])
if err != nil {
return err

View File

@@ -7,13 +7,13 @@ import (
"strings"
"github.com/urfave/cli/v2"
"go-micro.dev/v4/cmd/micro/cmd"
mcli "go-micro.dev/v4/cmd/micro/cli"
"go-micro.dev/v4/cmd/micro/generator"
tmpl "go-micro.dev/v4/cmd/micro/generator/template"
)
func init() {
cmd.Register(&cli.Command{
mcli.Register(&cli.Command{
Name: "generate",
Usage: "Generate project template files after the fact",
Subcommands: []*cli.Command{

View File

@@ -7,7 +7,7 @@ import (
"strings"
"github.com/urfave/cli/v2"
"go-micro.dev/v4/cmd/micro/cmd"
mcli "go-micro.dev/v4/cmd/micro/cli"
"go-micro.dev/v4/cmd/micro/generator"
tmpl "go-micro.dev/v4/cmd/micro/generator/template"
)
@@ -29,25 +29,25 @@ var flags []cli.Flag = []cli.Flag{
// NewCommand returns a new new cli command.
func init() {
cmd.Register(&cli.Command{
mcli.Register(&cli.Command{
Name: "new",
Usage: "Create a project template",
Subcommands: []*cli.Command{
{
Name: "client",
Usage: "Create a client template, e.g. " + cmd.App().Name + " new client [github.com/auditemarlow/]helloworld",
Usage: "Create a client template, e.g. " + mcli.App().Name + " new client [github.com/auditemarlow/]helloworld",
Action: Client,
Flags: flags,
},
{
Name: "function",
Usage: "Create a function template, e.g. " + cmd.App().Name + " new function [github.com/auditemarlow/]helloworld",
Usage: "Create a function template, e.g. " + mcli.App().Name + " new function [github.com/auditemarlow/]helloworld",
Action: Function,
Flags: flags,
},
{
Name: "service",
Usage: "Create a service template, e.g. " + cmd.App().Name + " new service [github.com/auditemarlow/]helloworld",
Usage: "Create a service template, e.g. " + mcli.App().Name + " new service [github.com/auditemarlow/]helloworld",
Action: Service,
Flags: flags,
},

View File

@@ -9,7 +9,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"
"go-micro.dev/v4/cmd/micro/cmd"
mcli "go-micro.dev/v4/cmd/micro/cli"
"go-micro.dev/v4/runtime"
"go-micro.dev/v4/runtime/local/git"
)
@@ -34,9 +34,9 @@ var (
)
func init() {
cmd.Register(&cli.Command{
mcli.Register(&cli.Command{
Name: "run",
Usage: "Build and run a service continuously, e.g. " + cmd.App().Name + " run [github.com/auditemarlow/helloworld]",
Usage: "Build and run a service continuously, e.g. " + mcli.App().Name + " run [github.com/auditemarlow/helloworld]",
Flags: flags,
Action: Run,
})
@@ -66,7 +66,7 @@ func Run(ctx *cli.Context) error {
command := strings.TrimSpace(ctx.String("command"))
args := strings.TrimSpace(ctx.String("args"))
r := *cmd.DefaultCmd.Options().Runtime
r := *mcli.DefaultCLI.Options().Runtime
var retries = DefaultRetries
if ctx.IsSet("retries") {

View File

@@ -5,11 +5,11 @@ import (
"sort"
"github.com/urfave/cli/v2"
"go-micro.dev/v4/cmd/micro/cmd"
mcli "go-micro.dev/v4/cmd/micro/cli"
)
func init() {
cmd.Register(&cli.Command{
mcli.Register(&cli.Command{
Name: "services",
Usage: "List services in the registry",
Action: List,
@@ -19,7 +19,7 @@ func init() {
// List fetches running services from the registry and lists them. Exits on
// error.
func List(ctx *cli.Context) error {
r := *cmd.DefaultOptions().Registry
r := *mcli.DefaultOptions().Registry
srvs, err := r.ListServices()
if err != nil {
return err

View File

@@ -0,0 +1,27 @@
package stream
import (
"github.com/urfave/cli/v2"
mcli "go-micro.dev/v4/cmd/micro/cli"
)
func init() {
mcli.Register(&cli.Command{
Name: "stream",
Usage: "Create a service stream",
Subcommands: []*cli.Command{
{
Name: "bidi",
Aliases: []string{"b"},
Usage: "Create a bidirectional service stream, e.g. " + mcli.App().Name + " stream bidirectional helloworld Helloworld.PingPong '{\"stroke\": 1}' '{\"stroke\": 2}'",
Action: Bidirectional,
},
{
Name: "server",
Aliases: []string{"s"},
Usage: "Create a server service stream, e.g. " + mcli.App().Name + " stream server helloworld Helloworld.ServerStream '{\"count\": 10}'",
Action: Server,
},
},
})
}

View File

@@ -1,11 +0,0 @@
package cli
import (
_ "go-micro.dev/v4/cmd/micro/cmd/cli/call"
_ "go-micro.dev/v4/cmd/micro/cmd/cli/describe"
_ "go-micro.dev/v4/cmd/micro/cmd/cli/generate"
_ "go-micro.dev/v4/cmd/micro/cmd/cli/new"
_ "go-micro.dev/v4/cmd/micro/cmd/cli/run"
_ "go-micro.dev/v4/cmd/micro/cmd/cli/services"
_ "go-micro.dev/v4/cmd/micro/cmd/cli/stream"
)

View File

@@ -1,27 +0,0 @@
package stream
import (
"github.com/urfave/cli/v2"
"go-micro.dev/v4/cmd/micro/cmd"
)
func init() {
cmd.Register(&cli.Command{
Name: "stream",
Usage: "Create a service stream",
Subcommands: []*cli.Command{
{
Name: "bidi",
Aliases: []string{"b"},
Usage: "Create a bidirectional service stream, e.g. " + cmd.App().Name + " stream bidirectional helloworld Helloworld.PingPong '{\"stroke\": 1}' '{\"stroke\": 2}'",
Action: Bidirectional,
},
{
Name: "server",
Aliases: []string{"s"},
Usage: "Create a server service stream, e.g. " + cmd.App().Name + " stream server helloworld Helloworld.ServerStream '{\"count\": 10}'",
Action: Server,
},
},
})
}

View File

@@ -1,12 +1,18 @@
package main
import (
"go-micro.dev/v4/cmd/micro/cmd"
"go-micro.dev/v4/cmd/micro/cli"
// register commands
_ "go-micro.dev/v4/cmd/micro/cmd/cli"
_ "go-micro.dev/v4/cmd/micro/cli/call"
_ "go-micro.dev/v4/cmd/micro/cli/describe"
_ "go-micro.dev/v4/cmd/micro/cli/generate"
_ "go-micro.dev/v4/cmd/micro/cli/new"
_ "go-micro.dev/v4/cmd/micro/cli/run"
_ "go-micro.dev/v4/cmd/micro/cli/services"
_ "go-micro.dev/v4/cmd/micro/cli/stream"
)
func main() {
cmd.Run()
cli.Run()
}