Skip to content

Commit

Permalink
Merge pull request #40 from nginx-proxy/nocolor
Browse files Browse the repository at this point in the history
feat: disable colored output if NO_COLOR is defined
  • Loading branch information
buchdag authored Dec 25, 2023
2 parents 7cfd546 + b69ab4c commit 30ad9ca
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
build-args: FOREGO_VERSION=${{ steps.forego_version.outputs.VERSION }}
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x
file: Dockerfile.${{ matrix.base }}
sbom: true
Expand Down
9 changes: 7 additions & 2 deletions Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
ARG FOREGO_VERSION=main

# Build forego
FROM --platform=$BUILDPLATFORM golang:1.21.5-alpine as go-builder

ENV CGO_ENABLED=0

ARG TARGETOS TARGETARCH TARGETVARIANT
ARG FOREGO_VERSION TARGETOS TARGETARCH TARGETVARIANT
ENV GOOS=$TARGETOS GOARCH=$TARGETARCH VARIANT=$TARGETVARIANT

RUN apk add --no-cache musl-dev
Expand All @@ -21,11 +23,14 @@ RUN set -eu; \
*) [ -z "$VARIANT" ] ;; \
esac; \
go env | grep -E 'OS=|ARCH=|ARM=|AMD64='; \
go build -o forego .; \
go build -ldflags "-X main.buildVersion=${FOREGO_VERSION}" -o forego .; \
go clean -cache

FROM --platform=$TARGETPLATFORM alpine:3.19.0

ARG FOREGO_VERSION
ENV FOREGO_VERSION=${FOREGO_VERSION}

RUN apk add --no-cache bash

# Install Forego
Expand Down
9 changes: 7 additions & 2 deletions Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
ARG FOREGO_VERSION=main

# Build forego
FROM --platform=$BUILDPLATFORM golang:1.21.5 as go-builder

ENV CGO_ENABLED=0

ARG TARGETOS TARGETARCH TARGETVARIANT
ARG FOREGO_VERSION TARGETOS TARGETARCH TARGETVARIANT
ENV GOOS=$TARGETOS GOARCH=$TARGETARCH VARIANT=$TARGETVARIANT

WORKDIR /build
Expand All @@ -19,11 +21,14 @@ RUN set -eu; \
*) [ -z "$VARIANT" ] ;; \
esac; \
go env | grep -E 'OS=|ARCH=|ARM=|AMD64='; \
go build -o forego .; \
go build -ldflags "-X main.buildVersion=${FOREGO_VERSION}" -o forego .; \
go clean -cache

FROM --platform=$TARGETPLATFORM debian:12.4-slim

ARG FOREGO_VERSION
ENV FOREGO_VERSION=${FOREGO_VERSION}

# Install Forego
COPY --from=go-builder /build/forego /usr/local/bin/forego

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
BIN = forego
SRC = $(shell find . -name '*.go')

TAG:=`git describe --tags`
LDFLAGS:=-X main.buildVersion=$(TAG)

.PHONY: all build clean lint test

all: build
Expand All @@ -20,4 +23,4 @@ test: lint get-deps build
go test -v -race -cover ./...

$(BIN): $(SRC)
go build -o $@
go build -ldflags "$(LDFLAGS)" -o $@
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ require (
github.com/subosito/gotenv v1.6.0
)

require golang.org/x/text v0.12.0 // indirect
require (
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
21 changes: 12 additions & 9 deletions outlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type OutletFactory struct {
Padding int
LeftFormatter string

sync.Mutex
}
Expand Down Expand Up @@ -77,17 +77,20 @@ func (of *OutletFactory) WriteLine(left, right string, leftC, rightC ct.Color, i
of.Lock()
defer of.Unlock()

ct.ChangeColor(leftC, true, ct.None, false)
formatter := fmt.Sprintf("%%-%ds | ", of.Padding)
fmt.Printf(formatter, left)
if colorize {
ct.ChangeColor(leftC, true, ct.None, false)
}
fmt.Printf(of.LeftFormatter, 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()
}
}
6 changes: 5 additions & 1 deletion start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"sync"
"syscall"
"time"

"golang.org/x/crypto/ssh/terminal"
)

const defaultPort = 5000
Expand All @@ -21,6 +23,7 @@ var flagConcurrency string
var flagRestart bool
var flagShutdownGraceTime int
var envs envFiles
var colorize bool

var cmdStart = &Command{
Run: runStart,
Expand Down Expand Up @@ -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 = os.Getenv("NO_COLOR") == "" && terminal.IsTerminal(int(os.Stdout.Fd()))
}

func readConfigFile(config_path string, flagProcfile *string, flagPort *int, flagConcurrency *string, flagShutdownGraceTime *int) error {
Expand Down Expand Up @@ -277,7 +281,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,
Expand Down
8 changes: 4 additions & 4 deletions unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin freebsd linux netbsd openbsd
//go:build darwin || freebsd || linux || netbsd || openbsd

package main

Expand All @@ -7,7 +7,7 @@ import (
"syscall"
)

var osShell string = "bash"
var osShell string = "/bin/sh"

const osHaveSigTerm = true

Expand All @@ -16,8 +16,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{osShell, shellArgument, shellCommand}
shellCommand := fmt.Sprintf("cd \"$1\" || exit 66; test -f .profile && . ./.profile; exec %s", command)
return []string{osShell, shellArgument, shellCommand, "sh", root}
}

func (p *Process) PlatformSpecificInit() {
Expand Down

0 comments on commit 30ad9ca

Please sign in to comment.