示例#1
0
func (c *putCommand) Run() error {
	buckets, err := clibolt.SplitBuckets(c.Arguments.Bucket)
	if err != nil {
		return err
	}

	key, err := clibolt.DecodeKey(c.Arguments.Key)
	if err != nil {
		return err
	}

	val, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
	}

	err = clibolt.Bolt.State.DB.Update(func(tx *bolt.Tx) error {
		bucket, err := clibolt.LookupBucket(tx, buckets)
		if err != nil {
			return err
		}
		return bucket.Put(key, val)
	})
	if err != nil {
		return err
	}
	return nil
}
示例#2
0
func (c *getCommand) Run() error {
	buckets, err := clibolt.SplitBuckets(c.Arguments.Bucket)
	if err != nil {
		return err
	}

	key, err := clibolt.DecodeKey(c.Arguments.Key)
	if err != nil {
		return err
	}

	var val []byte
	err = clibolt.Bolt.State.DB.View(func(tx *bolt.Tx) error {
		bucket, err := clibolt.LookupBucket(tx, buckets)
		if err != nil {
			return err
		}
		val = bucket.Get(key)
		return nil
	})
	if err != nil {
		return err
	}
	if val == nil {
		return errors.New("database key not found")
	}
	_, err = os.Stdout.Write(val)
	if err != nil {
		return err
	}
	return nil
}