From 8a94cca0234adc9aacee8bd43b592b6ee56836c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CSvnFrs=E2=80=9D?= Date: Thu, 4 Jan 2024 23:54:01 +0700 Subject: [PATCH] Contents require for new go-task command to work --- Taskfile.yml | 7 ++++--- app/dev/open-browser.go | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 66b1949..41f007d 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,6 +4,7 @@ tasks: run: desc: Run docker first then start the browser cmds: - - "sudo docker compose up --build -d" - - "sleep 2" - - "./app/dev/open-browser" \ No newline at end of file + - | + cd app/dev/ + go build open-browser.go + ./open-browser \ No newline at end of file diff --git a/app/dev/open-browser.go b/app/dev/open-browser.go index 9eb9119..071e8d0 100644 --- a/app/dev/open-browser.go +++ b/app/dev/open-browser.go @@ -1,9 +1,11 @@ package main import ( + "fmt" "os" "os/exec" "runtime" + "time" ) func main() { @@ -11,6 +13,35 @@ func main() { // Check the operating system switch runtime.GOOS { + case "linux": + dockerCmd := exec.Command("sudo", "docker-compose", "up", "--build", "-d") + if err := runAndWait(dockerCmd); err != nil { + fmt.Println("Error running Docker command:", err) + os.Exit(1) + } + case "darwin": + // Assume macOS uses the same command as Linux + dockerCmd := exec.Command("docker-compose", "up", "--build", "-d") + if err := runAndWait(dockerCmd); err != nil { + fmt.Println("Error running Docker command:", err) + os.Exit(1) + } + case "windows": + dockerCmd := exec.Command("docker-compose", "up", "--build", "-d") + if err := runAndWait(dockerCmd); err != nil { + fmt.Println("Error running Docker command:", err) + os.Exit(1) + } + default: + fmt.Println("Unsupported operating system") + os.Exit(1) + } + + // Wait for 10 seconds + time.Sleep(10 * time.Second) + + // Open the browser + switch runtime.GOOS { case "linux": exec.Command("xdg-open", url).Start() case "darwin": @@ -18,7 +49,13 @@ func main() { case "windows": exec.Command("cmd", "/c", "start", url).Start() default: - println("Unsupported operating system") + fmt.Println("Unsupported operating system") os.Exit(1) } } + +func runAndWait(cmd *exec.Cmd) error { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +}