Skip to content

Commit

Permalink
feat: Several adjustments to log messages (#1573)
Browse files Browse the repository at this point in the history
Reviewed-by: Cezar Craciunoiu <[email protected]>
Approved-by: Cezar Craciunoiu <[email protected]>
  • Loading branch information
craciunoiuc authored Apr 18, 2024
2 parents 5e51f02 + 9a1f49d commit 2e2a603
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
34 changes: 29 additions & 5 deletions internal/cli/kraft/cloud/instance/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kcinstances "sdk.kraft.cloud/instances"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
Expand Down Expand Up @@ -154,14 +155,37 @@ func Logs(ctx context.Context, opts *LogOptions, args ...string) error {
case <-ctx.Done():
return
case err := <-errChan:
errGroup = append(errGroup, err)
if err != nil {
errGroup = append(errGroup, err)
}

resp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, instance)
if err != nil {
errGroup = append(errGroup, err)
return
}

inst, err := resp.FirstOrErr()
if err != nil {
errGroup = append(errGroup, err)
}

if inst.StopCodeReason() != kcinstances.StopCodeReasonOK {
message := []string{
"",
"It looks like the instance exited fatally. To see more details about why, run:",
"",
fmt.Sprintf("\tkraft cloud instance get %s", inst.Name),
"",
}

consumer.Consume(message...)
}

return
case line, ok := <-logChan:
if ok {
if err := consumer.Consume(fmt.Sprintf("%s\n", line)); err != nil {
errGroup = append(errGroup, err)
return
}
consumer.Consume(line)
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions internal/cli/kraft/logs/colorful_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type LogConsumer interface {
Consume(line string) error
Consume(line ...string)
}

type ColorfulConsumer struct {
Expand All @@ -40,12 +40,12 @@ func NewColorfulConsumer(streams *iostreams.IOStreams, color bool, prefix string
}

// Consume implements logConsumer
func (c *ColorfulConsumer) Consume(s string) error {
if c.prefix != "" {
s = fmt.Sprintf("%s | %s", c.color(c.prefix), s)
}

fmt.Fprint(c.streams.Out, s)
func (c *ColorfulConsumer) Consume(strs ...string) {
for _, s := range strs {
if c.prefix != "" {
s = fmt.Sprintf("%s | %s", c.color(c.prefix), s)
}

return nil
fmt.Fprintf(c.streams.Out, "%s\n", s)
}
}
9 changes: 2 additions & 7 deletions internal/cli/kraft/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ func (opts *LogOptions) Run(ctx context.Context, args []string) error {
} else {
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
if err := consumer.Consume(scanner.Text() + "\n"); err != nil {
errGroup = append(errGroup, err)
}
consumer.Consume(scanner.Text())
}
}
}
Expand Down Expand Up @@ -285,10 +283,7 @@ loop:
// Wait on either channel
select {
case line := <-logs:
if err := consumer.Consume(line); err != nil {
log.G(ctx).Errorf("could not consume log line: %v", err)
return err
}
consumer.Consume(line)

case err := <-errs:
eof = true
Expand Down
2 changes: 1 addition & 1 deletion internal/logtail/logtail.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func peekAndRead(file *os.File, reader *bufio.Reader, logs *chan string, errs *c
}

if len(s) > discarded {
*logs <- string(s[discarded:])
*logs <- string(s[discarded : len(s)-1])
}

return false
Expand Down

0 comments on commit 2e2a603

Please sign in to comment.