diff --git a/build b/build index 5efd0f2..518d670 100755 --- a/build +++ b/build @@ -14,29 +14,37 @@ build_go() { build_docker() { if [ $DOCKER_IMAGE ]; then image=$DOCKER_IMAGE + if echo $image | grep -q server; then + os=windows + exe=C:/build/listen/listen.exe + else + os=linux + exe=/usr/src/listen/listen + fi else - image=1-alpine - fi - - if echo $i | grep -q server; then - os=windows - exe=C:/build/listen/listen.exe - else - os=linux - exe=/usr/src/listen/listen + if [ -f /c/Program\ Files/Docker/Docker/DockerCli.exe ]; then + os=windows + exe=C:/build/listen/listen.exe + image=1-nanoserver + else + os=linux + exe=/usr/src/listen/listen + image=1-alpine + fi fi echo building with $os image golang:$image mkdir -p out/docker - docker build -t listen-build --build-arg IMAGE=$image -f docker/build.$os.Dockerfile . - docker create --name listen-build-tmp listen-build - docker cp listen-build-tmp:$exe out/docker - docker rm listen-build-tmp + if docker build -t listen-build --build-arg IMAGE=$image -f docker/build.$os.Dockerfile .; then + docker create --name listen-build-tmp listen-build + docker cp listen-build-tmp:$exe out/docker + docker rm listen-build-tmp + fi } mkdir -p out # if an arg is specified, force building with the specified method -[ $1 ] && build_$1 +[ $1 ] && build_$1 && exit # prefer building with local go install if it exists on path if which go &> /dev/null; then diff --git a/listen.go b/listen.go index facee04..627c9f1 100644 --- a/listen.go +++ b/listen.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "os/signal" + "runtime" "slices" "time" @@ -28,6 +29,15 @@ type Listen struct { RunFirst bool } +func printStatus(msg string) { + c := color.New(color.FgWhite) + if runtime.GOOS == "windows" { + c.Fprintf(color.Output, "%s", msg) + } else { + c.Fprintf(os.Stderr, "%s", msg) + } +} + func cksumCheck(l Listen, k string, t *bool) bool { hasher := sha256.New() f, err := os.Open(k) @@ -66,15 +76,15 @@ func allCheck(l Listen) bool { func runTrigger(l Listen, f string, r bool, quit chan bool) bool { if !l.Quiet && !r { if l.Condition == "any" { - color.New(color.FgWhite).Fprintf(os.Stderr, "& File %s modified. ", f) + printStatus(fmt.Sprintf("& File %s modified. ", f)) } else { - color.New(color.FgWhite).Fprint(os.Stderr, "& All files have been modified. ") + printStatus("& All files have been modified. ") } } if len(l.Command) >= 1 { if !l.Quiet { - color.New(color.FgWhite).Fprintln(os.Stderr, "Running command...") + printStatus("Running command...\n") } cmd := exec.Command(l.Command[0], l.Command[1:]...) @@ -93,7 +103,7 @@ func runTrigger(l Listen, f string, r bool, quit chan bool) bool { } if !l.Quiet { - color.New(color.FgWhite).Fprintln(os.Stderr, "& Returned to listening...") + printStatus("& Returned to listening...\n") } } else { if !l.Quiet { @@ -236,48 +246,47 @@ func loopInterval(l Listen, quit chan bool) { } func startMessage(l Listen) { - c := color.New(color.FgWhite) - c.Fprintf(os.Stderr, "& listen %s\n", VERSION) - c.Fprintln(os.Stderr, "& This program is free software, and comes with ABSOLUTELY NO WARRANTY.") - c.Fprintln(os.Stderr, "& Run 'listen --license' for details.") - c.Fprintln(os.Stderr, "&") + printStatus(fmt.Sprintf("& listen %s\n", VERSION)) + printStatus("& This program is free software, and comes with ABSOLUTELY NO WARRANTY.\n") + printStatus("& Run 'listen --license' for details.\n") + printStatus("&\n") if len(l.Command) >= 1 { - c.Fprintln(os.Stderr, "& This command will run:") - c.Fprint(os.Stderr, "& ") + printStatus("& This command will run:\n") + printStatus("& ") for _, value := range l.Command { - c.Fprint(os.Stderr, value, " ") + printStatus(value + " ") } - c.Fprintln(os.Stderr) - c.Fprint(os.Stderr, "& When ") + fmt.Println() + printStatus("& When ") } else { - c.Fprint(os.Stderr, "& listen will exit when ") + printStatus("& listen will exit when ") } if l.Cksum { - c.Fprint(os.Stderr, "the checksum of ") + printStatus("the checksum of ") } if len(l.FileMap) >= 2 { if l.Condition == "any" { - c.Fprint(os.Stderr, "any of ") + printStatus("any of ") } else { - c.Fprint(os.Stderr, "all ") + printStatus("all ") } - c.Fprint(os.Stderr, "these files have ") + printStatus("these files have ") } else { - c.Fprint(os.Stderr, "this file has ") + printStatus("this file has ") } if l.Cksum { - c.Fprintln(os.Stderr, "changed:") + printStatus("changed:\n") } else { - c.Fprintln(os.Stderr, "been modified:") + printStatus("been modified:\n") } for key := range l.FileMap { - c.Fprintf(os.Stderr, "& %s\n", key) + printStatus(fmt.Sprintf("& %s\n", key)) } } @@ -293,11 +302,11 @@ func (l Listen) Run() { if l.RunFirst { if !l.Quiet { - color.New(color.FgWhite).Fprintf(os.Stderr, "& -r option specified. ") + printStatus("& -r option specified. ") } runTrigger(l, "", true, quit) } else { - color.New(color.FgWhite).Fprintln(os.Stderr, "& Starting now...") + printStatus("& Starting now...\n") } // start main loop @@ -314,7 +323,7 @@ func (l Listen) Run() { } if !l.Quiet { - color.New(color.FgWhite).Fprintln(os.Stderr, "& Exiting...") + printStatus("& Exiting...\n") } }