Skip to content

Commit

Permalink
Replace urfavecli with Cobra
Browse files Browse the repository at this point in the history
Closes vmware-archive#18

Signed-off-by: Josh Kim <kjosh@vmware.com>
  • Loading branch information
jooskim committed Aug 16, 2021
1 parent 06ac948 commit e62d15d
Show file tree
Hide file tree
Showing 9 changed files with 757 additions and 294 deletions.
2 changes: 1 addition & 1 deletion plank/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ something that looks more informational or more appealing at least.
|----|----|----|----|----|
|--hostname|-n|localhost|false|Hostname where Plank is to be served. Also reads from `$PLANK_SERVER_HOSTNAME` environment variable|
|--port|-p|30080|false|Port where Plank is to be served. Also reads from `$PLANK_SERVER_PORT` environment variable|
|--rootDir|-r|<current directory>|false|Root directory for the server. Also reads from `$PLANK_SERVER_ROOT` environment variable|
|--rootdir|-r|<current directory>|false|Root directory for the server. Also reads from `$PLANK_SERVER_ROOTDIR` environment variable|
|--static|-s|-|false|Path to a location where static files will be served. Can be used multiple times|
|--no-fabric-broker|-|false|false|Do not start Fabric broker|
|--fabric-endpoint|-|/fabric|false|Fabric broker endpoint (ignored if --no-fabric-broker is present)|
Expand Down
84 changes: 39 additions & 45 deletions plank/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package main

import (
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/vmware/transport-go/plank/pkg/server"
"github.com/vmware/transport-go/plank/services"
"github.com/vmware/transport-go/plank/utils"
Expand All @@ -15,54 +15,48 @@ var version string
var platformServer *server.PlatformServer

func main() {
app := cli.NewApp()
app.Name = "Plank"
app.Version = version
app.Description = "Plank"
app.Commands = []cli.Command{
{
Name: "start-server",
Usage: "Start server",
Flags: utils.UrfaveCLIFlags,
Action: func(c *cli.Context) error {
var platformServer server.PlatformServer
var err error
configPath := c.String("config-file")
var serverConfig *server.PlatformServerConfig

// if server config file is available create a server instance with it. otherwise, create it with a
// server config with provided CLI flag values
if len(configPath) > 0 {
platformServer, err = server.NewPlatformServerFromConfig(configPath)
if err != nil {
return err
}
} else {
serverConfig, err := server.CreateServerConfigFromUrfaveCLIContext(c)
if err != nil {
return err
}
platformServer = server.NewPlatformServer(serverConfig)
}

// register services
if err := platformServer.RegisterService(services.NewPingPongService(), services.PingPongServiceChan); err != nil {
panic(err)
}
if err := platformServer.RegisterService(services.NewStockTickerService(), services.StockTickerServiceChannel); err != nil {
panic(err)
}

// start server
syschan := make(chan os.Signal, 1)
platformServer.StartServer(syschan)
// define the root command - entry of our application
app := &cobra.Command{
Use: "plank",
Version: version,
Short: "Plank demo application",
}

return nil
},
// define a command that starts the Plank server
startCmd := &cobra.Command{
Use: "start-server",
Short: "Start Plank server",
RunE: func(cmd *cobra.Command, args []string) error {
var platformServer server.PlatformServer
platformServer = server.NewPlatformServer(serverConfig)

// register services
if err := platformServer.RegisterService(services.NewPingPongService(), services.PingPongServiceChan); err != nil {
return err
}
if err := platformServer.RegisterService(services.NewStockTickerService(), services.StockTickerServiceChannel); err != nil {
return err
}

// start server
syschan := make(chan os.Signal, 1)
platformServer.StartServer(syschan)
return nil
},
}

err := app.Run(os.Args)
if err != nil {
panic(err)
// create a new server configuration. this Cobra variant of the server.CreateServerConfig() function
// configures and parses flags from the command line arguments into Cobra Command's structure. otherwise,
// it is identical to server.CreateServerConfig() which you can use if you don't want to use Cobra.
serverConfig, err := server.CreateServerConfigForCobraCommand(startCmd)

// add startCmd command to app
app.AddCommand(startCmd)

// start the app
if err = app.Execute(); err != nil {
utils.Log.Fatalln(err)
}
}
12 changes: 9 additions & 3 deletions plank/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module github.com/vmware/transport-go/plank
go 1.16

require (
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
github.com/coreos/etcd v3.3.10+incompatible // indirect
github.com/coreos/go-etcd v2.0.0+incompatible // indirect
github.com/cpuguy83/go-md2man v1.0.10 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/fatih/color v1.12.0
github.com/go-git/go-git/v5 v5.4.2
Expand All @@ -16,16 +20,18 @@ require (
github.com/prometheus/client_golang v1.6.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
github.com/sirupsen/logrus v1.8.0
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
github.com/streadway/amqp v1.0.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/urfave/cli v1.22.5
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 // indirect
github.com/vmware/transport-go v1.2.0
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/tools v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

Expand Down
Loading

0 comments on commit e62d15d

Please sign in to comment.