Skip to content

Commit

Permalink
flatten cli (micro#2332)
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Nov 1, 2021
1 parent 3c70d23 commit 2b9a6f9
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 75 deletions.
6 changes: 3 additions & 3 deletions cmd/micro/cmd/cli/call/call.go → cmd/micro/cli/call/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/micro/cmd/cmd.go → cmd/micro/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"fmt"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions cmd/micro/cmd/cli/new/new.go → cmd/micro/cli/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/micro/cmd/cli/run/run.go → cmd/micro/cli/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
})
Expand Down Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions cmd/micro/cli/stream/stream.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}
11 changes: 0 additions & 11 deletions cmd/micro/cmd/cli/cli.go

This file was deleted.

27 changes: 0 additions & 27 deletions cmd/micro/cmd/cli/stream/stream.go

This file was deleted.

12 changes: 9 additions & 3 deletions cmd/micro/main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 2b9a6f9

Please sign in to comment.