mirror of
https://github.com/docker/compose.git
synced 2025-07-27 23:54:04 +02:00
Add remove function to context store
Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
parent
4788dd5b93
commit
3c43606a20
@ -30,6 +30,7 @@ package store
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -71,6 +72,8 @@ type Store interface {
|
|||||||
Create(name string, data TypedContext) error
|
Create(name string, data TypedContext) error
|
||||||
// List returns the list of created contexts
|
// List returns the list of created contexts
|
||||||
List() ([]*Metadata, error)
|
List() ([]*Metadata, error)
|
||||||
|
// Remove removes a context by name from the context store
|
||||||
|
Remove(name string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type store struct {
|
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)
|
meta := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name), metaFile)
|
||||||
m, err := read(meta, getter)
|
m, err := read(meta, getter)
|
||||||
if os.IsNotExist(err) {
|
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 {
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -187,7 +190,7 @@ func (s *store) Create(name string, data TypedContext) error {
|
|||||||
dir := contextdirOf(name)
|
dir := contextdirOf(name)
|
||||||
metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir)
|
metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir)
|
||||||
if _, err := os.Stat(metaDir); !os.IsNotExist(err) {
|
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)
|
err := os.Mkdir(metaDir, 0755)
|
||||||
@ -238,10 +241,26 @@ func (s *store) List() ([]*Metadata, error) {
|
|||||||
return result, nil
|
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 {
|
func contextdirOf(name string) string {
|
||||||
return digest.FromString(name).Encoded()
|
return digest.FromString(name).Encoded()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func objectName(name string) string {
|
||||||
|
return fmt.Sprintf("context %q", name)
|
||||||
|
}
|
||||||
|
|
||||||
type dummyContext struct{}
|
type dummyContext struct{}
|
||||||
|
|
||||||
// Metadata represents the docker context metadata
|
// Metadata represents the docker context metadata
|
||||||
|
@ -107,6 +107,26 @@ func (suite *StoreTestSuite) TestList() {
|
|||||||
require.Equal(suite.T(), contexts[1].Name, "test2")
|
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) {
|
func TestExampleTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(StoreTestSuite))
|
suite.Run(t, new(StoreTestSuite))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user