From 66cba1abfb5967726cdf13b5560d9414ec879f9d Mon Sep 17 00:00:00 2001 From: Sebastian Schwarz Date: Fri, 13 Apr 2018 14:45:27 +0200 Subject: [PATCH 1/9] Only colorize output if stdout is a terminal --- outlet.go | 16 ++++++++++------ start.go | 4 ++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/outlet.go b/outlet.go index 596b98a..f740462 100644 --- a/outlet.go +++ b/outlet.go @@ -77,17 +77,21 @@ func (of *OutletFactory) WriteLine(left, right string, leftC, rightC ct.Color, i of.Lock() defer of.Unlock() - ct.ChangeColor(leftC, true, ct.None, false) + if colorize { + ct.ChangeColor(leftC, true, ct.None, false) + } formatter := fmt.Sprintf("%%-%ds | ", of.Padding) fmt.Printf(formatter, left) - if isError { - ct.ChangeColor(ct.Red, true, ct.None, true) - } else { - ct.ResetColor() + if colorize { + if isError { + ct.ChangeColor(rightC, true, ct.None, true) + } else { + ct.ResetColor() + } } fmt.Println(right) - if isError { + if colorize && isError { ct.ResetColor() } } diff --git a/start.go b/start.go index 59f5e47..a943e51 100644 --- a/start.go +++ b/start.go @@ -11,6 +11,8 @@ import ( "sync" "syscall" "time" + + "golang.org/x/crypto/ssh/terminal" ) const defaultPort = 5000 @@ -21,6 +23,7 @@ var flagConcurrency string var flagRestart bool var flagShutdownGraceTime int var envs envFiles +var colorize bool var cmdStart = &Command{ Run: runStart, @@ -88,6 +91,7 @@ func init() { cmdStart.Flag.IntVar(&flagShutdownGraceTime, "t", defaultShutdownGraceTime, "shutdown grace time") err := readConfigFile(".forego", &flagProcfile, &flagPort, &flagConcurrency, &flagShutdownGraceTime) handleError(err) + colorize = terminal.IsTerminal(int(os.Stdout.Fd())) } func readConfigFile(config_path string, flagProcfile *string, flagPort *int, flagConcurrency *string, flagShutdownGraceTime *int) error { From 402f02ad3e120f385fcf41ed1cc1f71ace350347 Mon Sep 17 00:00:00 2001 From: Sebastian Schwarz Date: Tue, 15 May 2018 14:30:37 +0200 Subject: [PATCH 2/9] Use /bin/sh instead of bash There are plenty of environments without BASH, but with sh. --- unix.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unix.go b/unix.go index 84f158c..feeb052 100644 --- a/unix.go +++ b/unix.go @@ -14,8 +14,8 @@ func ShellInvocationCommand(interactive bool, root, command string) []string { if interactive { shellArgument = "-ic" } - shellCommand := fmt.Sprintf("cd \"%s\"; source .profile 2>/dev/null; exec %s", root, command) - return []string{"sh", shellArgument, shellCommand} + shellCommand := fmt.Sprintf("cd \"%s\"; . ./.profile 2>/dev/null; exec %s", root, command) + return []string{"/bin/sh", shellArgument, shellCommand} } func (p *Process) PlatformSpecificInit() { From 5a92dac359a33b88c5f14f6924e978597db50d94 Mon Sep 17 00:00:00 2001 From: Sebastian Schwarz Date: Tue, 15 May 2018 14:43:59 +0200 Subject: [PATCH 3/9] Safeguard against shell characters in directory These could have caused problems: $!"\ --- unix.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unix.go b/unix.go index feeb052..1594687 100644 --- a/unix.go +++ b/unix.go @@ -14,8 +14,8 @@ func ShellInvocationCommand(interactive bool, root, command string) []string { if interactive { shellArgument = "-ic" } - shellCommand := fmt.Sprintf("cd \"%s\"; . ./.profile 2>/dev/null; exec %s", root, command) - return []string{"/bin/sh", shellArgument, shellCommand} + shellCommand := fmt.Sprintf("cd \"$1\"; . ./.profile 2>/dev/null; exec %s", command) + return []string{"/bin/sh", shellArgument, shellCommand, "sh", root} } func (p *Process) PlatformSpecificInit() { From ddae874cc6f6fbf2a358200f8f02d96132bb8c84 Mon Sep 17 00:00:00 2001 From: Sebastian Schwarz Date: Tue, 15 May 2018 14:43:59 +0200 Subject: [PATCH 4/9] Improve error handling. - exit if directory doesn't exist - source .profile only if it exists - don't suppress stderr while doing that --- unix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unix.go b/unix.go index 1594687..ef42d06 100644 --- a/unix.go +++ b/unix.go @@ -14,7 +14,7 @@ func ShellInvocationCommand(interactive bool, root, command string) []string { if interactive { shellArgument = "-ic" } - shellCommand := fmt.Sprintf("cd \"$1\"; . ./.profile 2>/dev/null; exec %s", command) + shellCommand := fmt.Sprintf("cd \"$1\" || exit 66; test -e .profile && . ./.profile; exec %s", command) return []string{"/bin/sh", shellArgument, shellCommand, "sh", root} } From 0384c1724e2946f7e94d5fd7e448d93c83625560 Mon Sep 17 00:00:00 2001 From: iain barnett Date: Sat, 21 Sep 2019 15:20:30 +0900 Subject: [PATCH 5/9] The -f operator is a bit more specific. --- unix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unix.go b/unix.go index ef42d06..9e891e9 100644 --- a/unix.go +++ b/unix.go @@ -14,7 +14,7 @@ func ShellInvocationCommand(interactive bool, root, command string) []string { if interactive { shellArgument = "-ic" } - shellCommand := fmt.Sprintf("cd \"$1\" || exit 66; test -e .profile && . ./.profile; exec %s", command) + shellCommand := fmt.Sprintf("cd \"$1\" || exit 66; test -f .profile && . ./.profile; exec %s", command) return []string{"/bin/sh", shellArgument, shellCommand, "sh", root} } From 0802a0eac31553b88881a6018c29e86a5553eb68 Mon Sep 17 00:00:00 2001 From: David Terei Date: Tue, 17 Apr 2018 10:29:52 -0700 Subject: [PATCH 6/9] Fix uses of format flags in non printf functions --- start_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/start_test.go b/start_test.go index b57ca5f..8982588 100644 --- a/start_test.go +++ b/start_test.go @@ -123,7 +123,7 @@ func TestPortFromEnv(t *testing.T) { os.Setenv("PORT", "4000") port, err = basePort(env) if err != nil { - t.Fatal("Can not get port: %s", err) + t.Fatalf("Can not get port: %s", err) } if port != 4000 { t.Fatal("Base port should be 4000") @@ -162,14 +162,14 @@ func TestConfigBeOverrideByForegoFile(t *testing.T) { } if port != 15000 { - t.Fatal("port should be 15000, got %d", port) + t.Fatalf("port should be 15000, got %d", port) } if concurrency != "foo=2,bar=3" { - t.Fatal("concurrency should be 'foo=2,bar=3', got %s", concurrency) + t.Fatalf("concurrency should be 'foo=2,bar=3', got %s", concurrency) } if gracetime != 30 { - t.Fatal("gracetime should be 3, got %d", gracetime) + t.Fatalf("gracetime should be 3, got %d", gracetime) } } From 6b1ec8965b4440616adfd97e0cb4794236c41e47 Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Sun, 12 Apr 2020 09:11:50 -0400 Subject: [PATCH 7/9] Add injecting of version tag into build Injects Makefile arg VERSION into main.Version with default value of "dev". --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a69d6f5..1cb44ea 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ BIN = forego SRC = $(shell find . -name '*.go' -not -path './vendor/*') +VERSION = dev .PHONY: all build clean lint release test @@ -20,4 +21,4 @@ test: lint build go test -v -race -cover ./... $(BIN): $(SRC) - go build -o $@ + go build -ldflags "-X main.Version=$(VERSION)" -o $@ From cea46c39e0ca0f4abf8ee8bb280a128ce9533fd2 Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Sun, 12 Apr 2020 09:32:04 -0400 Subject: [PATCH 8/9] Disable colored output if NO_COLOR is defined --- start.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start.go b/start.go index a943e51..29eaf2e 100644 --- a/start.go +++ b/start.go @@ -91,7 +91,7 @@ func init() { cmdStart.Flag.IntVar(&flagShutdownGraceTime, "t", defaultShutdownGraceTime, "shutdown grace time") err := readConfigFile(".forego", &flagProcfile, &flagPort, &flagConcurrency, &flagShutdownGraceTime) handleError(err) - colorize = terminal.IsTerminal(int(os.Stdout.Fd())) + colorize = os.Getenv("NO_COLOR") == "" && terminal.IsTerminal(int(os.Stdout.Fd())) } func readConfigFile(config_path string, flagProcfile *string, flagPort *int, flagConcurrency *string, flagShutdownGraceTime *int) error { From 483193b018da367643eeed21febce6dbc084c1bd Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Sun, 12 Apr 2020 10:05:15 -0400 Subject: [PATCH 9/9] Create outlet formatter once rather than for each line The output padding is based upon the longest name of processes and thus never changes at runtime so we can optimize things a bit by only creating the output formatter pattern once at startup. --- outlet.go | 5 ++--- start.go | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/outlet.go b/outlet.go index f740462..4f699ed 100644 --- a/outlet.go +++ b/outlet.go @@ -12,7 +12,7 @@ import ( ) type OutletFactory struct { - Padding int + LeftFormatter string sync.Mutex } @@ -80,8 +80,7 @@ func (of *OutletFactory) WriteLine(left, right string, leftC, rightC ct.Color, i if colorize { ct.ChangeColor(leftC, true, ct.None, false) } - formatter := fmt.Sprintf("%%-%ds | ", of.Padding) - fmt.Printf(formatter, left) + fmt.Printf(of.LeftFormatter, left) if colorize { if isError { diff --git a/start.go b/start.go index 29eaf2e..9c7c60b 100644 --- a/start.go +++ b/start.go @@ -279,7 +279,7 @@ func runStart(cmd *Command, args []string) { handleError(err) of := NewOutletFactory() - of.Padding = pf.LongestProcessName(concurrency) + of.LeftFormatter = fmt.Sprintf("%%-%ds | ", pf.LongestProcessName(concurrency)) f := &Forego{ outletFactory: of,