cli/context/store: metadataStore.get(), .remove(): accept name instead of ID

This allows callers to just pass the name, and handle the conversion to ID and
path internally.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-09-28 16:33:18 +02:00
parent a496a7d501
commit c6dfff131f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 17 additions and 14 deletions

View File

@ -41,7 +41,7 @@ func TestMetadataCreateGetRemove(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
// create a new instance to check it does not depend on some sort of state // create a new instance to check it does not depend on some sort of state
testee = metadataStore{root: testDir, config: testCfg} testee = metadataStore{root: testDir, config: testCfg}
meta, err := testee.get(contextdirOf("test-context")) meta, err := testee.get("test-context")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, meta, testMeta) assert.DeepEqual(t, meta, testMeta)
@ -49,13 +49,13 @@ func TestMetadataCreateGetRemove(t *testing.T) {
err = testee.createOrUpdate(expected2) err = testee.createOrUpdate(expected2)
assert.NilError(t, err) assert.NilError(t, err)
meta, err = testee.get(contextdirOf("test-context")) meta, err = testee.get("test-context")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, meta, expected2) assert.DeepEqual(t, meta, expected2)
assert.NilError(t, testee.remove(contextdirOf("test-context"))) assert.NilError(t, testee.remove("test-context"))
assert.NilError(t, testee.remove(contextdirOf("test-context"))) // support duplicate remove assert.NilError(t, testee.remove("test-context")) // support duplicate remove
_, err = testee.get(contextdirOf("test-context")) _, err = testee.get("test-context")
assert.Assert(t, IsErrContextDoesNotExist(err)) assert.Assert(t, IsErrContextDoesNotExist(err))
} }
@ -121,7 +121,7 @@ func TestWithEmbedding(t *testing.T) {
}, },
} }
assert.NilError(t, testee.createOrUpdate(Metadata{Metadata: testCtxMeta, Name: "test"})) assert.NilError(t, testee.createOrUpdate(Metadata{Metadata: testCtxMeta, Name: "test"}))
res, err := testee.get(contextdirOf("test")) res, err := testee.get("test")
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, testCtxMeta, res.Metadata) assert.Equal(t, testCtxMeta, res.Metadata)
} }

View File

@ -55,9 +55,12 @@ func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) {
return reflect.ValueOf(typed).Elem().Interface(), nil return reflect.ValueOf(typed).Elem().Interface(), nil
} }
func (s *metadataStore) get(id contextdir) (Metadata, error) { func (s *metadataStore) get(name string) (Metadata, error) {
contextDir := s.contextDir(id) return s.getByID(contextdirOf(name))
bytes, err := os.ReadFile(filepath.Join(contextDir, metaFile)) }
func (s *metadataStore) getByID(id contextdir) (Metadata, error) {
bytes, err := os.ReadFile(filepath.Join(s.contextDir(id), metaFile))
if err != nil { if err != nil {
return Metadata{}, convertContextDoesNotExist(err) return Metadata{}, convertContextDoesNotExist(err)
} }
@ -80,8 +83,8 @@ func (s *metadataStore) get(id contextdir) (Metadata, error) {
return r, err return r, err
} }
func (s *metadataStore) remove(id contextdir) error { func (s *metadataStore) remove(name string) error {
contextDir := s.contextDir(id) contextDir := s.contextDir(contextdirOf(name))
return os.RemoveAll(contextDir) return os.RemoveAll(contextDir)
} }
@ -95,7 +98,7 @@ func (s *metadataStore) list() ([]Metadata, error) {
} }
var res []Metadata var res []Metadata
for _, dir := range ctxDirs { for _, dir := range ctxDirs {
c, err := s.get(contextdir(dir)) c, err := s.getByID(contextdir(dir))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -137,14 +137,14 @@ func (s *store) CreateOrUpdate(meta Metadata) error {
func (s *store) Remove(name string) error { func (s *store) Remove(name string) error {
id := contextdirOf(name) id := contextdirOf(name)
if err := s.meta.remove(id); err != nil { if err := s.meta.remove(name); err != nil {
return patchErrContextName(err, name) return patchErrContextName(err, name)
} }
return patchErrContextName(s.tls.removeAllContextData(id), name) return patchErrContextName(s.tls.removeAllContextData(id), name)
} }
func (s *store) GetMetadata(name string) (Metadata, error) { func (s *store) GetMetadata(name string) (Metadata, error) {
res, err := s.meta.get(contextdirOf(name)) res, err := s.meta.get(name)
patchErrContextName(err, name) patchErrContextName(err, name)
return res, err return res, err
} }