From dfbb24d9956272a69fc5063dbc1adc67c7573a5e Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Wed, 16 Oct 2024 00:42:35 +0200 Subject: Make Runcommand optional --- main.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index e197b10..be9d57b 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,16 @@ -// [PROGRAM] will run a build command and a run command on file system changes. This is so you can +// wbr will run a build command and a run command on file system changes. This is so you can // build&run on save. // You specify a build command and run command as flags and optionally you can filter by extension. +// TODO: +// fix bug when only build command is provided that the output is like this: +// [./build.sh [./build.sh] ./main.c ] + package main import ( "flag" "fmt" - "os" "os/exec" "strings" @@ -17,7 +20,8 @@ import ( // Colors in escape sequences var ( Reset = "\033[0m" - BoldCyan = "\033[36m" + BoldCyan = "\033[36;1m" + Cyan = "\033[36m" // Red = "\033[31m" // Green = "\033[32m" // Yellow = "\033[33m" @@ -45,7 +49,7 @@ func CompileRun(fileName string) { } // Build the program, exit if return if there was an error - { + if len(BuildCommand) > 0 && BuildCommand[0] != "" { var buildCmd *exec.Cmd if len(BuildCommand) > 1 { buildCmd = exec.Command(BuildCommand[0], append(BuildCommand[1:], fileName)...) @@ -56,11 +60,12 @@ func CompileRun(fileName string) { out, err := buildCmd.CombinedOutput() if len(BuildCommand) > 1 { - fmt.Printf("\n[%s%s %s%s]\n", BoldCyan, BuildCommand[0], strings.Join(append(BuildCommand[1:], fileName), " "), Reset) + fmt.Printf("[%s%s %s%s]\n", Cyan, BuildCommand[0], strings.Join(append(BuildCommand[1:], fileName), " "), Reset) } else { - fmt.Printf("[%s%s %s %s%s]\n", BoldCyan, BuildCommand[0], BuildCommand, fileName, Reset) + fmt.Printf("[%s%s %s %s%s]\n", Cyan, BuildCommand[0], BuildCommand, fileName, Reset) } + fmt.Println(len(BuildCommand)) if len(out) > 0 { fmt.Printf("%s", out) } @@ -74,8 +79,8 @@ func CompileRun(fileName string) { } } - // There was no error in the build so we can execute the run command - { + if len(RunCommand) > 0 && RunCommand[0] != "" { + // There was no error in the build so we can execute the run command var runCmd *exec.Cmd if len(RunCommand) > 1 { runCmd = exec.Command(RunCommand[0], RunCommand[1:]...) @@ -85,9 +90,9 @@ func CompileRun(fileName string) { out, err := runCmd.CombinedOutput() if len(RunCommand) > 1 { - fmt.Printf("[%s%s %s%s]\n", BoldCyan, RunCommand[0], strings.Join(append(RunCommand[1:]), " "), Reset) + fmt.Printf("[%s%s %s%s]\n", Cyan, RunCommand[0], strings.Join(RunCommand[1:], " "), Reset) } else { - fmt.Printf("[%s%s%s]\n", BoldCyan, RunCommand[0], Reset) + fmt.Printf("[%s%s%s]\n", Cyan, RunCommand[0], Reset) } if len(out) > 0 { @@ -118,17 +123,12 @@ func main() { p := flag.String("p", ".", "Path to watch for files") flag.Parse() - if *bc == "" { - fmt.Println("flag is required: -b") - os.Exit(1) - } - BuildCommand = strings.Split(*bc, " ") RunCommand = strings.Split(*rc, " ") Ext = *e Path = *p - fmt.Printf("[%swatching '%s'%s]", BoldCyan, Path, Reset) + fmt.Printf("[%swbr%s:%s watching '%s'%s]\n", BoldCyan, Reset, Cyan, Path, Reset) go func() { for { -- cgit v1.2.3