-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: down command to remove containers
- Loading branch information
1 parent
e119a09
commit d3208be
Showing
7 changed files
with
94 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters