github.com/slantview/etcdctl@v0.1.3-0.20131011185546-5aaeca137f94/test_and_set.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  )
     8  
     9  const TestAndSetUsage = `usage: etcdctl [etcd flags] testAndSet <key> <prevValue> <value> [testAndSet flags]
    10  special flags: --ttl to set a key with ttl`
    11  
    12  var (
    13  	testAndSetFlag = flag.NewFlagSet("testAndSet", flag.ExitOnError)
    14  	testAndSetTtl  = testAndSetFlag.Int64("ttl", 0, "ttl of the key")
    15  )
    16  
    17  func init() {
    18  	registerCommand("testAndSet", TestAndSetUsage, 4, 6, testAndSet)
    19  }
    20  
    21  func testAndSet(args []string) error {
    22  	key := args[1]
    23  	prevValue := args[2]
    24  	value := args[3]
    25  	testAndSetFlag.Parse(args[4:])
    26  	resp, success, err := client.TestAndSet(key, prevValue, value, uint64(*testAndSetTtl))
    27  
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	if success {
    33  		fmt.Println(resp.Value)
    34  		return nil
    35  	}
    36  
    37  	return errors.New("TestAndSet: prevValue does not match the current value of the given key")
    38  }