Skip to content

Commit

Permalink
[CLI] upgrade urfave/cli to v2 (#6285)
Browse files Browse the repository at this point in the history
What changed?

changed GlobalFlag to normal flags as it's no longer supported
Flag aliases are separated into a separate field
populate error for CLI actions
Why?

V2 introduced a background context field in cli.Context which can be used to propagate traces and security tokens in a centralized way.

How did you test it?
Unit test

Potential risks

Release notes

Documentation Changes

Before

shengs@shengs-DQG34F2RHF cli % cadence --env development --do not-exist-domain domain describe
Error: Domain not-exist-domain does not exist.
Error Details: Domain not-exist-domain does not exist.
('export CADENCE_CLI_SHOW_STACKS=1' to see stack traces)
After

shengs@shengs-DQG34F2RHF cli % go run main.go --do not-exist-domain domain describe
Error: Domain not-exist-domain does not exist.
Error Details: Domain not-exist-domain does not exist.
('export CADENCE_CLI_SHOW_STACKS=1' to see stack traces)
Domain not-exist-domain does not exist.
exit status 1
  • Loading branch information
shijiesheng authored Sep 27, 2024
1 parent 02984fd commit d19937f
Show file tree
Hide file tree
Showing 56 changed files with 2,763 additions and 2,415 deletions.
84 changes: 43 additions & 41 deletions cmd/bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,24 @@
package main

import (
"fmt"
"log"
"os"
"path"
"strings"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/uber/cadence/bench"
"github.com/uber/cadence/bench/lib"
"github.com/uber/cadence/common/config"
)

const (
flagRoot = "root"
flagRootWithAlias = flagRoot + ", r"
flagConfig = "config"
flagConfigWithAlias = flagConfig + ", c"
flagEnv = "env"
flagEnvWithAlias = flagEnv + ", e"
flagZone = "zone"
flagZoneWithAlias = flagZone + ", az"
flagRoot = "root"
flagConfig = "config"
flagEnv = "env"
flagZone = "zone"
)

const (
Expand All @@ -51,7 +48,7 @@ const (
defaultZone = ""
)

func startHandler(c *cli.Context) {
func startHandler(c *cli.Context) error {
env := getEnvironment(c)
zone := getZone(c)
configDir := getConfigDir(c)
Expand All @@ -60,25 +57,26 @@ func startHandler(c *cli.Context) {

var cfg lib.Config
if err := config.Load(env, configDir, zone, &cfg); err != nil {
log.Fatal("Failed to load config file: ", err)
return fmt.Errorf("failed to load config file: %w", err)
}

if err := cfg.Validate(); err != nil {
log.Fatal("Invalid config: ", err)
return fmt.Errorf("invalid config: %w", err)
}

benchWorker, err := bench.NewWorker(&cfg)
if err != nil {
log.Fatal("Failed to initialize bench worker: ", err)
return fmt.Errorf("failed to initialize bench worker: %w", err)
}

if err := benchWorker.Run(); err != nil {
log.Fatal("Failed to run bench worker: ", err)
return fmt.Errorf("failed to run bench worker: %w", err)
}
return nil
}

func getRootDir(c *cli.Context) string {
rootDir := c.GlobalString(flagRoot)
rootDir := c.String(flagRoot)
if len(rootDir) == 0 {
var err error
if rootDir, err = os.Getwd(); err != nil {
Expand All @@ -90,16 +88,16 @@ func getRootDir(c *cli.Context) string {

func getConfigDir(c *cli.Context) string {
rootDir := getRootDir(c)
configDir := c.GlobalString(flagConfig)
configDir := c.String(flagConfig)
return path.Join(rootDir, configDir)
}

func getEnvironment(c *cli.Context) string {
return strings.TrimSpace(c.GlobalString(flagEnv))
return strings.TrimSpace(c.String(flagEnv))
}

func getZone(c *cli.Context) string {
return strings.TrimSpace(c.GlobalString(flagZone))
return strings.TrimSpace(c.String(flagZone))
}

func buildCLI() *cli.App {
Expand All @@ -109,38 +107,42 @@ func buildCLI() *cli.App {
app.Version = "0.0.1"

app.Flags = []cli.Flag{
cli.StringFlag{
Name: flagRootWithAlias,
Value: defaultRoot,
Usage: "root directory of execution environment",
EnvVar: lib.EnvKeyRoot,
&cli.StringFlag{
Name: flagRoot,
Aliases: []string{"r"},
Value: defaultRoot,
Usage: "root directory of execution environment",
EnvVars: []string{lib.EnvKeyRoot},
},
cli.StringFlag{
Name: flagConfigWithAlias,
Value: defaultConfig,
Usage: "config dir path relative to root",
EnvVar: lib.EnvKeyConfigDir,
&cli.StringFlag{
Name: flagConfig,
Aliases: []string{"c"},
Value: defaultConfig,
Usage: "config dir path relative to root",
EnvVars: []string{lib.EnvKeyConfigDir},
},
cli.StringFlag{
Name: flagEnvWithAlias,
Value: defaultEnv,
Usage: "runtime environment",
EnvVar: lib.EnvKeyEnvironment,
&cli.StringFlag{
Name: flagEnv,
Aliases: []string{"e"},
Value: defaultEnv,
Usage: "runtime environment",
EnvVars: []string{lib.EnvKeyEnvironment},
},
cli.StringFlag{
Name: flagZoneWithAlias,
Value: defaultZone,
Usage: "availability zone",
EnvVar: lib.EnvKeyAvailabilityZone,
&cli.StringFlag{
Name: flagZone,
Aliases: []string{"z"},
Value: defaultZone,
Usage: "availability zone",
EnvVars: []string{lib.EnvKeyAvailabilityZone},
},
}

app.Commands = []cli.Command{
app.Commands = []*cli.Command{
{
Name: "start",
Usage: "start cadence bench worker",
Action: func(c *cli.Context) {
startHandler(c)
Action: func(c *cli.Context) error {
return startHandler(c)
},
},
}
Expand Down
82 changes: 44 additions & 38 deletions cmd/canary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
"path"
"strings"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/uber/cadence/canary"
"github.com/uber/cadence/common/config"
)

func startHandler(c *cli.Context) {
func startHandler(c *cli.Context) error {
env := getEnvironment(c)
zone := getZone(c)
configDir := getConfigDir(c)
Expand All @@ -42,26 +42,27 @@ func startHandler(c *cli.Context) {

var cfg canary.Config
if err := config.Load(env, configDir, zone, &cfg); err != nil {
log.Fatal("Failed to load config file: ", err)
return fmt.Errorf("failed to load config file: %w", err)
}

if err := cfg.Validate(); err != nil {
log.Fatal("Invalid config: ", err)
return fmt.Errorf("invalid config: %w", err)
}

mode := c.String("mode")
canary, err := canary.NewCanaryRunner(&cfg)
if err != nil {
log.Fatal("Failed to initialize canary: ", err)
return fmt.Errorf("failed to initialize canary: %w", err)
}

if err := canary.Run(mode); err != nil {
log.Fatal("Failed to run canary: ", err)
return fmt.Errorf("failed to run canary: %w", err)
}
return nil
}

func getRootDir(c *cli.Context) string {
rootDir := c.GlobalString("root")
rootDir := c.String("root")
if len(rootDir) == 0 {
var err error
if rootDir, err = os.Getwd(); err != nil {
Expand All @@ -73,16 +74,16 @@ func getRootDir(c *cli.Context) string {

func getConfigDir(c *cli.Context) string {
rootDir := getRootDir(c)
configDir := c.GlobalString("config")
configDir := c.String("config")
return path.Join(rootDir, configDir)
}

func getEnvironment(c *cli.Context) string {
return strings.TrimSpace(c.GlobalString("env"))
return strings.TrimSpace(c.String("env"))
}

func getZone(c *cli.Context) string {
return strings.TrimSpace(c.GlobalString("zone"))
return strings.TrimSpace(c.String("zone"))
}

func buildCLI() *cli.App {
Expand All @@ -92,46 +93,51 @@ func buildCLI() *cli.App {
app.Version = "0.0.1"

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "root, r",
Value: ".",
Usage: "root directory of execution environment",
EnvVar: canary.EnvKeyRoot,
&cli.StringFlag{
Name: "root",
Aliases: []string{"r"},
Value: ".",
Usage: "root directory of execution environment",
EnvVars: []string{canary.EnvKeyRoot},
},
cli.StringFlag{
Name: "config, c",
Value: "config/canary",
Usage: "config dir path relative to root",
EnvVar: canary.EnvKeyConfigDir,
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: "config/canary",
Usage: "config dir path relative to root",
EnvVars: []string{canary.EnvKeyConfigDir},
},
cli.StringFlag{
Name: "env, e",
Value: "development",
Usage: "runtime environment",
EnvVar: canary.EnvKeyEnvironment,
&cli.StringFlag{
Name: "env",
Aliases: []string{"e"},
Value: "development",
Usage: "runtime environment",
EnvVars: []string{canary.EnvKeyEnvironment},
},
cli.StringFlag{
Name: "zone, az",
Value: "",
Usage: "availability zone",
EnvVar: canary.EnvKeyAvailabilityZone,
&cli.StringFlag{
Name: "zone",
Aliases: []string{"az"},
Value: "",
Usage: "availability zone",
EnvVars: []string{canary.EnvKeyAvailabilityZone},
},
}

app.Commands = []cli.Command{
app.Commands = []*cli.Command{
{
Name: "start",
Usage: "start cadence canary worker or cron, or both",
Flags: []cli.Flag{
cli.StringFlag{
Name: "mode, m",
Value: canary.ModeAll,
Usage: fmt.Sprintf("%v, %v or %v", canary.ModeWorker, canary.ModeCronCanary, canary.ModeAll),
EnvVar: canary.EnvKeyMode,
&cli.StringFlag{
Name: "mode",
Aliases: []string{"m"},
Value: canary.ModeAll,
Usage: fmt.Sprintf("%v, %v or %v", canary.ModeWorker, canary.ModeCronCanary, canary.ModeAll),
EnvVars: []string{canary.EnvKeyMode},
},
},
Action: func(c *cli.Context) {
startHandler(c)
Action: func(c *cli.Context) error {
return startHandler(c)
},
},
}
Expand Down
Loading

0 comments on commit d19937f

Please sign in to comment.