mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #5994 from gyuho/v2-error
etcdctl/ctlv2: error handling with JSON
This commit is contained in:
commit
5b7728f3cb
@ -54,7 +54,7 @@ func handleClusterHealth(c *cli.Context) error {
|
|||||||
|
|
||||||
tr, err := getTransport(c)
|
tr, err := getTransport(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hc := http.Client{
|
hc := http.Client{
|
||||||
@ -66,7 +66,7 @@ func handleClusterHealth(c *cli.Context) error {
|
|||||||
ms, err := mi.List(context.TODO())
|
ms, err := mi.List(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("cluster may be unhealthy: failed to list members")
|
fmt.Println("cluster may be unhealthy: failed to list members")
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -15,10 +15,12 @@
|
|||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/coreos/etcd/client"
|
"github.com/coreos/etcd/client"
|
||||||
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -30,7 +32,18 @@ const (
|
|||||||
ExitClusterNotHealthy
|
ExitClusterNotHealthy
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleError(code int, err error) {
|
func handleError(c *cli.Context, code int, err error) {
|
||||||
|
if c.GlobalString("output") == "json" {
|
||||||
|
if err, ok := err.(*client.Error); ok {
|
||||||
|
b, err := json.Marshal(err)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Fprintln(os.Stderr, string(b))
|
||||||
|
os.Exit(code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprintln(os.Stderr, "Error: ", err)
|
fmt.Fprintln(os.Stderr, "Error: ", err)
|
||||||
if cerr, ok := err.(*client.ClusterError); ok {
|
if cerr, ok := err.(*client.ClusterError); ok {
|
||||||
fmt.Fprintln(os.Stderr, cerr.Detail())
|
fmt.Fprintln(os.Stderr, cerr.Detail())
|
||||||
|
@ -49,7 +49,7 @@ func execWatchCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
argslen := len(args)
|
argslen := len(args)
|
||||||
|
|
||||||
if argslen < 2 {
|
if argslen < 2 {
|
||||||
handleError(ExitBadArgs, errors.New("key and command to exec required"))
|
handleError(c, ExitBadArgs, errors.New("key and command to exec required"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -95,7 +95,7 @@ func execWatchCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
for {
|
for {
|
||||||
resp, err := w.Next(context.TODO())
|
resp, err := w.Next(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
if resp.Node.Dir {
|
if resp.Node.Dir {
|
||||||
fmt.Fprintf(os.Stderr, "Ignored dir %s change\n", resp.Node.Key)
|
fmt.Fprintf(os.Stderr, "Ignored dir %s change\n", resp.Node.Key)
|
||||||
|
@ -43,7 +43,7 @@ func NewGetCommand() cli.Command {
|
|||||||
// getCommandFunc executes the "get" command.
|
// getCommandFunc executes the "get" command.
|
||||||
func getCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func getCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
|
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
@ -54,7 +54,7 @@ func getCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sorted, Quorum: quorum})
|
resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sorted, Quorum: quorum})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Node.Dir {
|
if resp.Node.Dir {
|
||||||
|
@ -54,7 +54,7 @@ func lsCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sort, Recursive: recursive, Quorum: quorum})
|
resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sort, Recursive: recursive, Quorum: quorum})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
printLs(c, resp)
|
printLs(c, resp)
|
||||||
|
@ -43,12 +43,12 @@ func NewMakeCommand() cli.Command {
|
|||||||
// mkCommandFunc executes the "mk" command.
|
// mkCommandFunc executes the "mk" command.
|
||||||
func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
value, err := argOrStdin(c.Args(), os.Stdin, 1)
|
value, err := argOrStdin(c.Args(), os.Stdin, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitBadArgs, errors.New("value required"))
|
handleError(c, ExitBadArgs, errors.New("value required"))
|
||||||
}
|
}
|
||||||
|
|
||||||
ttl := c.Int("ttl")
|
ttl := c.Int("ttl")
|
||||||
@ -69,7 +69,7 @@ func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
printResponseKey(resp, c.GlobalString("output"))
|
printResponseKey(resp, c.GlobalString("output"))
|
||||||
|
@ -41,7 +41,7 @@ func NewMakeDirCommand() cli.Command {
|
|||||||
// mkdirCommandFunc executes the "mkdir" command.
|
// mkdirCommandFunc executes the "mkdir" command.
|
||||||
func mkdirCommandFunc(c *cli.Context, ki client.KeysAPI, prevExist client.PrevExistType) {
|
func mkdirCommandFunc(c *cli.Context, ki client.KeysAPI, prevExist client.PrevExistType) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
|
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
@ -51,7 +51,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})
|
resp, err := ki.Set(ctx, key, "", &client.SetOptions{TTL: time.Duration(ttl) * time.Second, Dir: true, PrevExist: prevExist})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
if c.GlobalString("output") != "simple" {
|
if c.GlobalString("output") != "simple" {
|
||||||
printResponseKey(resp, c.GlobalString("output"))
|
printResponseKey(resp, c.GlobalString("output"))
|
||||||
|
@ -43,7 +43,7 @@ func NewRemoveCommand() cli.Command {
|
|||||||
// rmCommandFunc executes the "rm" command.
|
// rmCommandFunc executes the "rm" command.
|
||||||
func rmCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func rmCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
recursive := c.Bool("recursive")
|
recursive := c.Bool("recursive")
|
||||||
@ -55,7 +55,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})
|
resp, err := ki.Delete(ctx, key, &client.DeleteOptions{PrevIndex: uint64(prevIndex), PrevValue: prevValue, Dir: dir, Recursive: recursive})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
if !resp.Node.Dir || c.GlobalString("output") != "simple" {
|
if !resp.Node.Dir || c.GlobalString("output") != "simple" {
|
||||||
printResponseKey(resp, c.GlobalString("output"))
|
printResponseKey(resp, c.GlobalString("output"))
|
||||||
|
@ -37,7 +37,7 @@ func NewRemoveDirCommand() cli.Command {
|
|||||||
// rmdirCommandFunc executes the "rmdir" command.
|
// rmdirCommandFunc executes the "rmdir" command.
|
||||||
func rmdirCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func rmdirCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ func rmdirCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
resp, err := ki.Delete(ctx, key, &client.DeleteOptions{Dir: true})
|
resp, err := ki.Delete(ctx, key, &client.DeleteOptions{Dir: true})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !resp.Node.Dir || c.GlobalString("output") != "simple" {
|
if !resp.Node.Dir || c.GlobalString("output") != "simple" {
|
||||||
|
@ -50,12 +50,12 @@ func NewSetCommand() cli.Command {
|
|||||||
// setCommandFunc executes the "set" command.
|
// setCommandFunc executes the "set" command.
|
||||||
func setCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func setCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
value, err := argOrStdin(c.Args(), os.Stdin, 1)
|
value, err := argOrStdin(c.Args(), os.Stdin, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitBadArgs, errors.New("value required"))
|
handleError(c, ExitBadArgs, errors.New("value required"))
|
||||||
}
|
}
|
||||||
|
|
||||||
ttl := c.Int("ttl")
|
ttl := c.Int("ttl")
|
||||||
@ -66,7 +66,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})
|
resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevIndex: uint64(prevIndex), PrevValue: prevValue})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
printResponseKey(resp, c.GlobalString("output"))
|
printResponseKey(resp, c.GlobalString("output"))
|
||||||
|
@ -42,12 +42,12 @@ func NewUpdateCommand() cli.Command {
|
|||||||
// updateCommandFunc executes the "update" command.
|
// updateCommandFunc executes the "update" command.
|
||||||
func updateCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func updateCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
value, err := argOrStdin(c.Args(), os.Stdin, 1)
|
value, err := argOrStdin(c.Args(), os.Stdin, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitBadArgs, errors.New("value required"))
|
handleError(c, ExitBadArgs, errors.New("value required"))
|
||||||
}
|
}
|
||||||
|
|
||||||
ttl := c.Int("ttl")
|
ttl := c.Int("ttl")
|
||||||
@ -56,7 +56,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})
|
resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevExist})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
printResponseKey(resp, c.GlobalString("output"))
|
printResponseKey(resp, c.GlobalString("output"))
|
||||||
|
@ -41,7 +41,7 @@ func NewUpdateDirCommand() cli.Command {
|
|||||||
// updatedirCommandFunc executes the "updatedir" command.
|
// updatedirCommandFunc executes the "updatedir" command.
|
||||||
func updatedirCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func updatedirCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
ttl := c.Int("ttl")
|
ttl := c.Int("ttl")
|
||||||
@ -49,7 +49,7 @@ func updatedirCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
resp, err := ki.Set(ctx, key, "", &client.SetOptions{TTL: time.Duration(ttl) * time.Second, Dir: true, PrevExist: client.PrevExist})
|
resp, err := ki.Set(ctx, key, "", &client.SetOptions{TTL: time.Duration(ttl) * time.Second, Dir: true, PrevExist: client.PrevExist})
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
if c.GlobalString("output") != "simple" {
|
if c.GlobalString("output") != "simple" {
|
||||||
printResponseKey(resp, c.GlobalString("output"))
|
printResponseKey(resp, c.GlobalString("output"))
|
||||||
|
@ -237,10 +237,10 @@ func mustNewClient(c *cli.Context) client.Client {
|
|||||||
if err == client.ErrNoEndpoints {
|
if err == client.ErrNoEndpoints {
|
||||||
fmt.Fprintf(os.Stderr, "etcd cluster has no published client endpoints.\n")
|
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(), ","))
|
fmt.Fprintf(os.Stderr, "Try '--no-sync' if you want to access non-published client endpoints(%s).\n", strings.Join(hc.Endpoints(), ","))
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
if isConnectionError(err) {
|
if isConnectionError(err) {
|
||||||
handleError(ExitBadConnection, err)
|
handleError(c, ExitBadConnection, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if debug {
|
if debug {
|
||||||
|
@ -46,7 +46,7 @@ func NewWatchCommand() cli.Command {
|
|||||||
// watchCommandFunc executes the "watch" command.
|
// watchCommandFunc executes the "watch" command.
|
||||||
func watchCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
func watchCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
handleError(ExitBadArgs, errors.New("key required"))
|
handleError(c, ExitBadArgs, errors.New("key required"))
|
||||||
}
|
}
|
||||||
key := c.Args()[0]
|
key := c.Args()[0]
|
||||||
recursive := c.Bool("recursive")
|
recursive := c.Bool("recursive")
|
||||||
@ -67,7 +67,7 @@ func watchCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
|||||||
for !stop {
|
for !stop {
|
||||||
resp, err := w.Next(context.TODO())
|
resp, err := w.Next(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(c, ExitServerError, err)
|
||||||
}
|
}
|
||||||
if resp.Node.Dir {
|
if resp.Node.Dir {
|
||||||
continue
|
continue
|
||||||
|
Loading…
x
Reference in New Issue
Block a user