mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 01:52:26 +08:00
Future-proof keystore.Has by returning an error as well
License: MIT Signed-off-by: Michael Muré <batolettre@gmail.com>
This commit is contained in:
@ -260,13 +260,21 @@ var keyRenameCmd = &cmds.Command{
|
||||
|
||||
overwrite := false
|
||||
force, _, _ := res.Request().Option("f").Bool()
|
||||
if force && ks.Has(newName) {
|
||||
overwrite = true
|
||||
err := ks.Delete(newName)
|
||||
if force {
|
||||
exist, err := ks.Has(newName)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
if exist {
|
||||
overwrite = true
|
||||
err := ks.Delete(newName)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = ks.Put(newName, oldKey)
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type Keystore interface {
|
||||
Has(string) bool
|
||||
Has(string) (bool, error)
|
||||
Put(string, ci.PrivKey) error
|
||||
Get(string) (ci.PrivKey, error)
|
||||
Delete(string) error
|
||||
@ -55,12 +55,20 @@ func NewFSKeystore(dir string) (*FSKeystore, error) {
|
||||
return &FSKeystore{dir}, nil
|
||||
}
|
||||
|
||||
func (ks *FSKeystore) Has(name string) bool {
|
||||
func (ks *FSKeystore) Has(name string) (bool, error) {
|
||||
kp := filepath.Join(ks.dir, name)
|
||||
|
||||
_, err := os.Stat(kp)
|
||||
|
||||
return err == nil
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (ks *FSKeystore) Put(name string, k ci.PrivKey) error {
|
||||
|
@ -82,13 +82,21 @@ func TestKeystoreBasics(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !ks.Has("foo") {
|
||||
exist, err := ks.Has("foo")
|
||||
if !exist {
|
||||
t.Fatal("should know it has a key named foo")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if ks.Has("nonexistingkey") {
|
||||
exist, err = ks.Has("nonexistingkey")
|
||||
if exist {
|
||||
t.Fatal("should know it doesn't have a key named nonexistingkey")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ks.Delete("bar"); err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -10,9 +10,9 @@ func NewMemKeystore() *MemKeystore {
|
||||
return &MemKeystore{make(map[string]ci.PrivKey)}
|
||||
}
|
||||
|
||||
func (mk *MemKeystore) Has(name string) bool {
|
||||
func (mk *MemKeystore) Has(name string) (bool, error) {
|
||||
_, ok := mk.keys[name]
|
||||
return ok
|
||||
return ok, nil
|
||||
}
|
||||
|
||||
func (mk *MemKeystore) Put(name string, k ci.PrivKey) error {
|
||||
|
@ -47,13 +47,21 @@ func TestMemKeyStoreBasics(t *testing.T) {
|
||||
t.Fatal("should not be able to overwrite key")
|
||||
}
|
||||
|
||||
if !ks.Has("foo") {
|
||||
exist, err := ks.Has("foo")
|
||||
if !exist {
|
||||
t.Fatal("should know it has a key named foo")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if ks.Has("nonexistingkey") {
|
||||
exist, err = ks.Has("nonexistingkey")
|
||||
if exist {
|
||||
t.Fatal("should know it doesn't have a key named nonexistingkey")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ks.Delete("bar"); err != nil {
|
||||
t.Fatal(err)
|
||||
|
Reference in New Issue
Block a user