浏览代码

etcdctl: mk use etcd/client

Xiang Li 10 年之前
父节点
当前提交
9d7a8dd2b0
共有 1 个文件被更改,包括 15 次插入7 次删除
  1. 15 7
      etcdctl/command/mk_command.go

+ 15 - 7
etcdctl/command/mk_command.go

@@ -17,9 +17,11 @@ package command
 import (
 	"errors"
 	"os"
+	"time"
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
-	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
+	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/client"
 )
 
 // NewMakeCommand returns the CLI command for "mk".
@@ -31,23 +33,29 @@ func NewMakeCommand() cli.Command {
 			cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
 		},
 		Action: func(c *cli.Context) {
-			handleKey(c, makeCommandFunc)
+			mkCommandFunc(c, mustNewKeyAPI(c))
 		},
 	}
 }
 
-// makeCommandFunc executes the "make" command.
-func makeCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
+// mkCommandFunc executes the "mk" command.
+func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
 	if len(c.Args()) == 0 {
-		return nil, errors.New("key required")
+		handleError(ExitBadArgs, errors.New("key required"))
 	}
 	key := c.Args()[0]
 	value, err := argOrStdin(c.Args(), os.Stdin, 1)
 	if err != nil {
-		return nil, errors.New("value required")
+		handleError(ExitBadArgs, errors.New("value required"))
 	}
 
 	ttl := c.Int("ttl")
 
-	return client.Create(key, value, uint64(ttl))
+	// TODO: handle transport timeout
+	resp, err := ki.Set(context.TODO(), key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevIgnore})
+	if err != nil {
+		handleError(ExitServerError, err)
+	}
+
+	printResponseKey(resp, c.GlobalString("output"))
 }