From 366e5481e42bcdf18a1aa3b47b911eaefc9d7562 Mon Sep 17 00:00:00 2001 From: davidmdm Date: Thu, 27 May 2021 17:23:10 -0400 Subject: [PATCH 1/2] add tidy command --- src/cmd/get/tidy/tidy.go | 66 ++++++++++++++++++++++++++++++++++++ src/internal/context_test.go | 12 +++---- src/internal/yey.go | 6 ++-- src/main.go | 2 ++ 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 src/cmd/get/tidy/tidy.go diff --git a/src/cmd/get/tidy/tidy.go b/src/cmd/get/tidy/tidy.go new file mode 100644 index 0000000..f1c5022 --- /dev/null +++ b/src/cmd/get/tidy/tidy.go @@ -0,0 +1,66 @@ +package tidy + +import ( + "context" + "fmt" + "path/filepath" + "strings" + + "github.com/spf13/cobra" + + yey "github.com/silphid/yey/src/internal" + "github.com/silphid/yey/src/internal/docker" +) + +// New creates a cobra command +func New() *cobra.Command { + return &cobra.Command{ + Use: "tidy", + Short: "cleans unreferenced project containers", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return run(cmd.Context()) + }, + } +} + +func run(ctx context.Context) error { + contexts, err := yey.LoadContexts() + if err != nil { + return err + } + + validNames := make(map[string]struct{}) + for _, name := range contexts.GetNames() { + ctx, err := contexts.GetContext(name) + if err != nil { + return err + } + validNames[yey.ContainerName(contexts.Path, ctx)] = struct{}{} + } + + prefix := fmt.Sprintf( + "yey-%s-%s", + filepath.Base(filepath.Dir(contexts.Path)), + yey.Hash(contexts.Path), + ) + + names, err := docker.ListContainers(ctx) + if err != nil { + return err + } + + for _, container := range names { + if !strings.HasPrefix(container, prefix) { + continue + } + if _, ok := validNames[container]; ok { + continue + } + if err := docker.Remove(ctx, container); err != nil { + return err + } + } + + return nil +} diff --git a/src/internal/context_test.go b/src/internal/context_test.go index f683461..09d20ac 100644 --- a/src/internal/context_test.go +++ b/src/internal/context_test.go @@ -84,32 +84,32 @@ func TestMerge(t *testing.T) { func TestSameHashesForSameContexts(t *testing.T) { ctx1 := getCtx1() - hash1 := hash(ctx1.String()) + hash1 := Hash(ctx1.String()) ctx2 := getCtx1() - hash2 := hash(ctx2.String()) + hash2 := Hash(ctx2.String()) assert.Equal(t, hash1, hash2) } func TestDifferentHashesForDifferentEnvs(t *testing.T) { ctx1 := getCtx1() - hash1 := hash(ctx1.String()) + hash1 := Hash(ctx1.String()) ctx2 := getCtx1() ctx2.Env["ENV1"] = "value1b" - hash2 := hash(ctx2.String()) + hash2 := Hash(ctx2.String()) assert.NotEqual(t, hash1, hash2) } func TestDifferentHashesForDifferentMounts(t *testing.T) { ctx1 := getCtx1() - hash1 := hash(ctx1.String()) + hash1 := Hash(ctx1.String()) ctx2 := getCtx1() ctx2.Mounts["/local/mount1"] = "/container/mount1b" - hash2 := hash(ctx2.String()) + hash2 := Hash(ctx2.String()) assert.NotEqual(t, hash1, hash2) } diff --git a/src/internal/yey.go b/src/internal/yey.go index e43dcab..1c80084 100644 --- a/src/internal/yey.go +++ b/src/internal/yey.go @@ -8,7 +8,7 @@ import ( "path/filepath" ) -func hash(value string) string { +func Hash(value string) string { hasher := crc64.New(crc64.MakeTable(crc64.ECMA)) io.WriteString(hasher, value) return hex.EncodeToString(hasher.Sum(nil)) @@ -18,8 +18,8 @@ func ContainerName(path string, context Context) string { return fmt.Sprintf( "yey-%s-%s-%s-%s", filepath.Base(filepath.Dir(path)), - hash(path), + Hash(path), context.Name, - hash(context.String()), + Hash(context.String()), ) } diff --git a/src/main.go b/src/main.go index 01b8334..3b66e7f 100644 --- a/src/main.go +++ b/src/main.go @@ -12,6 +12,7 @@ import ( getcontainers "github.com/silphid/yey/src/cmd/get/containers" getcontext "github.com/silphid/yey/src/cmd/get/context" getcontexts "github.com/silphid/yey/src/cmd/get/contexts" + "github.com/silphid/yey/src/cmd/get/tidy" "github.com/silphid/yey/src/cmd/run" "github.com/silphid/yey/src/cmd/versioning" @@ -31,6 +32,7 @@ func main() { rootCmd := cmd.NewRoot() rootCmd.AddCommand(run.New()) rootCmd.AddCommand(versioning.New(version)) + rootCmd.AddCommand(tidy.New()) getCmd := get.New() getCmd.AddCommand(getcontext.New()) From e84ee6667deabb2781e567692e5ce1a8b6b2ba84 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette <1917993+silphid@users.noreply.github.com> Date: Thu, 27 May 2021 18:01:03 -0400 Subject: [PATCH 2/2] Minor help wording change --- src/cmd/get/tidy/tidy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/get/tidy/tidy.go b/src/cmd/get/tidy/tidy.go index f1c5022..159b4c2 100644 --- a/src/cmd/get/tidy/tidy.go +++ b/src/cmd/get/tidy/tidy.go @@ -16,7 +16,7 @@ import ( func New() *cobra.Command { return &cobra.Command{ Use: "tidy", - Short: "cleans unreferenced project containers", + Short: "Removes unreferenced containers", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { return run(cmd.Context())