Skip to content

use markdown in output text #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/pkg/cli/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func NormalizeServiceName(s string) string {
}

func warnf(format string, args ...interface{}) {
logrus.Warnf(format, args...)
// By default, the logrus StandardLogger writes to os.Stderr
logrus.Warnf(term.MarkDown(term.Stderr, format), args...)
term.HadWarnings = true
}

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/cli/compose_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func validateProject(project *compose.Project) error {
warnf("unsupported compose directive: read_only")
}
if svccfg.Restart == "" {
warnf("missing compose directive: restart; assuming 'unless-stopped' (add 'restart' to silence)")
warnf("missing compose directive: `restart`; assuming 'unless-stopped' (add 'restart' to silence)")
} else if svccfg.Restart != "always" && svccfg.Restart != "unless-stopped" {
warnf("unsupported compose directive: restart; assuming 'unless-stopped' (add 'restart' to silence)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restart should be marked code here too.

}
Expand Down
10 changes: 5 additions & 5 deletions src/pkg/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ type Quotas struct {

func (q Quotas) Validate(service *defangv1.Service) error {
if service.Name == "" {
return errors.New("service name is required") // CodeInvalidArgument
return errors.New("service `name:` is required") // CodeInvalidArgument
}

if service.Build != nil {
if service.Build.Context == "" {
return errors.New("build.context is required") // CodeInvalidArgument
return errors.New("build `context:` is required") // CodeInvalidArgument
}
if service.Build.ShmSize > q.ShmSizeMiB || service.Build.ShmSize < 0 {
return fmt.Errorf("build.shm_size exceeds quota (max %v MiB)", q.ShmSizeMiB) // CodeInvalidArgument
return fmt.Errorf("build `shm_size:` exceeds quota (max %v MiB)", q.ShmSizeMiB) // CodeInvalidArgument
}
} else {
if service.Image == "" {
return errors.New("missing image or build") // CodeInvalidArgument
return errors.New("each service must have either `image:` or `build:`") // CodeInvalidArgument
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func (q Quotas) Validate(service *defangv1.Service) error {
return fmt.Errorf("ingress port requires a CMD healthcheck")
}
default:
return fmt.Errorf("unsupported healthcheck: %v", service.Healthcheck.Test)
return fmt.Errorf("unsupported `healthcheck:` %v", service.Healthcheck.Test)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/pkg/quota/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ func TestValidate(t *testing.T) {
{
name: "empty service",
service: &defangv1.Service{},
wantErr: "service name is required",
wantErr: "service `name:` is required",
},
{
name: "no image, no build",
service: &defangv1.Service{Name: "test"},
wantErr: "missing image or build",
wantErr: "each service must have either `image:` or `build:`",
},
{
name: "empty build",
service: &defangv1.Service{Name: "test", Build: &defangv1.Build{}},
wantErr: "build.context is required",
wantErr: "build `context:` is required",
},
{
name: "shm size exceeds quota",
service: &defangv1.Service{Name: "test", Build: &defangv1.Build{Context: ".", ShmSize: 30721}},
wantErr: "build.shm_size exceeds quota (max 30720 MiB)",
wantErr: "build `shm_size:` exceeds quota (max 30720 MiB)",
},
{
name: "port 0 out of range",
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestValidate(t *testing.T) {
Test: []string{"BLAH"},
},
},
wantErr: "unsupported healthcheck: [BLAH]",
wantErr: "unsupported `healthcheck:` [BLAH]",
},
{
name: "too many replicas",
Expand Down
15 changes: 15 additions & 0 deletions src/pkg/term/colorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package term
import (
"fmt"
"os"
"regexp"

"github.com/muesli/termenv"
"golang.org/x/term"
Expand Down Expand Up @@ -43,13 +44,27 @@ func ForceColor(color bool) {
}
}

var backticksRegex = regexp.MustCompile("`([^`]+)`")

func markdown(msg string) string {
return backticksRegex.ReplaceAllString(msg, termenv.CSI+"7m$1"+termenv.CSI+"27m")
}

func MarkDown(w *termenv.Output, msg string) string {
if !DoColor(w) {
return msg
}
return markdown(msg)
}

func output(w *termenv.Output, c Color, msg string) (int, error) {
if len(msg) == 0 {
return 0, nil
}
if DoColor(w) {
w.WriteString(termenv.CSI + c.Sequence(false) + "m")
defer w.Reset()
msg = markdown(msg)
}
return w.WriteString(msg)
}
Expand Down