From c6dfff131fa7605fde4832b6d36b3cd86975c24f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 28 Sep 2022 16:33:18 +0200 Subject: [PATCH] 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 --- cli/context/store/metadata_test.go | 12 ++++++------ cli/context/store/metadatastore.go | 15 +++++++++------ cli/context/store/store.go | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cli/context/store/metadata_test.go b/cli/context/store/metadata_test.go index 9de17408b0..b81f53b0f5 100644 --- a/cli/context/store/metadata_test.go +++ b/cli/context/store/metadata_test.go @@ -41,7 +41,7 @@ func TestMetadataCreateGetRemove(t *testing.T) { assert.NilError(t, err) // create a new instance to check it does not depend on some sort of state testee = metadataStore{root: testDir, config: testCfg} - meta, err := testee.get(contextdirOf("test-context")) + meta, err := testee.get("test-context") assert.NilError(t, err) assert.DeepEqual(t, meta, testMeta) @@ -49,13 +49,13 @@ func TestMetadataCreateGetRemove(t *testing.T) { err = testee.createOrUpdate(expected2) assert.NilError(t, err) - meta, err = testee.get(contextdirOf("test-context")) + meta, err = testee.get("test-context") assert.NilError(t, err) assert.DeepEqual(t, meta, expected2) - assert.NilError(t, testee.remove(contextdirOf("test-context"))) - assert.NilError(t, testee.remove(contextdirOf("test-context"))) // support duplicate remove - _, err = testee.get(contextdirOf("test-context")) + assert.NilError(t, testee.remove("test-context")) + assert.NilError(t, testee.remove("test-context")) // support duplicate remove + _, err = testee.get("test-context") assert.Assert(t, IsErrContextDoesNotExist(err)) } @@ -121,7 +121,7 @@ func TestWithEmbedding(t *testing.T) { }, } 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.Equal(t, testCtxMeta, res.Metadata) } diff --git a/cli/context/store/metadatastore.go b/cli/context/store/metadatastore.go index 151852fa1b..92fc7d4724 100644 --- a/cli/context/store/metadatastore.go +++ b/cli/context/store/metadatastore.go @@ -55,9 +55,12 @@ func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) { return reflect.ValueOf(typed).Elem().Interface(), nil } -func (s *metadataStore) get(id contextdir) (Metadata, error) { - contextDir := s.contextDir(id) - bytes, err := os.ReadFile(filepath.Join(contextDir, metaFile)) +func (s *metadataStore) get(name string) (Metadata, error) { + return s.getByID(contextdirOf(name)) +} + +func (s *metadataStore) getByID(id contextdir) (Metadata, error) { + bytes, err := os.ReadFile(filepath.Join(s.contextDir(id), metaFile)) if err != nil { return Metadata{}, convertContextDoesNotExist(err) } @@ -80,8 +83,8 @@ func (s *metadataStore) get(id contextdir) (Metadata, error) { return r, err } -func (s *metadataStore) remove(id contextdir) error { - contextDir := s.contextDir(id) +func (s *metadataStore) remove(name string) error { + contextDir := s.contextDir(contextdirOf(name)) return os.RemoveAll(contextDir) } @@ -95,7 +98,7 @@ func (s *metadataStore) list() ([]Metadata, error) { } var res []Metadata for _, dir := range ctxDirs { - c, err := s.get(contextdir(dir)) + c, err := s.getByID(contextdir(dir)) if err != nil { return nil, err } diff --git a/cli/context/store/store.go b/cli/context/store/store.go index 19ad980a47..d4657b99bd 100644 --- a/cli/context/store/store.go +++ b/cli/context/store/store.go @@ -137,14 +137,14 @@ func (s *store) CreateOrUpdate(meta Metadata) error { func (s *store) Remove(name string) error { 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(s.tls.removeAllContextData(id), name) } func (s *store) GetMetadata(name string) (Metadata, error) { - res, err := s.meta.get(contextdirOf(name)) + res, err := s.meta.get(name) patchErrContextName(err, name) return res, err }