Skip to content

Commit

Permalink
Merge pull request #13 from silphid/add-tidy-command
Browse files Browse the repository at this point in the history
add tidy command
  • Loading branch information
silphid authored May 27, 2021
2 parents 22f2226 + e84ee66 commit d14fdbb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
66 changes: 66 additions & 0 deletions src/cmd/get/tidy/tidy.go
Original file line number Diff line number Diff line change
@@ -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: "Removes unreferenced 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
}
12 changes: 6 additions & 6 deletions src/internal/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal/yey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()),
)
}
2 changes: 2 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
Expand Down

0 comments on commit d14fdbb

Please sign in to comment.