Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] upgrade urfave/cli to v2 #6285

Merged
merged 16 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading