Ejemplo n.º 1
0
func (c *UnlockCommand) Run(args []string) int {
	var sessionId string

	c.Consul = new(ConsulFlags)
	cmdFlags := NewFlagSet(c.Consul)
	cmdFlags.StringVar(&sessionId, "session", "", "")
	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }

	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	if sessionId == "" {
		c.UI.Error("Session ID must be provided")
		c.UI.Error("")
		c.UI.Error(c.Help())
		return 1
	}

	extra := cmdFlags.Args()
	if len(extra) < 1 {
		c.UI.Error("Key path must be specified")
		c.UI.Error("")
		c.UI.Error(c.Help())
		return 1
	}

	path := extra[0]

	consul, err := NewConsulClient(c.Consul, &c.UI)
	if err != nil {
		c.UI.Error(err.Error())
		return 1
	}
	client := consul.KV()

	kv := new(consulapi.KVPair)
	kv.Key = path
	kv.Session = sessionId
	kv.Flags = LockFlagValue

	success, _, err := client.Release(kv, nil)
	if err != nil {
		c.UI.Error(err.Error())
		return 1
	}

	if !success {
		return 1
	}

	return 0
}
Ejemplo n.º 2
0
func (c *KVWriteCommand) Run(args []string) int {
	c.AddDataCenter()
	flags := c.Meta.FlagSet()
	flags.StringVar(&c.modifyIndex, "modifyindex", "", "")
	flags.StringVar(&c.dataFlags, "flags", "", "")
	flags.Usage = func() { c.UI.Output(c.Help()) }

	if err := flags.Parse(args); err != nil {
		return 1
	}

	extra := flags.Args()
	if len(extra) < 2 {
		c.UI.Error("Key path and value must be specified")
		c.UI.Error("")
		c.UI.Error(c.Help())
		return 1
	}

	path := extra[0]
	value := strings.Join(extra[1:], " ")

	kv := new(consulapi.KVPair)

	kv.Key = path
	if strings.HasPrefix(value, "@") {
		v, err := ioutil.ReadFile(value[1:])
		if err != nil {
			c.UI.Error(fmt.Sprintf("ReadFile error: %v", err))
			return 1
		}
		kv.Value = v
	} else {
		kv.Value = []byte(value)
	}

	// &flags=
	//
	if c.dataFlags != "" {
		f, err := strconv.ParseUint(c.dataFlags, 0, 64)
		if err != nil {
			c.UI.Error(fmt.Sprintf("Error parsing flags: %v", c.dataFlags))
			c.UI.Error("")
			return 1
		}
		kv.Flags = f
	}

	consul, err := c.Client()
	if err != nil {
		c.UI.Error(err.Error())
		return 1
	}
	client := consul.KV()

	writeOpts := c.WriteOptions()

	if c.modifyIndex == "" {
		_, err := client.Put(kv, writeOpts)
		if err != nil {
			c.UI.Error(err.Error())
			return 1
		}
	} else {
		// Check-and-Set
		i, err := strconv.ParseUint(c.modifyIndex, 0, 64)
		if err != nil {
			c.UI.Error(fmt.Sprintf("Error parsing modifyIndex: %v", c.modifyIndex))
			c.UI.Error("")
			return 1
		}
		kv.ModifyIndex = i

		success, _, err := client.CAS(kv, writeOpts)
		if err != nil {
			c.UI.Error(err.Error())
			return 1
		}

		if !success {
			return 1
		}
	}

	return 0
}
Ejemplo n.º 3
0
func (c *WriteCommand) Run(args []string) int {
	var modifyIndex string
	var flags string

	c.Consul = new(ConsulFlags)
	cmdFlags := NewFlagSet(c.Consul)
	cmdFlags.StringVar(&modifyIndex, "cas", "", "")
	cmdFlags.StringVar(&flags, "flags", "", "")
	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }

	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	extra := cmdFlags.Args()
	if len(extra) < 2 {
		c.UI.Error("Key path and value must be specified")
		c.UI.Error("")
		c.UI.Error(c.Help())
		return 1
	}

	path := extra[0]
	value := strings.Join(extra[1:], " ")

	kv := new(consulapi.KVPair)

	kv.Key = path
	if strings.HasPrefix(value, "@") {
		v, err := ioutil.ReadFile(value[1:])
		if err != nil {
			c.UI.Error(fmt.Sprintf("ReadFile error: %v", err))
			return 1
		}
		kv.Value = v
	} else {
		kv.Value = []byte(value)
	}

	// &flags=
	//
	if flags != "" {
		f, err := strconv.ParseUint(flags, 0, 64)
		if err != nil {
			c.UI.Error(fmt.Sprintf("Error parsing flags: %v", flags))
			c.UI.Error("")
			return 1
		}
		kv.Flags = f
	}

	consul, err := NewConsulClient(c.Consul, &c.UI)
	if err != nil {
		c.UI.Error(err.Error())
		return 1
	}
	client := consul.KV()

	if modifyIndex == "" {
		_, err := client.Put(kv, nil)
		if err != nil {
			c.UI.Error(err.Error())
			return 1
		}
	} else {
		// Check-and-Set
		i, err := strconv.ParseUint(modifyIndex, 0, 64)
		if err != nil {
			c.UI.Error(fmt.Sprintf("Error parsing modifyIndex: %v", modifyIndex))
			c.UI.Error("")
			return 1
		}
		kv.ModifyIndex = i

		success, _, err := client.CAS(kv, nil)
		if err != nil {
			c.UI.Error(err.Error())
			return 1
		}

		if !success {
			return 1
		}
	}

	return 0
}
Ejemplo n.º 4
0
func (k *Kv) Write(args []string, kwo *KvWriteOptions) error {
	if len(args) < 2 {
		return fmt.Errorf("Key path and value must be specified")
	}

	path := args[0]
	value := strings.Join(args[1:], " ")

	kv := new(consulapi.KVPair)

	kv.Key = path
	if strings.HasPrefix(value, "@") {
		v, err := ioutil.ReadFile(value[1:])
		if err != nil {
			return fmt.Errorf("ReadFile error: %v", err)
		}
		kv.Value = v
	} else {
		kv.Value = []byte(value)
	}

	// &flags=
	//
	if kwo.DataFlags != "" {
		f, err := strconv.ParseUint(kwo.DataFlags, 0, 64)
		if err != nil {
			return fmt.Errorf("Error parsing flags: %v", kwo.DataFlags)
		}
		kv.Flags = f
	}

	client, err := k.KV()
	if err != nil {
		return err
	}

	writeOpts := k.WriteOptions()

	if kwo.ModifyIndex == "" {
		_, err := client.Put(kv, writeOpts)
		if err != nil {
			return err
		}
	} else {
		// Check-and-Set
		i, err := strconv.ParseUint(kwo.ModifyIndex, 0, 64)
		if err != nil {
			return fmt.Errorf("Error parsing modifyIndex: %v", kwo.ModifyIndex)
		}
		kv.ModifyIndex = i

		success, _, err := client.CAS(kv, writeOpts)
		if err != nil {
			return err
		}

		if !success {
			return fmt.Errorf("Failed to write to K/V")
		}
	}

	return nil
}