Skip to content

Commit

Permalink
Merge pull request #12969 from ptabor/20210514-etcdctl-step-by-step
Browse files Browse the repository at this point in the history
Refactor common code from etcdctl (v2,v3) to pkg/corbautl.
  • Loading branch information
gyuho authored May 14, 2021
2 parents eae7a84 + 00c6090 commit f2bc5ee
Show file tree
Hide file tree
Showing 52 changed files with 553 additions and 276 deletions.
9 changes: 5 additions & 4 deletions etcdctl/ctlv2/command/cluster_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func handleClusterHealth(c *cli.Context) error {

tr, err := getTransport(c)
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

hc := http.Client{
Expand All @@ -67,7 +68,7 @@ func handleClusterHealth(c *cli.Context) error {
ms, err := mi.List(context.TODO())
if err != nil {
fmt.Println("cluster may be unhealthy: failed to list members")
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

for {
Expand Down Expand Up @@ -128,9 +129,9 @@ func handleClusterHealth(c *cli.Context) error {

if !forever {
if healthyMembers == len(ms) {
os.Exit(ExitSuccess)
os.Exit(cobrautl.ExitSuccess)
}
os.Exit(ExitClusterNotHealthy)
os.Exit(cobrautl.ExitClusterNotHealthy)
}

fmt.Printf("\nnext check after 10 second...\n\n")
Expand Down
9 changes: 0 additions & 9 deletions etcdctl/ctlv2/command/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ import (
"go.etcd.io/etcd/client/v2"
)

const (
ExitSuccess = iota
ExitBadArgs
ExitBadConnection
ExitBadAuth
ExitServerError
ExitClusterNotHealthy
)

func handleError(c *cli.Context, code int, err error) {
if c.GlobalString("output") == "json" {
if err, ok := err.(*client.Error); ok {
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/exec_watch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/signal"

"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -50,7 +51,7 @@ func execWatchCommandFunc(c *cli.Context, ki client.KeysAPI) {
argslen := len(args)

if argslen < 2 {
handleError(c, ExitBadArgs, errors.New("key and command to exec required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key and command to exec required"))
}

var (
Expand Down Expand Up @@ -93,7 +94,7 @@ func execWatchCommandFunc(c *cli.Context, ki client.KeysAPI) {
for {
resp, err := w.Next(context.TODO())
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}
if resp.Node.Dir {
fmt.Fprintf(os.Stderr, "Ignored dir %s change\n", resp.Node.Key)
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/get_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewGetCommand returns the CLI command for "get".
Expand All @@ -43,7 +44,7 @@ func NewGetCommand() cli.Command {
// getCommandFunc executes the "get" command.
func getCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}

key := c.Args()[0]
Expand All @@ -54,7 +55,7 @@ func getCommandFunc(c *cli.Context, ki client.KeysAPI) {
resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sorted, Quorum: quorum})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

if resp.Node.Dir {
Expand Down
3 changes: 2 additions & 1 deletion etcdctl/ctlv2/command/ls_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

func NewLsCommand() cli.Command {
Expand Down Expand Up @@ -54,7 +55,7 @@ func lsCommandFunc(c *cli.Context, ki client.KeysAPI) {
resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sort, Recursive: recursive, Quorum: quorum})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

printLs(c, resp)
Expand Down
7 changes: 4 additions & 3 deletions etcdctl/ctlv2/command/mk_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewMakeCommand returns the CLI command for "mk".
Expand All @@ -43,12 +44,12 @@ func NewMakeCommand() cli.Command {
// mkCommandFunc executes the "mk" command.
func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]
value, err := argOrStdin(c.Args(), os.Stdin, 1)
if err != nil {
handleError(c, ExitBadArgs, errors.New("value required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("value required"))
}

ttl := c.Int("ttl")
Expand All @@ -69,7 +70,7 @@ func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
}
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

printResponseKey(resp, c.GlobalString("output"))
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/mkdir_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewMakeDirCommand returns the CLI command for "mkdir".
Expand All @@ -41,7 +42,7 @@ func NewMakeDirCommand() cli.Command {
// mkdirCommandFunc executes the "mkdir" command.
func mkdirCommandFunc(c *cli.Context, ki client.KeysAPI, prevExist client.PrevExistType) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}

key := c.Args()[0]
Expand All @@ -51,7 +52,7 @@ func mkdirCommandFunc(c *cli.Context, ki client.KeysAPI, prevExist client.PrevEx
resp, err := ki.Set(ctx, key, "", &client.SetOptions{TTL: time.Duration(ttl) * time.Second, Dir: true, PrevExist: prevExist})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}
if c.GlobalString("output") != "simple" {
printResponseKey(resp, c.GlobalString("output"))
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/rm_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewRemoveCommand returns the CLI command for "rm".
Expand All @@ -43,7 +44,7 @@ func NewRemoveCommand() cli.Command {
// rmCommandFunc executes the "rm" command.
func rmCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]
recursive := c.Bool("recursive")
Expand All @@ -55,7 +56,7 @@ func rmCommandFunc(c *cli.Context, ki client.KeysAPI) {
resp, err := ki.Delete(ctx, key, &client.DeleteOptions{PrevIndex: uint64(prevIndex), PrevValue: prevValue, Dir: dir, Recursive: recursive})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}
if !resp.Node.Dir || c.GlobalString("output") != "simple" {
printResponseKey(resp, c.GlobalString("output"))
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/rmdir_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewRemoveDirCommand returns the CLI command for "rmdir".
Expand All @@ -37,15 +38,15 @@ func NewRemoveDirCommand() cli.Command {
// rmdirCommandFunc executes the "rmdir" command.
func rmdirCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]

ctx, cancel := contextWithTotalTimeout(c)
resp, err := ki.Delete(ctx, key, &client.DeleteOptions{Dir: true})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

if !resp.Node.Dir || c.GlobalString("output") != "simple" {
Expand Down
7 changes: 4 additions & 3 deletions etcdctl/ctlv2/command/set_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewSetCommand returns the CLI command for "set".
Expand Down Expand Up @@ -50,12 +51,12 @@ func NewSetCommand() cli.Command {
// setCommandFunc executes the "set" command.
func setCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]
value, err := argOrStdin(c.Args(), os.Stdin, 1)
if err != nil {
handleError(c, ExitBadArgs, errors.New("value required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("value required"))
}

ttl := c.Int("ttl")
Expand All @@ -66,7 +67,7 @@ func setCommandFunc(c *cli.Context, ki client.KeysAPI) {
resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevIndex: uint64(prevIndex), PrevValue: prevValue})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

printResponseKey(resp, c.GlobalString("output"))
Expand Down
7 changes: 4 additions & 3 deletions etcdctl/ctlv2/command/update_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewUpdateCommand returns the CLI command for "update".
Expand All @@ -42,12 +43,12 @@ func NewUpdateCommand() cli.Command {
// updateCommandFunc executes the "update" command.
func updateCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]
value, err := argOrStdin(c.Args(), os.Stdin, 1)
if err != nil {
handleError(c, ExitBadArgs, errors.New("value required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("value required"))
}

ttl := c.Int("ttl")
Expand All @@ -56,7 +57,7 @@ func updateCommandFunc(c *cli.Context, ki client.KeysAPI) {
resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevExist})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}

printResponseKey(resp, c.GlobalString("output"))
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/update_dir_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/urfave/cli"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewUpdateDirCommand returns the CLI command for "updatedir".
Expand All @@ -41,15 +42,15 @@ func NewUpdateDirCommand() cli.Command {
// updatedirCommandFunc executes the "updatedir" command.
func updatedirCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]
ttl := c.Int("ttl")
ctx, cancel := contextWithTotalTimeout(c)
resp, err := ki.Set(ctx, key, "", &client.SetOptions{TTL: time.Duration(ttl) * time.Second, Dir: true, PrevExist: client.PrevExist})
cancel()
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}
if c.GlobalString("output") != "simple" {
printResponseKey(resp, c.GlobalString("output"))
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"go.etcd.io/etcd/client/pkg/v3/transport"
"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"

"github.com/bgentry/speakeasy"
"github.com/urfave/cli"
Expand Down Expand Up @@ -242,10 +243,10 @@ func mustNewClient(c *cli.Context) client.Client {
if err == client.ErrNoEndpoints {
fmt.Fprintf(os.Stderr, "etcd cluster has no published client endpoints.\n")
fmt.Fprintf(os.Stderr, "Try '--no-sync' if you want to access non-published client endpoints(%s).\n", strings.Join(hc.Endpoints(), ","))
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}
if isConnectionError(err) {
handleError(c, ExitBadConnection, err)
handleError(c, cobrautl.ExitBadConnection, err)
}
}
if debug {
Expand Down
5 changes: 3 additions & 2 deletions etcdctl/ctlv2/command/watch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/signal"

"go.etcd.io/etcd/client/v2"
"go.etcd.io/etcd/pkg/v3/cobrautl"

"github.com/urfave/cli"
)
Expand All @@ -47,7 +48,7 @@ func NewWatchCommand() cli.Command {
// watchCommandFunc executes the "watch" command.
func watchCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(c, ExitBadArgs, errors.New("key required"))
handleError(c, cobrautl.ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]
recursive := c.Bool("recursive")
Expand All @@ -68,7 +69,7 @@ func watchCommandFunc(c *cli.Context, ki client.KeysAPI) {
for !stop {
resp, err := w.Next(context.TODO())
if err != nil {
handleError(c, ExitServerError, err)
handleError(c, cobrautl.ExitServerError, err)
}
if resp.Node.Dir {
continue
Expand Down
Loading

0 comments on commit f2bc5ee

Please sign in to comment.