mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +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,7 +260,14 @@ var keyRenameCmd = &cmds.Command{
|
|||||||
|
|
||||||
overwrite := false
|
overwrite := false
|
||||||
force, _, _ := res.Request().Option("f").Bool()
|
force, _, _ := res.Request().Option("f").Bool()
|
||||||
if force && ks.Has(newName) {
|
if force {
|
||||||
|
exist, err := ks.Has(newName)
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if exist {
|
||||||
overwrite = true
|
overwrite = true
|
||||||
err := ks.Delete(newName)
|
err := ks.Delete(newName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -268,6 +275,7 @@ var keyRenameCmd = &cmds.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = ks.Put(newName, oldKey)
|
err = ks.Put(newName, oldKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Keystore interface {
|
type Keystore interface {
|
||||||
Has(string) bool
|
Has(string) (bool, error)
|
||||||
Put(string, ci.PrivKey) error
|
Put(string, ci.PrivKey) error
|
||||||
Get(string) (ci.PrivKey, error)
|
Get(string) (ci.PrivKey, error)
|
||||||
Delete(string) error
|
Delete(string) error
|
||||||
@ -55,12 +55,20 @@ func NewFSKeystore(dir string) (*FSKeystore, error) {
|
|||||||
return &FSKeystore{dir}, nil
|
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)
|
kp := filepath.Join(ks.dir, name)
|
||||||
|
|
||||||
_, err := os.Stat(kp)
|
_, 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 {
|
func (ks *FSKeystore) Put(name string, k ci.PrivKey) error {
|
||||||
|
@ -82,13 +82,21 @@ func TestKeystoreBasics(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ks.Has("foo") {
|
exist, err := ks.Has("foo")
|
||||||
|
if !exist {
|
||||||
t.Fatal("should know it has a key named foo")
|
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")
|
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 {
|
if err := ks.Delete("bar"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -10,9 +10,9 @@ func NewMemKeystore() *MemKeystore {
|
|||||||
return &MemKeystore{make(map[string]ci.PrivKey)}
|
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]
|
_, ok := mk.keys[name]
|
||||||
return ok
|
return ok, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mk *MemKeystore) Put(name string, k ci.PrivKey) error {
|
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")
|
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")
|
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")
|
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 {
|
if err := ks.Delete("bar"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
Reference in New Issue
Block a user