本文整理汇总了Golang中github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd.Client类的典型用法代码示例。如果您正苦于以下问题:Golang Client类的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: removeDirCommandFunc
// removeDirCommandFunc executes the "rmdir" command.
func removeDirCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, errors.New("Key required")
}
key := c.Args()[0]
return client.DeleteDir(key)
}
示例2: prepAuth
func prepAuth(client *etcd.Client, usernameFlag string) error {
username, password, err := getUsernamePasswordFromFlag(usernameFlag)
if err != nil {
return err
}
client.SetCredentials(username, password)
return nil
}
示例3: watchCommandFunc
// watchCommandFunc executes the "watch" command.
func watchCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, errors.New("Key required")
}
key := c.Args()[0]
recursive := c.Bool("recursive")
forever := c.Bool("forever")
index := 0
if c.Int("after-index") != 0 {
index = c.Int("after-index") + 1
}
if forever {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
stop := make(chan bool)
go func() {
<-sigch
os.Exit(0)
}()
receiver := make(chan *etcd.Response)
errCh := make(chan error, 1)
go func() {
_, err := client.Watch(key, uint64(index), recursive, receiver, stop)
errCh <- err
}()
for {
select {
case resp := <-receiver:
printAll(resp, c.GlobalString("output"))
case err := <-errCh:
handleError(-1, err)
}
}
} else {
var resp *etcd.Response
var err error
resp, err = client.Watch(key, uint64(index), recursive, nil, nil)
if err != nil {
handleError(ErrorFromEtcd, err)
}
if err != nil {
return nil, err
}
printAll(resp, c.GlobalString("output"))
}
return nil, nil
}
示例4: setDirCommandFunc
// setDirCommandFunc executes the "setDir" command.
func setDirCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, errors.New("Key required")
}
key := c.Args()[0]
ttl := c.Int("ttl")
return client.SetDir(key, uint64(ttl))
}
示例5: getCommandFunc
// getCommandFunc executes the "get" command.
func getCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, errors.New("Key required")
}
key := c.Args()[0]
sorted := c.Bool("sort")
// Retrieve the value from the server.
return client.Get(key, sorted, false)
}
示例6: lsCommandFunc
// lsCommandFunc executes the "ls" command.
func lsCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
key := "/"
if len(c.Args()) != 0 {
key = c.Args()[0]
}
recursive := c.Bool("recursive")
// Retrieve the value from the server.
return client.Get(key, false, recursive)
}
示例7: makeCommandFunc
// makeCommandFunc executes the "make" command.
func makeCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, 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")
}
ttl := c.Int("ttl")
return client.Create(key, value, uint64(ttl))
}
示例8: runSet
func runSet(c *etcd.Client, setc chan set, wg *sync.WaitGroup) {
for s := range setc {
log.Println("copying key:", s.key)
if s.ttl != 0 && s.ttl < 300 {
log.Printf("extending key %s's ttl to 300 seconds", s.key)
s.ttl = 5 * 60
}
_, err := c.Set(s.key, s.value, uint64(s.ttl))
if err != nil {
log.Fatalf("failed to copy key: %v\n", err)
}
}
wg.Done()
}
示例9: setCommandFunc
// setCommandFunc executes the "set" command.
func setCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, 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")
}
ttl := c.Int("ttl")
prevValue := c.String("swap-with-value")
prevIndex := c.Int("swap-with-index")
if prevValue == "" && prevIndex == 0 {
return client.Set(key, value, uint64(ttl))
}
return client.CompareAndSwap(key, value, uint64(ttl), prevValue, uint64(prevIndex))
}
示例10: getCommandFunc
// getCommandFunc executes the "get" command.
func getCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, errors.New("Key required")
}
key := c.Args()[0]
consistent := c.Bool("consistent")
sorted := c.Bool("sort")
// Setup consistency on the client.
if consistent {
client.SetConsistency(etcd.STRONG_CONSISTENCY)
} else {
client.SetConsistency(etcd.WEAK_CONSISTENCY)
}
// Retrieve the value from the server.
return client.Get(key, sorted, false)
}
示例11: removeCommandFunc
// removeCommandFunc executes the "rm" command.
func removeCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, errors.New("Key required")
}
key := c.Args()[0]
recursive := c.Bool("recursive")
dir := c.Bool("dir")
// TODO: distinguish with flag is not set and empty flag
// the cli pkg need to provide this feature
prevValue := c.String("with-value")
prevIndex := uint64(c.Int("with-index"))
if prevValue != "" || prevIndex != 0 {
return client.CompareAndDelete(key, prevValue, prevIndex)
}
if recursive || !dir {
return client.Delete(key, recursive)
}
return client.DeleteDir(key)
}
示例12: dumpCURL
// dumpCURL blindly dumps all curl output to os.Stderr
func dumpCURL(client *etcd.Client) {
client.OpenCURL()
for {
fmt.Fprintf(os.Stderr, "Curl-Example: %s\n", client.RecvCURL())
}
}
示例13: execWatchCommandFunc
// execWatchCommandFunc executes the "exec-watch" command.
func execWatchCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
_ = io.Copy
_ = exec.Command
args := c.Args()
argsLen := len(args)
if argsLen < 2 {
return nil, errors.New("Key and command to exec required")
}
key := args[argsLen-1]
cmdArgs := args[:argsLen-1]
index := 0
if c.Int("after-index") != 0 {
index = c.Int("after-index") + 1
key = args[0]
cmdArgs = args[2:]
}
recursive := c.Bool("recursive")
if recursive != false {
key = args[0]
cmdArgs = args[2:]
}
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
stop := make(chan bool)
go func() {
<-sigch
stop <- true
os.Exit(0)
}()
receiver := make(chan *etcd.Response)
client.SetConsistency(etcd.WEAK_CONSISTENCY)
go client.Watch(key, uint64(index), recursive, receiver, stop)
for {
resp := <-receiver
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Env = environResponse(resp, os.Environ())
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
err = cmd.Start()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
cmd.Wait()
}
}