cheapcash/rename.go

47 lines
848 B
Go
Raw Normal View History

2021-11-16 13:18:23 +00:00
package cheapcash
import "os"
2021-11-19 10:55:31 +00:00
// Rename a key. It's that simple.
// The contents of the cache stays the same, but the key
// is renamed.
//
// It will return 2 different error in case of:
//
// 1. If the old key (first argument parameter) doesn't exists,
// it will return an error of ErrNotExists
//
// 2. If the new key (second argument parameter) already exists,
// it will return an error of ErrExists
2021-11-16 13:18:23 +00:00
func (c *Cache) Rename(old, new string) error {
err := checkDir(sanitizePath(c.Path))
if err != nil {
return err
}
checkOld, err := c.Exists(c.Path + old)
if err != nil {
return err
}
checkNew, err := c.Exists(c.Path + new)
if err != nil {
return err
}
if !checkOld {
return ErrNotExists
}
if checkNew {
return ErrExists
}
err = os.Rename(c.Path+old, c.Path+new)
if err != nil {
return err
}
return nil
}