From 04710bf8f48aad99141256bfc6a7e9ee63247a95 Mon Sep 17 00:00:00 2001 From: Sergey Voloshin Date: Thu, 15 Sep 2022 00:21:18 +0300 Subject: [PATCH 1/3] feat(service): autostart local services Refs: DL-T-30 --- command/up.go | 33 +++++++++++++++++++++++++++++++-- main.go | 6 +++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/command/up.go b/command/up.go index 95dc729..1948fa3 100644 --- a/command/up.go +++ b/command/up.go @@ -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" @@ -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...") @@ -78,6 +85,28 @@ func up() { showProjectInfo() } +func startLocalServices() error { + reader := bufio.NewReader(os.Stdin) + + pterm.FgRed.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 + } + return errors.New("start local services first: dl service up") +} + // showProjectInfo Display project links func showProjectInfo() { l := project.Env.GetString("LOCAL_DOMAIN") diff --git a/main.go b/main.go index 479a90a..5360dcb 100644 --- a/main.go +++ b/main.go @@ -86,18 +86,18 @@ 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") + 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.Fatal.Println(err) + pterm.FgRed.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") + pterm.FgRed.Println("An old version of wpdeploy is running. Please stop wpdeploy with the command: wpdeploy local-services down") return false } return true From ffece77abf6a7d1e983ab235520149425bfe3ec3 Mon Sep 17 00:00:00 2001 From: Sergey Voloshin Date: Thu, 15 Sep 2022 00:28:07 +0300 Subject: [PATCH 2/3] fix(service): autostart local services msg Refs: DL-T-30 --- command/up.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/command/up.go b/command/up.go index 1948fa3..d12f36e 100644 --- a/command/up.go +++ b/command/up.go @@ -88,7 +88,7 @@ func up() { func startLocalServices() error { reader := bufio.NewReader(os.Stdin) - pterm.FgRed.Print("Local services are not running. Would you like to launch (Y/n)?") + pterm.FgYellow.Print("Local services are not running. Would you like to launch (Y/n)? ") a, err := reader.ReadString('\n') if err != nil { @@ -104,7 +104,8 @@ func startLocalServices() error { } return nil } - return errors.New("start local services first: dl service up") + //goland:noinspection GoErrorStringFormat + return errors.New("Start local services first: dl service up") } // showProjectInfo Display project links From 0bd6b878ef68ccca71bc914360ccdd94f101f227 Mon Sep 17 00:00:00 2001 From: Sergey Voloshin Date: Thu, 15 Sep 2022 01:05:45 +0300 Subject: [PATCH 3/3] feat(service): legacy compatibility mode Refs: DL-T-30 --- helper/compatibility.go | 74 +++++++++++++++++++++++++++++++++++++++++ main.go | 27 +-------------- 2 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 helper/compatibility.go diff --git a/helper/compatibility.go b/helper/compatibility.go new file mode 100644 index 0000000..d208f39 --- /dev/null +++ b/helper/compatibility.go @@ -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 +} diff --git a/main.go b/main.go index 5360dcb..3d0d149 100644 --- a/main.go +++ b/main.go @@ -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" @@ -26,7 +22,7 @@ func main() { return } - if !wpdeployCheck() { + if !helper.WpdeployCheck() { return } @@ -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.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 { - pterm.FgRed.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 {