Skip to content

Commit

Permalink
Merge pull request #26 from local-deploy/DL-T-30
Browse files Browse the repository at this point in the history
legacy compatibility
  • Loading branch information
varrcan authored Sep 16, 2022
2 parents c294293 + 0bd6b87 commit 0152368
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 28 deletions.
34 changes: 32 additions & 2 deletions command/up.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package command

import (
"bufio"
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"

"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -43,8 +47,11 @@ func up() {
traefikExists, err := cli.ContainerList(ctx, types.ContainerListOptions{Filters: containerFilter})

if len(traefikExists) == 0 {
pterm.FgRed.Printfln("Start local services first: dl service up")
return
err := startLocalServices()
if err != nil {
pterm.FgRed.Println(err)
return
}
}

pterm.FgGreen.Printfln("Starting project...")
Expand Down Expand Up @@ -78,6 +85,29 @@ func up() {
showProjectInfo()
}

func startLocalServices() error {
reader := bufio.NewReader(os.Stdin)

pterm.FgYellow.Print("Local services are not running. Would you like to launch (Y/n)? ")

a, err := reader.ReadString('\n')
if err != nil {
return err
}

a = strings.TrimSpace(a)
if strings.ToLower(a) == "y" || a == "" {
ctx := context.Background()
err := progress.Run(ctx, upService)
if err != nil {
return err
}
return nil
}
//goland:noinspection GoErrorStringFormat
return errors.New("Start local services first: dl service up")
}

// showProjectInfo Display project links
func showProjectInfo() {
l := project.Env.GetString("LOCAL_DOMAIN")
Expand Down
74 changes: 74 additions & 0 deletions helper/compatibility.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package helper

import (
"bufio"
"context"
"errors"
"os"
"os/exec"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/pterm/pterm"
)

// WpdeployCheck Legacy app check
func WpdeployCheck() bool {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
pterm.FgRed.Println("Failed to connect to socket")
return false
}

containerFilter := filters.NewArgs(filters.Arg("label", "com.docker.compose.project=local-services"))
isExists, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true, Filters: containerFilter})
if err != nil {
pterm.FgRed.Println(err)
return false
}
if len(isExists) > 0 {
err := wpdeployDown()
if err != nil {
pterm.FgRed.Println(err)
return false
}
return false
}
return true
}

func wpdeployDown() error {
dir, _ := os.Getwd()
wpdeploy, _ := exec.LookPath("wpdeploy")
reader := bufio.NewReader(os.Stdin)

pterm.FgYellow.Print("An old version of wpdeploy is running. Do you want to stop it (Y/n)? ")

a, err := reader.ReadString('\n')
if err != nil {
return err
}

//goland:noinspection GoErrorStringFormat
errorMsg := errors.New("Stop wpdeploy local services first: wpdeploy local-services down")

a = strings.TrimSpace(a)
if strings.ToLower(a) == "y" || a == "" {
cmdDown := &exec.Cmd{
Path: wpdeploy,
Dir: dir,
Args: []string{wpdeploy, "local-services", "down"},
Stdout: os.Stdout,
Stderr: os.Stderr,
}
err = cmdDown.Run()
if err != nil {
return err
}
return nil
}
return errorMsg
}
27 changes: 1 addition & 26 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package main

import (
"context"
"os/exec"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -26,7 +22,7 @@ func main() {
return
}

if !wpdeployCheck() {
if !helper.WpdeployCheck() {
return
}

Expand Down Expand Up @@ -82,27 +78,6 @@ func createConfigFile() error {
return errWrite
}

func wpdeployCheck() bool {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
pterm.Fatal.Println("Failed to connect to socket")
return false
}

containerFilter := filters.NewArgs(filters.Arg("label", "com.docker.compose.project=local-services"))
isExists, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true, Filters: containerFilter})
if err != nil {
pterm.Fatal.Println(err)
return false
}
if len(isExists) > 0 {
pterm.Error.Println("An old version of wpdeploy is running. Please stop wpdeploy with the command: wpdeploy local-services down")
return false
}
return true
}

func dockerCheck() bool {
_, err := exec.LookPath("docker")
if err != nil {
Expand Down

0 comments on commit 0152368

Please sign in to comment.