Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper filtering for yey get containers #34

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/cmd/get/containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,63 @@ import (
"github.com/TwinProduction/go-color"
"github.com/spf13/cobra"

yey "github.com/silphid/yey/src/internal"
"github.com/silphid/yey/src/internal/docker"
)

type Options struct {
All bool
}

// New creates a cobra command
func New() *cobra.Command {
return &cobra.Command{
var options Options

cmd := &cobra.Command{
Use: "containers",
Short: "Lists running containers",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return run(cmd.Context())
return run(cmd.Context(), options)
},
}

cmd.Flags().BoolVarP(&options.All, "all", "a", false, "include all yey containers")

return cmd
}

func run(ctx context.Context) error {
names, err := docker.ListContainers(ctx, true)
func run(ctx context.Context, options Options) error {
containers, err := docker.ListContainers(ctx, true)
if err != nil {
return err
}
if len(names) == 0 {
totalCount := len(containers)

if !options.All {
contexts, err := yey.LoadContexts()
if err != nil {
return err
}
prefix := yey.ContainerPathPrefix(contexts.Path)

var filteredContainers []string
for _, container := range containers {
if strings.HasPrefix(container, prefix) {
filteredContainers = append(filteredContainers, container)
}
}
containers = filteredContainers
}

if len(containers) == 0 {
if totalCount > 0 {
fmt.Fprintln(os.Stderr, color.Ize(color.Green, fmt.Sprintf("no project-specific yey containers found, but %d other(s) were found that you could include with --all flag", totalCount)))
return nil
}
fmt.Fprintln(os.Stderr, color.Ize(color.Green, "no yey containers found"))
return nil
}
fmt.Println(strings.Join(names, "\n"))
fmt.Println(strings.Join(containers, "\n"))
return nil
}
5 changes: 5 additions & 0 deletions src/cmd/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func run(ctx context.Context, names []string, options RemoveOptions) error {
if err != nil {
return fmt.Errorf("failed to list containers to prompt for removal: %w", err)
}
totalCount := len(containers)

// Compute all valid contexts
combos := contexts.GetCombos()
Expand Down Expand Up @@ -84,6 +85,10 @@ func run(ctx context.Context, names []string, options RemoveOptions) error {

// Abort if no containers to remove
if len(validContainers) == 0 && len(otherContainers) == 0 {
if totalCount > 0 {
fmt.Fprintln(os.Stderr, color.Ize(color.Green, fmt.Sprintf("no project-specific yey containers found, but %d other(s) were found that you could include with --all flag", totalCount)))
return nil
}
fmt.Fprintln(os.Stderr, color.Ize(color.Green, "no yey containers found to remove"))
return nil
}
Expand Down