From 3c43606a204030cbd83b43d9476add4d7c663332 Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Mon, 11 May 2020 17:57:17 +0200 Subject: [PATCH] Add remove function to context store Signed-off-by: Christopher Crone --- context/store/store.go | 23 +++++++++++++++++++++-- context/store/store_test.go | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/context/store/store.go b/context/store/store.go index ea0dd6ca..51ad0ea9 100644 --- a/context/store/store.go +++ b/context/store/store.go @@ -30,6 +30,7 @@ package store import ( "context" "encoding/json" + "fmt" "io/ioutil" "os" "path/filepath" @@ -71,6 +72,8 @@ type Store interface { Create(name string, data TypedContext) error // List returns the list of created contexts List() ([]*Metadata, error) + // Remove removes a context by name from the context store + Remove(name string) error } type store struct { @@ -119,7 +122,7 @@ func (s *store) Get(name string, getter func() interface{}) (*Metadata, error) { meta := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name), metaFile) m, err := read(meta, getter) if os.IsNotExist(err) { - return nil, errors.Wrapf(errdefs.ErrNotFound, "context %q", name) + return nil, errors.Wrap(errdefs.ErrNotFound, objectName(name)) } else if err != nil { return nil, err } @@ -187,7 +190,7 @@ func (s *store) Create(name string, data TypedContext) error { dir := contextdirOf(name) metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir) if _, err := os.Stat(metaDir); !os.IsNotExist(err) { - return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", name) + return errors.Wrap(errdefs.ErrAlreadyExists, objectName(name)) } err := os.Mkdir(metaDir, 0755) @@ -238,10 +241,26 @@ func (s *store) List() ([]*Metadata, error) { return result, nil } +func (s *store) Remove(name string) error { + dir := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name)) + // Check if directory exists because os.RemoveAll returns nil if it doesn't + if _, err := os.Stat(dir); os.IsNotExist(err) { + return errors.Wrap(errdefs.ErrNotFound, objectName(name)) + } + if err := os.RemoveAll(dir); err != nil { + return errors.Wrapf(errdefs.ErrUnknown, "unable to remove %s: %s", objectName(name), err) + } + return nil +} + func contextdirOf(name string) string { return digest.FromString(name).Encoded() } +func objectName(name string) string { + return fmt.Sprintf("context %q", name) +} + type dummyContext struct{} // Metadata represents the docker context metadata diff --git a/context/store/store_test.go b/context/store/store_test.go index 7c8999fb..56917969 100644 --- a/context/store/store_test.go +++ b/context/store/store_test.go @@ -107,6 +107,26 @@ func (suite *StoreTestSuite) TestList() { require.Equal(suite.T(), contexts[1].Name, "test2") } +func (suite *StoreTestSuite) TestRemoveNotFound() { + err := suite.store.Remove("notfound") + require.EqualError(suite.T(), err, `context "notfound": not found`) + require.True(suite.T(), errdefs.IsNotFoundError(err)) +} + +func (suite *StoreTestSuite) TestRemove() { + err := suite.store.Create("testremove", TypedContext{}) + require.Nil(suite.T(), err) + contexts, err := suite.store.List() + require.Nil(suite.T(), err) + require.Equal(suite.T(), len(contexts), 1) + + err = suite.store.Remove("testremove") + require.Nil(suite.T(), err) + contexts, err = suite.store.List() + require.Nil(suite.T(), err) + require.Equal(suite.T(), len(contexts), 0) +} + func TestExampleTestSuite(t *testing.T) { suite.Run(t, new(StoreTestSuite)) }