Skip to content

Commit 9150395

Browse files
committed
use markdown in output text
1 parent 173770b commit 9150395

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

src/pkg/cli/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func NormalizeServiceName(s string) string {
7070
}
7171

7272
func warnf(format string, args ...interface{}) {
73-
logrus.Warnf(format, args...)
73+
// By default, the logrus StandardLogger writes to os.Stderr
74+
logrus.Warnf(term.MarkDown(term.Stderr, format), args...)
7475
term.HadWarnings = true
7576
}
7677

src/pkg/cli/compose_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func validateProject(project *compose.Project) error {
2929
warnf("unsupported compose directive: read_only")
3030
}
3131
if svccfg.Restart == "" {
32-
warnf("missing compose directive: restart; assuming 'unless-stopped' (add 'restart' to silence)")
32+
warnf("missing compose directive: `restart`; assuming 'unless-stopped' (add 'restart' to silence)")
3333
} else if svccfg.Restart != "always" && svccfg.Restart != "unless-stopped" {
3434
warnf("unsupported compose directive: restart; assuming 'unless-stopped' (add 'restart' to silence)")
3535
}

src/pkg/quota/quota.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ type Quotas struct {
2020

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

2626
if service.Build != nil {
2727
if service.Build.Context == "" {
28-
return errors.New("build.context is required") // CodeInvalidArgument
28+
return errors.New("build `context:` is required") // CodeInvalidArgument
2929
}
3030
if service.Build.ShmSize > q.ShmSizeMiB || service.Build.ShmSize < 0 {
31-
return fmt.Errorf("build.shm_size exceeds quota (max %v MiB)", q.ShmSizeMiB) // CodeInvalidArgument
31+
return fmt.Errorf("build `shm_size:` exceeds quota (max %v MiB)", q.ShmSizeMiB) // CodeInvalidArgument
3232
}
3333
} else {
3434
if service.Image == "" {
35-
return errors.New("missing image or build") // CodeInvalidArgument
35+
return errors.New("each service must have either `image:` or `build:`") // CodeInvalidArgument
3636
}
3737
}
3838

@@ -91,7 +91,7 @@ func (q Quotas) Validate(service *defangv1.Service) error {
9191
return fmt.Errorf("ingress port requires a CMD healthcheck")
9292
}
9393
default:
94-
return fmt.Errorf("unsupported healthcheck: %v", service.Healthcheck.Test)
94+
return fmt.Errorf("unsupported `healthcheck:` %v", service.Healthcheck.Test)
9595
}
9696
}
9797

src/pkg/quota/quota_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ func TestValidate(t *testing.T) {
1515
{
1616
name: "empty service",
1717
service: &defangv1.Service{},
18-
wantErr: "service name is required",
18+
wantErr: "service `name:` is required",
1919
},
2020
{
2121
name: "no image, no build",
2222
service: &defangv1.Service{Name: "test"},
23-
wantErr: "missing image or build",
23+
wantErr: "each service must have either `image:` or `build:`",
2424
},
2525
{
2626
name: "empty build",
2727
service: &defangv1.Service{Name: "test", Build: &defangv1.Build{}},
28-
wantErr: "build.context is required",
28+
wantErr: "build `context:` is required",
2929
},
3030
{
3131
name: "shm size exceeds quota",
3232
service: &defangv1.Service{Name: "test", Build: &defangv1.Build{Context: ".", ShmSize: 30721}},
33-
wantErr: "build.shm_size exceeds quota (max 30720 MiB)",
33+
wantErr: "build `shm_size:` exceeds quota (max 30720 MiB)",
3434
},
3535
{
3636
name: "port 0 out of range",
@@ -145,7 +145,7 @@ func TestValidate(t *testing.T) {
145145
Test: []string{"BLAH"},
146146
},
147147
},
148-
wantErr: "unsupported healthcheck: [BLAH]",
148+
wantErr: "unsupported `healthcheck:` [BLAH]",
149149
},
150150
{
151151
name: "too many replicas",

src/pkg/term/colorizer.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package term
33
import (
44
"fmt"
55
"os"
6+
"regexp"
67

78
"github.com/muesli/termenv"
89
"golang.org/x/term"
@@ -43,13 +44,27 @@ func ForceColor(color bool) {
4344
}
4445
}
4546

47+
var backticksRegex = regexp.MustCompile("`([^`]+)`")
48+
49+
func markdown(msg string) string {
50+
return backticksRegex.ReplaceAllString(msg, termenv.CSI+"7m$1"+termenv.CSI+"27m")
51+
}
52+
53+
func MarkDown(w *termenv.Output, msg string) string {
54+
if !DoColor(w) {
55+
return msg
56+
}
57+
return markdown(msg)
58+
}
59+
4660
func output(w *termenv.Output, c Color, msg string) (int, error) {
4761
if len(msg) == 0 {
4862
return 0, nil
4963
}
5064
if DoColor(w) {
5165
w.WriteString(termenv.CSI + c.Sequence(false) + "m")
5266
defer w.Reset()
67+
msg = markdown(msg)
5368
}
5469
return w.WriteString(msg)
5570
}

0 commit comments

Comments
 (0)