Skip to content

Commit

Permalink
use markdown in output text
Browse files Browse the repository at this point in the history
  • Loading branch information
lionello committed May 22, 2024
1 parent 173770b commit e626163
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
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)")
}
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
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

0 comments on commit e626163

Please sign in to comment.