etcdctl: Add an option to mk command to create in-order keys.
Adding a new option to the mk command to create in-order keys under a given directory, identified by <key> argument. Optionally TTL can also be set on the newly created in-order key.release-2.3
parent
a4624666fe
commit
ef0c8e5c18
|
@ -110,6 +110,12 @@ $ etcdctl mk /foo/new_bar "Hello world"
|
||||||
Hello world
|
Hello world
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Create a new in-order key under dir `/fooDir`:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ etcdctl mk --in-order /fooDir "Hello world"
|
||||||
|
```
|
||||||
|
|
||||||
Create a new dir `/fooDir`, only if the key did not previously exist:
|
Create a new dir `/fooDir`, only if the key did not previously exist:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -30,6 +30,7 @@ func NewMakeCommand() cli.Command {
|
||||||
Usage: "make a new key with a given value",
|
Usage: "make a new key with a given value",
|
||||||
ArgsUsage: "<key> <value>",
|
ArgsUsage: "<key> <value>",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
|
cli.BoolFlag{Name: "in-order", Usage: "create in-order key under directory <key>"},
|
||||||
cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
|
cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) {
|
Action: func(c *cli.Context) {
|
||||||
|
@ -50,13 +51,21 @@ func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ttl := c.Int("ttl")
|
ttl := c.Int("ttl")
|
||||||
|
inorder := c.Bool("in-order")
|
||||||
|
|
||||||
|
var resp *client.Response
|
||||||
ctx, cancel := contextWithTotalTimeout(c)
|
ctx, cancel := contextWithTotalTimeout(c)
|
||||||
// Since PrevNoExist means that the Node must not exist previously,
|
if !inorder {
|
||||||
// this Set method always creates a new key. Therefore, mk command
|
// Since PrevNoExist means that the Node must not exist previously,
|
||||||
// succeeds only if the key did not previously exist, and the command
|
// this Set method always creates a new key. Therefore, mk command
|
||||||
// prevents one from overwriting values accidentally.
|
// succeeds only if the key did not previously exist, and the command
|
||||||
resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevNoExist})
|
// prevents one from overwriting values accidentally.
|
||||||
|
resp, err = ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevNoExist})
|
||||||
|
} else {
|
||||||
|
// If in-order flag is specified then create an inorder key under
|
||||||
|
// the directory identified by the key argument.
|
||||||
|
resp, err = ki.CreateInOrder(ctx, key, value, &client.CreateInOrderOptions{TTL: time.Duration(ttl) * time.Second})
|
||||||
|
}
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(ExitServerError, err)
|
handleError(ExitServerError, err)
|
||||||
|
|
Loading…
Reference in New Issue