Skip to content

Commit

Permalink
feat(run): Introduce colorful prefix option
Browse files Browse the repository at this point in the history
Introduce the option to prepend all logs with
a colorful prefix that can help distinguish
between log streams.

Signed-off-by: Luca Seritan <luca.seritan@gmail.com>
  • Loading branch information
LucaSeri committed Nov 15, 2023
1 parent 21b8dd5 commit 9a8e35c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/cli/kraft/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ type RunOptions struct {
Memory string `long:"memory" short:"M" usage:"Assign memory to the unikernel (K/Ki, M/Mi, G/Gi)" default:"64Mi"`
Name string `long:"name" short:"n" usage:"Name of the instance"`
Network string `long:"network" usage:"Attach instance to the provided network in the format <driver>:<network>, e.g. bridge:kraft0"`
NoColor bool `long:"no-color" usage:"Disable color output for prefix"`
Platform string `noattribute:"true"`
Ports []string `long:"port" short:"p" usage:"Publish a machine's port(s) to the host" split:"false"`
Prefix string `long:"prefix" usage:"Prefix each log line with the given string (name expands to machine name)"`
Remove bool `long:"rm" usage:"Automatically remove the unikernel when it shutsdown"`
Rootfs string `long:"rootfs" usage:"Specify a path to use as root file system (can be volume or initramfs)"`
RunAs string `long:"as" usage:"Force a specific runner"`
Expand Down Expand Up @@ -351,7 +353,16 @@ func (opts *RunOptions) Run(ctx context.Context, args []string) error {

var exitErr error
if !opts.Detach {
exitErr = logs.FollowLogs(ctx, machine, opts.machineController)
if opts.Prefix == "name" {
opts.Prefix = machine.Name
}

consumer, err := logs.NewColorfulConsumer(iostreams.G(ctx), !opts.NoColor, opts.Prefix)
if err != nil {
return err
}

exitErr = logs.FollowLogs(ctx, machine, opts.machineController, consumer)

// Remove the instance on Ctrl+C if the --rm flag is passed
if opts.Remove {
Expand Down

0 comments on commit 9a8e35c

Please sign in to comment.