Skip to content

Commit

Permalink
feat: down command to remove containers
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzakhany committed Sep 30, 2022
1 parent e119a09 commit d3208be
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 14 deletions.
52 changes: 52 additions & 0 deletions internal/cmd/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"errors"
"fmt"
"strings"

"github.com/mirzakhany/dbctl/internal/container"
"github.com/spf13/cobra"
)

// GetDownCmd represents the down command
func GetDownCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "down",
Short: "stop one or more detached databases",
RunE: runDown,
}
return cmd
}

func runDown(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("invalid args, can be postgres(pg) and/or redis(rs)")
}

ctx := contextWithOsSignal()
r, err := container.List(ctx)
if err != nil {
return err
}

toRemove := make(map[string]struct{})
if contain(args, "postgres", "pg") {
toRemove["pg"] = struct{}{}
}

if contain(args, "redis", "rs") {
toRemove["rs"] = struct{}{}
}

for _, c := range r {
t := strings.Split(c.Name, "_")[1]
if _, ok := toRemove[t]; ok {
if err := container.Remove(ctx, c); err != nil {
return fmt.Errorf("stop database %s failed", c.Name)
}
}
}

return nil
}
11 changes: 1 addition & 10 deletions internal/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

// GetUpCmd represents the start command
// GetUpCmd represents the up command
func GetUpCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "up",
Expand All @@ -33,12 +33,3 @@ func runUp(cmd *cobra.Command, args []string) error {

return errors.New("invalid args, can be postgres(pg) and/or redis(rs)")
}

func contain(src []string, target, alias string) bool {
for _, s := range src {
if s == target || s == alias {
return true
}
}
return false
}
9 changes: 9 additions & 0 deletions internal/cmd/context.go → internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ func contextWithOsSignal(sig ...os.Signal) context.Context {
}()
return ctx
}

func contain(src []string, target, alias string) bool {
for _, s := range src {
if s == target || s == alias {
return true
}
}
return false
}
31 changes: 29 additions & 2 deletions internal/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package container
import (
"context"
"io"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand All @@ -19,7 +20,8 @@ type Request struct {
}

type Container struct {
ID string
ID string
Name string
}

func Run(ctx context.Context, req Request) (*Container, error) {
Expand Down Expand Up @@ -58,7 +60,7 @@ func Run(ctx context.Context, req Request) (*Container, error) {
return nil, err
}

cn := &Container{ID: resp.ID}
cn := &Container{ID: resp.ID, Name: req.Name}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return cn, err
}
Expand All @@ -79,3 +81,28 @@ func (c *Container) Terminate(ctx context.Context) error {
})
return err
}

func List(ctx context.Context) ([]*Container, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}

res, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return nil, err
}

out := make([]*Container, 0)
for _, c := range res {
n := c.Names[0]
if strings.HasPrefix(n, "/dbctl") {
out = append(out, &Container{ID: c.ID, Name: c.Names[0]})
}
}
return out, nil
}

func Remove(ctx context.Context, container *Container) error {
return container.Terminate(ctx)
}
2 changes: 1 addition & 1 deletion internal/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (p *Postgres) startUsingDocker(ctx context.Context) (func(ctx context.Conte
},
Cmd: []string{"postgres", "-c", "fsync=off", "-c", "synchronous_commit=off", "-c", "full_page_writes=off"},
ExposedPorts: []string{fmt.Sprintf("%s:5432/tcp", port)},
Name: fmt.Sprintf("dbctl_%d_%d", time.Now().Unix(), rnd.Uint64()),
Name: fmt.Sprintf("dbctl_pg_%d_%d", time.Now().Unix(), rnd.Uint64()),
})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (p *Redis) startUsingDocker(ctx context.Context) (func(ctx context.Context)
"--databases", "2000",
},
ExposedPorts: []string{fmt.Sprintf("%s:6379/tcp", port)},
Name: fmt.Sprintf("dbctl_%d_%d", time.Now().Unix(), rnd.Uint64()),
Name: fmt.Sprintf("dbctl_rs_%d_%d", time.Now().Unix(), rnd.Uint64()),
})
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {

root.AddCommand(cmd.GetStartCmd())
root.AddCommand(cmd.GetUpCmd())
root.AddCommand(cmd.GetDownCmd())
root.AddCommand(cmd.GetSelfUpdateCmd(version))

if err := root.Execute(); err != nil {
Expand Down

0 comments on commit d3208be

Please sign in to comment.