Skip to content

Commit

Permalink
fix docker-compose version regexp, add docker prune command
Browse files Browse the repository at this point in the history
  • Loading branch information
paramah committed Dec 14, 2022
1 parent bff551c commit 9dfd092
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 28 deletions.
1 change: 1 addition & 0 deletions app/cmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ var CmdDocker = cli.Command{
&docker.CmdComposePull,
&docker.CmdComposeStop,
&docker.CmdDockerLogin,
&docker.CmdPrune,
},
}
46 changes: 46 additions & 0 deletions app/cmd/docker/prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package docker

import (
"github.com/AlecAivazis/survey/v2"
"github.com/paramah/ledo/app/logger"
"github.com/paramah/ledo/app/modules/context"
"github.com/paramah/ledo/app/modules/docker"
"github.com/urfave/cli/v2"
"os"
)

var CmdPrune = cli.Command{
Name: "prune",
Usage: "clean and prune docker ",
Description: `Old and working docker system prune version.`,
Action: RunPrune,
}

func RunPrune(cmd *cli.Context) error {
var err error
ctx := context.InitCommand(cmd)

if ctx.Mode.CurrentMode != "dev" {
logger.Exit("docker prune is only available in dev mode!")
os.Exit(255)
}

wantPrune := false
prompt := &survey.Confirm{
Message: "Do You want prune docker (all data will be irretrievably lost) ?",
}
err = survey.AskOne(prompt, &wantPrune)
if err != nil {
return err
}
if wantPrune {
err = docker.ExecDockerPrune(ctx)
if err != nil {
return err
}
} else {
logger.Info("Done!")
}

return nil
}
17 changes: 17 additions & 0 deletions app/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ func Execute(msg string) {
pterm.Success.Printf("%v\n", msg)
}

func Info(msg string) {
pterm.Success.Prefix = pterm.Prefix{
Text: levelInfoName,
Style: pterm.NewStyle(pterm.BgGreen, pterm.FgBlack),
}
pterm.Success.Printf("%v\n", msg)
}


func Debug(msg string) {
pterm.Debug.Prefix = pterm.Prefix{
Text: levelDebugName,
Expand Down Expand Up @@ -49,3 +58,11 @@ func Critical(msg string, err error) {
os.Exit(1)
}

func Exit(msg string) {
pterm.Error.Prefix = pterm.Prefix{
Text: levelCriticalName,
Style: pterm.NewStyle(pterm.BgRed, pterm.FgLightWhite),
}
pterm.Error.Printf("%v\n", msg)
os.Exit(1)
}
7 changes: 5 additions & 2 deletions app/modules/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compose
import (
"bytes"
"errors"
"fmt"
"github.com/Masterminds/semver"
"github.com/paramah/ledo/app/logger"
"github.com/paramah/ledo/app/modules/context"
Expand All @@ -28,9 +29,11 @@ func CheckDockerComposeVersion() {
logger.Critical("No docker-compose installed. Please install docker-compose ie. via `pip3 install docker-compose`", err)
}

r := regexp.MustCompile("(.*){1}(version\\ ){1}(([0-9]+)\\.([0-9]+)\\.([0-9]+))")
r := regexp.MustCompile("(.*){1}(version\\ )(v{0,1}){1}(([0-9]+)\\.([0-9]+)\\.([0-9]+))")
result := r.FindStringSubmatch(output.String())
composeVersion := result[3]
composeVersion := result[4]

fmt.Printf("Output: %s", result)

verConstraint, _ := semver.NewConstraint(DockerComposeVersion)
composeSemVer, _ := semver.NewVersion(composeVersion)
Expand Down
14 changes: 11 additions & 3 deletions app/modules/config/ledofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type LedoFile struct {
Docker DockerMap `yaml:"docker,omitempty`
Modes map[string]string `yaml:"modes,omitempty"`
Project string `yaml:"project,omitempty"`
Docker DockerMap `yaml:"docker"`
Modes map[string]string `yaml:"modes"`
Project string `yaml:"project"`
Deployment []Deployment `yaml:"deployment,omitempty"`
}

type DockerMap struct {
Expand All @@ -22,6 +23,13 @@ type DockerMap struct {
Username string `yaml:"username,omitempty"`
}

type Deployment struct {
Host string `yaml:"host"`
IsSecure bool `yaml:"is_secure"`
TlsDirectory string `yaml:"tls_directory"`
Mode string `yaml:"mode"`
}

func NewLedoFile(s string) (*LedoFile, error) {
yamlFile, err := ioutil.ReadFile(s)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion app/modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func LoadConfigFile() (*config.LedoFile, error) {
return cfg, nil
}

func (lx *LedoContext) ExecCmdOutput(cmd string, cmdArgs []string) ([]byte, error) {
command, _ := exec.Command(cmd, cmdArgs...).Output()
return command, nil
}

func (lx *LedoContext) ExecCmd(cmd string, cmdArgs []string) error {
logger.Execute(fmt.Sprintf("%v %v\n", cmd, strings.Join(cmdArgs, " ")))
command := exec.Command(cmd, cmdArgs...)
Expand All @@ -94,7 +99,7 @@ func (lx *LedoContext) ExecCmd(cmd string, cmdArgs []string) error {
func (lx *LedoContext) ExecCmdSilent(cmd string, cmdArgs []string) error {
command := exec.Command(cmd, cmdArgs...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stdout = nil
command.Stderr = nil

if err := command.Start(); err != nil {
Expand Down
Loading

0 comments on commit 9dfd092

Please sign in to comment.